-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Show terminal errors without -v flag. Closes #6 * Started configure command * Add configure command. Closes #7 * Update README * update readme * Allow short name as an option
- Loading branch information
1 parent
d3b4946
commit f799685
Showing
3 changed files
with
114 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var natsURL string | ||
var natsClusterID string | ||
var useShortName bool | ||
var forceWrite bool | ||
|
||
func init() { | ||
rootCmd.AddCommand(configureCmd) | ||
configureCmd.PersistentFlags().StringVar(&natsURL, "nats-url", "", "NATS server url") | ||
configureCmd.PersistentFlags().StringVar(&natsClusterID, "nats-cluster", "", "NATS cluster id") | ||
configureCmd.PersistentFlags().BoolVar(&useShortName, "short-name", false, "Use short channel names. Channel conflicts may occur.") | ||
configureCmd.PersistentFlags().BoolVar(&forceWrite, "overwrite", false, "Overwrite current configuration") | ||
} | ||
|
||
var configureCmd = &cobra.Command{ | ||
Use: "configure", | ||
Short: "Configure Convey", | ||
Run: ConfigureCommandFunc, | ||
} | ||
|
||
// ConfigureCommandFunc is a handler for the configure command | ||
func ConfigureCommandFunc(cmd *cobra.Command, args []string) { | ||
viper.Set(configKeyNatsURL, natsURL) | ||
viper.Set(configKeyNatsClusterID, natsClusterID) | ||
viper.Set(configKeyUseShortName, useShortName) | ||
|
||
// If a config file is found, read it in. | ||
configFileExists := false | ||
if err := viper.ReadInConfig(); err == nil { | ||
configFileExists = true | ||
} | ||
|
||
// If config file doesn't exist and it hasn't been set in viper, set it | ||
if !configFileExists && viper.ConfigFileUsed() == "" { | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
errorExit(err.Error()) | ||
} | ||
viper.SetConfigFile(path.Join(home, ".convey.yaml")) | ||
} | ||
|
||
configFilePath := viper.ConfigFileUsed() | ||
|
||
if forceWrite || !configFileExists { | ||
err := viper.WriteConfigAs(configFilePath) | ||
if err != nil { | ||
errorExit(err.Error()) | ||
} | ||
} else { | ||
msg := fmt.Sprintf("Config file exists. Use --overwrite to overwrite the config file at %s", configFilePath) | ||
errorExit(msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters