Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] config and cleanup #617

Merged
merged 7 commits into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions api/client/amp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
66 changes: 41 additions & 25 deletions cmd/amp/cli/confighelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}

}
}

Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/amp/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Login(a *client.AMP) error {
}
}

a.Configuration.Github = lastEight
a.Configuration.GitHub = lastEight
cli.SaveConfiguration(a.Configuration)

welcomeUser(name)
Expand Down
4 changes: 2 additions & 2 deletions cmd/amp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/amp/platform_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down