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] update default config #614

Merged
merged 2 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
1 change: 0 additions & 1 deletion api/client/amp.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type Configuration struct {
Verbose bool
Github string
Target string
Images []string
Port string
ServerAddress string
AdminServerAddress string
Expand Down
32 changes: 0 additions & 32 deletions cmd/amp/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ package cli

import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/fatih/color"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)

var (
Expand All @@ -35,31 +31,3 @@ func PrintErr(err error) {
fmt.Println(err)
os.Exit(1)
}

// 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()
if err != nil {
return err
}
configdir = path.Join(homedir, ".config/amp")
}
err = os.MkdirAll(configdir, 0755)
if err != nil {
return
}
contents, err := yaml.Marshal(c)
if err != nil {
return
}
err = ioutil.WriteFile(path.Join(configdir, "amp.yaml"), contents, os.ModePerm)
if err != nil {
return
}
return
}
33 changes: 25 additions & 8 deletions cmd/amp/confighelper.go → cmd/amp/cli/confighelper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main
package cli

import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/appcelerator/amp/api/client"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

// InitConfig reads in a config file and ENV variables if set.
Expand Down Expand Up @@ -62,15 +64,30 @@ func InitConfig(configFile string, config *client.Configuration, verbose bool, s
}
}

// LoadImageList loads all the images in the config.
func LoadImageList() (images []string, err error) {
viper.SetConfigName("images")
viper.AddConfigPath("config")
err = viper.ReadInConfig()
// 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()
if err != nil {
return err
}
configdir = path.Join(homedir, ".config/amp")
}
err = os.MkdirAll(configdir, 0755)
if err != nil {
return
}
contents, err := yaml.Marshal(c)
if err != nil {
return
}
err = ioutil.WriteFile(path.Join(configdir, "amp.yaml"), contents, os.ModePerm)
if err != nil {
return
}

images = viper.GetStringSlice("images")
return
}
2 changes: 1 addition & 1 deletion cmd/amp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
// All main does is process commands and flags and invoke the app
func main() {
cobra.OnInitialize(func() {
InitConfig(configFile, Config, verbose, serverAddr)
cli.InitConfig(configFile, Config, verbose, serverAddr)
if addr := RootCmd.Flag("server").Value.String(); addr != "" {
Config.ServerAddress = addr
}
Expand Down