Updates color flag to work as a global flag (#143)

This commit is contained in:
Jay Gabriels
2021-08-14 14:23:21 -07:00
committed by GitHub
parent f0fb868bea
commit 805b9266e7
4 changed files with 36 additions and 22 deletions

View File

@@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
- users/orgs directory no longer appends "\_ghorg" or forces underscores
- make $HOME/.config/ghorg/conf.yaml optional
- color is off by default
- color flag configuration options are enabled/disabled
### Deprecated
### Removed
### Fixed

View File

@@ -28,16 +28,6 @@ var cloneCmd = &cobra.Command{
func cloneFunc(cmd *cobra.Command, argz []string) {
if cmd.Flags().Changed("color") {
colorToggle := cmd.Flag("color").Value.String()
if colorToggle == "on" {
os.Setenv("GHORG_COLOR", colorToggle)
} else {
os.Setenv("GHORG_COLOR", "off")
}
}
if cmd.Flags().Changed("path") {
absolutePath := configs.EnsureTrailingSlash((cmd.Flag("path").Value.String()))
os.Setenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO", absolutePath)

View File

@@ -47,8 +47,26 @@ var rootCmd = &cobra.Command{
},
}
// reads in configuration file and updates anything not set to default
func getOrSetDefaults(envVar string) {
if envVar == "GHORG_COLOR" {
if color == "enabled" {
os.Setenv("GHORG_COLOR", "enabled")
return
}
if color == "disabled" {
os.Setenv("GHORG_COLOR", "disabled")
return
}
if viper.GetString(envVar) == "enabled" {
os.Setenv("GHORG_COLOR", "enabled")
return
}
}
// When a user does not set value in $HOME/.config/ghorg/conf.yaml set the default values, else set env to what they have added to the file.
if viper.GetString(envVar) == "" {
switch envVar {
@@ -66,6 +84,8 @@ func getOrSetDefaults(envVar string) {
os.Setenv(envVar, "false")
case "GHORG_BACKUP":
os.Setenv(envVar, "false")
case "GHORG_COLOR":
os.Setenv(envVar, "disabled")
case "GHORG_PRESERVE_DIRECTORY_STRUCTURE":
os.Setenv(envVar, "false")
case "GHORG_CONCURRENCY":
@@ -73,7 +93,6 @@ func getOrSetDefaults(envVar string) {
}
} else {
s := viper.GetString(envVar)
// envs that need a trailing slash
if envVar == "GHORG_SCM_BASE_URL" || envVar == "GHORG_ABSOLUTE_PATH_TO_CLONE_TO" {
os.Setenv(envVar, configs.EnsureTrailingSlash(s))
@@ -81,6 +100,10 @@ func getOrSetDefaults(envVar string) {
os.Setenv(envVar, s)
}
}
if os.Getenv("GHORG_DEBUG") != "" {
fmt.Printf("%s: %s\n", envVar, os.Getenv(envVar))
}
}
func InitConfig() {
@@ -96,10 +119,6 @@ func InitConfig() {
}
if viper.GetString("color") == "on" {
os.Setenv("GHORG_COLOR", "on")
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
os.Setenv("GHORG_CONF", "none")
@@ -109,6 +128,10 @@ func InitConfig() {
}
}
if os.Getenv("GHORG_DEBUG") != "" {
fmt.Println("-------- Setting Default ENV values ---------")
}
getOrSetDefaults("GHORG_ABSOLUTE_PATH_TO_CLONE_TO")
getOrSetDefaults("GHORG_BRANCH")
getOrSetDefaults("GHORG_CLONE_PROTOCOL")
@@ -121,6 +144,7 @@ func InitConfig() {
getOrSetDefaults("GHORG_MATCH_PREFIX")
// Optionally set
getOrSetDefaults("GHORG_GITHUB_TOKEN")
getOrSetDefaults("GHORG_COLOR")
getOrSetDefaults("GHORG_TOPICS")
getOrSetDefaults("GHORG_GITLAB_TOKEN")
getOrSetDefaults("GHORG_BITBUCKET_USERNAME")
@@ -141,11 +165,10 @@ func InitConfig() {
func init() {
cobra.OnInitialize(InitConfig)
rootCmd.PersistentFlags().StringVarP(&color, "color", "", "off", "GHORG_COLOR - toggles colorful output")
rootCmd.PersistentFlags().StringVarP(&config, "config", "", "", "manually set the path to your config file")
rootCmd.PersistentFlags().StringVar(&color, "color", "", "GHORG_COLOR - toggles colorful output (default: disabled)")
rootCmd.PersistentFlags().StringVar(&config, "config", "", "manually set the path to your config file")
viper.SetDefault("config", configs.DefaultConfFile())
viper.SetDefault("color", "off")
_ = viper.BindPFlag("color", rootCmd.PersistentFlags().Lookup("color"))
_ = viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))

View File

@@ -11,7 +11,7 @@ import (
// PrintInfo prints yellow colored text to standard out
func PrintInfo(msg interface{}) {
switch os.Getenv("GHORG_COLOR") {
case "on":
case "enabled":
color.New(color.FgYellow).Println(msg)
default:
fmt.Println(msg)
@@ -21,7 +21,7 @@ func PrintInfo(msg interface{}) {
// PrintSuccess prints green colored text to standard out
func PrintSuccess(msg interface{}) {
switch os.Getenv("GHORG_COLOR") {
case "on":
case "enabled":
color.New(color.FgGreen).Println(msg)
default:
fmt.Println(msg)
@@ -31,7 +31,7 @@ func PrintSuccess(msg interface{}) {
// PrintError prints red colored text to standard out
func PrintError(msg interface{}) {
switch os.Getenv("GHORG_COLOR") {
case "on":
case "enabled":
color.New(color.FgRed).Println(msg)
default:
fmt.Println(msg)
@@ -41,7 +41,7 @@ func PrintError(msg interface{}) {
// PrintSubtleInfo prints magenta colored text to standard out
func PrintSubtleInfo(msg interface{}) {
switch os.Getenv("GHORG_COLOR") {
case "on":
case "enabled":
color.New(color.FgHiMagenta).Println(msg)
default:
fmt.Println(msg)