Skip to content

Commit

Permalink
Merge pull request #95 from amazeeio/config-rework
Browse files Browse the repository at this point in the history
Config defined by flag or environment variable
  • Loading branch information
shreddedbacon authored Apr 2, 2020
2 parents 22ff662 + 7be64e4 commit 64f41f2
Show file tree
Hide file tree
Showing 78 changed files with 175 additions and 32 deletions.
8 changes: 5 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"

Expand Down Expand Up @@ -58,7 +59,7 @@ var configDefaultCmd = &cobra.Command{
os.Exit(1)
}
viper.Set("default", strings.TrimSpace(string(lagoonConfig.Lagoon)))
err := viper.WriteConfig()
err := viper.WriteConfigAs(filepath.Join(configFilePath, configName+configExtension))
handleError(err)

resultData := output.Result{
Expand Down Expand Up @@ -140,7 +141,7 @@ var configAddCmd = &cobra.Command{
if lagoonConfig.Token != "" {
viper.Set("lagoons."+lagoonConfig.Lagoon+".token", lagoonConfig.Token)
}
err := viper.WriteConfig()
err := viper.WriteConfigAs(filepath.Join(configFilePath, configName+configExtension))
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
Expand Down Expand Up @@ -196,7 +197,7 @@ var configFeatureSwitch = &cobra.Command{
case "false":
viper.Set("updateCheckDisable", false)
}
err := viper.WriteConfig()
err := viper.WriteConfigAs(filepath.Join(configFilePath, configName+configExtension))
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
Expand Down Expand Up @@ -227,6 +228,7 @@ func init() {
configAddCmd.Flags().StringVarP(&lagoonGraphQL, "graphql", "g", "", "Lagoon GraphQL endpoint")
configAddCmd.Flags().StringVarP(&lagoonToken, "token", "t", "", "Lagoon GraphQL token")
configAddCmd.Flags().StringVarP(&lagoonUI, "ui", "u", "", "Lagoon UI location (https://ui-lagoon-master.ch.amazee.io)")
configAddCmd.PersistentFlags().BoolVarP(&createConfig, "create-config", "", false, "Create the config file if it is non existent (to be used with --config-file)")
configAddCmd.Flags().StringVarP(&lagoonKibana, "kibana", "k", "", "Lagoon Kibana URL (https://logs-db-ui-lagoon-master.ch.amazee.io)")
configFeatureSwitch.Flags().StringVarP(&updateCheck, "disable-update-check", "", "", "Enable or disable checking of updates (true/false)")
}
7 changes: 1 addition & 6 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ func publicKey(path string, skipAgent bool) (ssh.AuthMethod, func() error) {
}

func loginToken() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("couldn't get $HOME: %v", err)
}
skipAgent := false

privateKey := fmt.Sprintf("%s/.ssh/id_rsa", homeDir)
privateKey := fmt.Sprintf("%s/.ssh/id_rsa", userPath)
if cmdSSHKey != "" {
privateKey = cmdSSHKey
skipAgent = true
Expand Down
48 changes: 31 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/amazeeio/lagoon-cli/internal/helpers"
"github.com/amazeeio/lagoon-cli/pkg/app"
"github.com/amazeeio/lagoon-cli/pkg/graphql"
"github.com/amazeeio/lagoon-cli/pkg/lagoon/environments"
Expand All @@ -31,6 +32,10 @@ var versionFlag bool
var docsFlag bool
var updateInterval = time.Hour * 24 * 7 // One week interval between updates
var configName = ".lagoon"
var configExtension = ".yml"
var createConfig bool
var userPath string
var configFilePath string
var updateDocURL = "https://amazeeio.github.io/lagoon-cli"

var skipUpdateCheck bool
Expand All @@ -52,12 +57,7 @@ var rootCmd = &cobra.Command{
}
if skipUpdateCheck == false {
// Using code from https://github.com/drud/ddev/
home, err := os.UserHomeDir()
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
updateFile := filepath.Join(home, configName+".update")
updateFile := filepath.Join(userPath, ".lagoon.update")
// Do periodic detection of whether an update is available for lagoon-cli users.
timeToCheckForUpdates, err := updatecheck.IsUpdateNeeded(updateFile, updateInterval)
if err != nil {
Expand Down Expand Up @@ -132,6 +132,9 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&debugEnable, "debug", "", false, "Enable debugging output (if supported)")
rootCmd.PersistentFlags().BoolVarP(&skipUpdateCheck, "skip-update-check", "", false, "Skip checking for updates")

// get config-file from flag
rootCmd.PersistentFlags().StringP("config-file", "", "", "Path to the config file to use (must be *.yml or *.yaml)")

rootCmd.Flags().BoolVarP(&versionFlag, "version", "", false, "Version information")
rootCmd.Flags().BoolVarP(&docsFlag, "docs", "", false, "Generate docs")

Expand Down Expand Up @@ -198,27 +201,38 @@ func displayVersionInfo() {
}

func initConfig() {
var err error
// Find home directory.
home, err := os.UserHomeDir()
userPath, err = os.UserHomeDir()
if err != nil {
output.RenderError(fmt.Errorf("couldn't get $HOME: %v", err).Error(), outputOptions)
os.Exit(1)
}
configFilePath := userPath

// check if we are being given a path to a different config file
err = helpers.GetLagoonConfigFile(&configFilePath, &configName, &configExtension, createConfig, rootCmd)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}

// Search config in home directory with name ".lagoon" (without extension).
// Search config in userPath directory with default name ".lagoon" (without extension).
// @todo see if we can grok the proper info from the cwd .lagoon.yml
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.AddConfigPath(configFilePath)
viper.SetConfigName(configName)
viper.SetDefault("lagoons.amazeeio.hostname", "ssh.lagoon.amazeeio.cloud")
viper.SetDefault("lagoons.amazeeio.port", 32222)
viper.SetDefault("lagoons.amazeeio.token", "")
viper.SetDefault("lagoons.amazeeio.graphql", "https://api.lagoon.amazeeio.cloud/graphql")
viper.SetDefault("lagoons.amazeeio.ui", "https://ui-lagoon-master.ch.amazee.io")
viper.SetDefault("lagoons.amazeeio.kibana", "https://logs-db-ui-lagoon-master.ch.amazee.io/")
viper.SetDefault("default", "amazeeio")
err = viper.ReadInConfig()
if err != nil {
err = viper.WriteConfigAs(filepath.Join(home, configName+".yml"))
// if we can't read the file cause it doesn't exist, then we should set the default configuration options and try create it
viper.SetDefault("lagoons.amazeeio.hostname", "ssh.lagoon.amazeeio.cloud")
viper.SetDefault("lagoons.amazeeio.port", 32222)
viper.SetDefault("lagoons.amazeeio.token", "")
viper.SetDefault("lagoons.amazeeio.graphql", "https://api.lagoon.amazeeio.cloud/graphql")
viper.SetDefault("lagoons.amazeeio.ui", "https://ui-lagoon-master.ch.amazee.io")
viper.SetDefault("lagoons.amazeeio.kibana", "https://logs-db-ui-lagoon-master.ch.amazee.io/")
viper.SetDefault("default", "amazeeio")
err = viper.WriteConfigAs(filepath.Join(configFilePath, configName+configExtension))
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
Expand Down
3 changes: 1 addition & 2 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ var sshEnvCmd = &cobra.Command{
fmt.Println(lagoonssh.GenerateSSHConnectionString(sshConfig, sshService, sshContainer))
} else {
// get private key that the cli is using
homeDir, _ := os.UserHomeDir()
skipAgent := false

privateKey := fmt.Sprintf("%s/.ssh/id_rsa", homeDir)
privateKey := fmt.Sprintf("%s/.ssh/id_rsa", userPath)
if cmdSSHKey != "" {
privateKey = cmdSSHKey
skipAgent = true
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lagoon [flags]
### Options

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Add a project, or add notifications and variables to projects or environments
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lagoon add group [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_project-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lagoon add project-group [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_project-rocketchat.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lagoon add project-rocketchat [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_project-slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lagoon add project-slack [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ lagoon add project [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_rocketchat.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ lagoon add rocketchat [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ lagoon add slack [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_user-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lagoon add user-group [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_user-sshkey.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lagoon add user-sshkey [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ lagoon add user [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_add_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ lagoon add variable [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Configure Lagoon CLI
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
2 changes: 2 additions & 0 deletions docs/commands/lagoon_config_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lagoon config add [flags]
### Options

```
--create-config Create the config file if it is non existent (to be used with --config-file)
-g, --graphql string Lagoon GraphQL endpoint
-h, --help help for add
-H, --hostname string Lagoon SSH hostname
Expand All @@ -25,6 +26,7 @@ lagoon config add [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_config_current.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lagoon config current [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_config_default.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lagoon config default [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_config_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lagoon config delete [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_config_feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lagoon config feature [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_config_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lagoon config list [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Delete a project, or delete notifications and variables from projects or environ
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete_environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lagoon delete environment [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lagoon delete group [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete_project-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lagoon delete project-group [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete_project-rocketchat.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lagoon delete project-rocketchat [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete_project-slack.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lagoon delete project-slack [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lagoon delete project [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_delete_rocketchat.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lagoon delete rocketchat [flags]
### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
Expand Down
Loading

0 comments on commit 64f41f2

Please sign in to comment.