From 2ada81e068a0eb7bb2d2ca760708cca77b7911e0 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Thu, 7 Mar 2024 13:57:07 -0800 Subject: [PATCH] cmd: tighten up env var usage sections (#2962) Also, document OLLAMA_HOST client semantics per command that honors it. This looks nicer than having a general puprose environment variable section in the root usage which was showing up after the "addition help topics" section outputed by Cobra's default template. It was decided this was easier to work with than using a custom template for Cobra right now. --- cmd/cmd.go | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 44481788..b9afb2e2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -887,6 +887,14 @@ func versionHandler(cmd *cobra.Command, _ []string) { } } +func appendHostEnvDocs(cmd *cobra.Command) { + const hostEnvDocs = ` +Environment Variables: + OLLAMA_HOST The host:port or base URL of the Ollama server (e.g. http://localhost:11434) +` + cmd.SetUsageTemplate(cmd.UsageTemplate() + hostEnvDocs) +} + func NewCLI() *cobra.Command { log.SetFlags(log.LstdFlags | log.Lshortfile) cobra.EnableCommandSorting = false @@ -952,7 +960,6 @@ func NewCLI() *cobra.Command { runCmd.Flags().Bool("insecure", false, "Use an insecure registry") runCmd.Flags().Bool("nowordwrap", false, "Don't wrap words to the next line automatically") runCmd.Flags().String("format", "", "Response format (e.g. json)") - serveCmd := &cobra.Command{ Use: "serve", Aliases: []string{"start"}, @@ -963,20 +970,9 @@ func NewCLI() *cobra.Command { serveCmd.SetUsageTemplate(serveCmd.UsageTemplate() + ` Environment Variables: - OLLAMA_HOST - The host:port to bind to (default "127.0.0.1:11434") - - Examples: - "127.0.0.1:11434" - OLLAMA_ORIGINS - A comma separated list of allowed origins. If unset, the - default behavior is to allow same origin requests, only. - - Examples: - "localhost:11434" - "example.com,api.example.com" - OLLAMA_MODELS - The path to the models directory (default is "~/.ollama/models") + OLLAMA_HOST The host:port to bind to (default "127.0.0.1:11434") + OLLAMA_ORIGINS A comma separated list of allowed origins. + OLLAMA_MODELS The path to the models directory (default is "~/.ollama/models") `) pullCmd := &cobra.Command{ @@ -1022,6 +1018,19 @@ Environment Variables: RunE: DeleteHandler, } + for _, cmd := range []*cobra.Command{ + createCmd, + showCmd, + runCmd, + pullCmd, + pushCmd, + listCmd, + copyCmd, + deleteCmd, + } { + appendHostEnvDocs(cmd) + } + rootCmd.AddCommand( serveCmd, createCmd,