diff --git a/Makefile b/Makefile index f35a27dc3..09c07a51f 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ + .PHONY: all clean proto-clean bin-clean install install-host fmt simplify check version build-image run proto proto-host rules .PHONY: build build-cli-linux build-cli-darwin build-cli-windows build-server build-server-linux build-server-darwin build-server-windows .PHONY: dist dist-linux dist-darwin dist-windows @@ -87,12 +88,12 @@ version: install-deps: @$(GLIDE_INSTALL) - # temporary fix to trace conflict +# temporary fix to trace conflict @rm -rf vendor/github.com/docker/docker/vendor/golang.org/x/net/trace update-deps: @$(GLIDE_UPDATE) - # temporary fix to trace conflict +# temporary fix to trace conflict @rm -rf vendor/github.com/docker/docker/vendor/golang.org/x/net/trace # explicit rule to compile protobuf files diff --git a/api/client/amp.go b/api/client/amp.go index 7aa28a055..fce979e33 100644 --- a/api/client/amp.go +++ b/api/client/amp.go @@ -69,7 +69,7 @@ func (l logger) Println(args ...interface{}) { // Configuration is for all configurable client settings type Configuration struct { Verbose bool - Github string + GitHub string Target string Port string ServerAddress string @@ -120,7 +120,7 @@ func (a *AMP) GetAuthorizedContext() (ctx context.Context, err error) { // if a.Configuration.Github == "" { // return nil, fmt.Errorf("Requires login") // } - md := metadata.Pairs("sessionkey", a.Configuration.Github) + md := metadata.Pairs("sessionkey", a.Configuration.GitHub) ctx = metadata.NewContext(context.Background(), md) return } diff --git a/cmd/amp/cli/confighelper.go b/cmd/amp/cli/confighelper.go index 12d74ca07..6d7a63dd3 100644 --- a/cmd/amp/cli/confighelper.go +++ b/cmd/amp/cli/confighelper.go @@ -18,23 +18,28 @@ func InitConfig(configFile string, config *client.Configuration, verbose bool, s config.Verbose = verbose config.ServerAddress = serverAddr - // Add matching environment variables - will be first in precedence. + // Add matching environment variables - will take precedence over config files. viper.AutomaticEnv() - // Add config file specified using flag - will be next in precedence. - if configFile != "" { - viper.SetConfigFile(configFile) - } - - // Add default config file (without extension) - will be last in precedence. - // First search .config/amp directory; if not found, then attempt to also search working - // directory (will only succeed if process was started from application directory). + // Add default config file search paths in order of decreasing precedence. viper.SetConfigName("amp") + viper.AddConfigPath(".") if os.Getenv("XDG_CONFIG_HOME") != "" { viper.AddConfigPath("$XDG_CONFIG_HOME/amp") + } else { + homedir, err := homedir.Dir() + if err != nil { + return + } + viper.AddConfigPath(path.Join(homedir, ".config/amp")) + } + // last place to look: system dir on *nix + viper.AddConfigPath("/etc/amp/") + + // this must be last: config file specified using --use-config option will take precedence over other paths. + if configFile != "" { + viper.SetConfigFile(configFile) } - viper.AddConfigPath("$HOME/.config/amp") - viper.AddConfigPath(".") // If a config file is found, read it in. // Extra check for verbose because it might not have been set by @@ -45,7 +50,12 @@ func InitConfig(configFile string, config *client.Configuration, verbose bool, s } } else { if verbose || viper.GetBool("Verbose") { - fmt.Println("Warning: no valid configuration file (amp.yaml) found in ~/.config/amp/ or current directory") + if configFile != "" { + fmt.Printf("Warning: unable to load %s, using default configuration\n", configFile) + } else { + fmt.Println("Warning: no valid configuration file (amp.yaml) found in ~/.config/amp/ or current directory") + } + } } @@ -66,26 +76,32 @@ func InitConfig(configFile string, config *client.Configuration, verbose bool, s // SaveConfiguration saves the configuration to ~/.config/amp/amp.yaml func SaveConfiguration(c interface{}) (err error) { - var configdir string - xdgdir := os.Getenv("XDG_CONFIG_HOME") - if xdgdir != "" { - configdir = path.Join(xdgdir, "amp") - } else { - homedir, err := homedir.Dir() + configFilePath := viper.ConfigFileUsed() + + if configFilePath == "" { + var configdir string + xdgdir := os.Getenv("XDG_CONFIG_HOME") + if xdgdir != "" { + configdir = path.Join(xdgdir, "amp") + } else { + homedir, err := homedir.Dir() + if err != nil { + return err + } + configdir = path.Join(homedir, ".config/amp") + } + err = os.MkdirAll(configdir, 0755) if err != nil { - return err + return } - configdir = path.Join(homedir, ".config/amp") - } - err = os.MkdirAll(configdir, 0755) - if err != nil { - return + configFilePath = path.Join(configdir, "amp.yaml") } + contents, err := yaml.Marshal(c) if err != nil { return } - err = ioutil.WriteFile(path.Join(configdir, "amp.yaml"), contents, os.ModePerm) + err = ioutil.WriteFile(configFilePath, contents, os.ModePerm) if err != nil { return } diff --git a/cmd/amp/login.go b/cmd/amp/login.go index 822a28518..4ad64f9fb 100644 --- a/cmd/amp/login.go +++ b/cmd/amp/login.go @@ -41,7 +41,7 @@ func Login(a *client.AMP) error { } } - a.Configuration.Github = lastEight + a.Configuration.GitHub = lastEight cli.SaveConfiguration(a.Configuration) welcomeUser(name) diff --git a/cmd/amp/main.go b/cmd/amp/main.go index 1b9375f04..8381937b1 100644 --- a/cmd/amp/main.go +++ b/cmd/amp/main.go @@ -92,8 +92,8 @@ func main() { RootCmd.SetUsageTemplate(usageTemplate) RootCmd.SetHelpTemplate(helpTemplate) - RootCmd.PersistentFlags().StringVar(&configFile, "config", "", "Config file (default is $HOME/.config/amp/amp.yaml)") - RootCmd.PersistentFlags().BoolVar(&displayConfigFilePath, "config-file", false, "Display the location of the config file used (if any)") + RootCmd.PersistentFlags().StringVar(&configFile, "use-config", "", "Specify config file (overrides default at $HOME/.config/amp/amp.yaml)") + RootCmd.PersistentFlags().BoolVar(&displayConfigFilePath, "config-used", false, "Display config file used (if any)") RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output") RootCmd.PersistentFlags().StringVar(&serverAddr, "server", "", "Server address") RootCmd.PersistentFlags().BoolVarP(&listVersion, "version", "V", false, "Version number") diff --git a/cmd/amp/platform_start.go b/cmd/amp/platform_start.go index dca86b7f7..588d37899 100644 --- a/cmd/amp/platform_start.go +++ b/cmd/amp/platform_start.go @@ -18,7 +18,7 @@ var PlatformStart = &cobra.Command{ func init() { PlatformStart.Flags().BoolP("force", "f", false, "Start all possible services, do not stop on error") - PlatformStart.Flags().BoolP("quiet", "q", false, "Suppress terminal outpu") + PlatformStart.Flags().BoolP("quiet", "q", false, "Suppress terminal output") PlatformStart.Flags().BoolP("local", "l", false, "Use local amp image") PlatformCmd.AddCommand(PlatformStart) }