Skip to content

Commit

Permalink
Remove config file for now
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hudelmaier committed Jan 18, 2024
1 parent bd6081e commit 51d9be6
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 180 deletions.
54 changes: 0 additions & 54 deletions checkson-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,6 @@ _checkson_channels_create()
two_word_flags+=("--webhook-url")
two_word_flags+=("-w")
local_nonpersistent_flags+=("--webhook-url=")
flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -413,9 +410,6 @@ _checkson_channels_delete()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -439,9 +433,6 @@ _checkson_channels_list()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -465,9 +456,6 @@ _checkson_channels_show()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand Down Expand Up @@ -495,9 +483,6 @@ _checkson_channels()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -524,9 +509,6 @@ _checkson_completion()
flags+=("--help")
flags+=("-h")
local_nonpersistent_flags+=("--help")
flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand Down Expand Up @@ -582,9 +564,6 @@ _checkson_create()
two_word_flags+=("--webhook-url")
two_word_flags+=("-w")
local_nonpersistent_flags+=("--webhook-url=")
flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -608,9 +587,6 @@ _checkson_delete()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -634,9 +610,6 @@ _checkson_help()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -661,9 +634,6 @@ _checkson_list()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand Down Expand Up @@ -691,9 +661,6 @@ _checkson_login()
two_word_flags+=("--personal-access-token")
two_word_flags+=("-t")
local_nonpersistent_flags+=("--personal-access-token=")
flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -717,9 +684,6 @@ _checkson_logout()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -743,9 +707,6 @@ _checkson_logs()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -769,9 +730,6 @@ _checkson_runs()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -795,9 +753,6 @@ _checkson_show()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -821,9 +776,6 @@ _checkson_status()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand All @@ -847,9 +799,6 @@ _checkson_version()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand Down Expand Up @@ -886,9 +835,6 @@ _checkson_root_command()
flags_with_completion=()
flags_completion=()

flags+=("--config-file=")
two_word_flags+=("--config-file")
two_word_flags+=("-C")
flags+=("--dev-mode")
flags+=("--verbose")
flags+=("-V")
Expand Down
124 changes: 0 additions & 124 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package cmd

import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stefan-hudelmaier/checkson-cli/output"
"os"
"path/filepath"
"strings"
)

var cfgFile string
var Verbose bool
var DevMode bool

var envMapping = map[string]string{
"REQUESTTIMEOUT": "CONTEXTS_DEFAULT_REQUESTTIMEOUT",
}

var configPaths = []string{"$HOME/.config/checkson", "$HOME/.checkson", "$SNAP_REAL_HOME/.config/checkson", "$SNAP_DATA/checkson", "/etc/checkson"}

func NewChecksonCommand(streams output.IOStreams) *cobra.Command {

var rootCmd = &cobra.Command{
Expand All @@ -29,8 +16,6 @@ func NewChecksonCommand(streams output.IOStreams) *cobra.Command {
Long: `A command-line interface for managing Checkson checks`,
}

cobra.OnInitialize(initConfig)

rootCmd.AddCommand(newCompletionCmd())
rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newListChecksCmd())
Expand All @@ -44,8 +29,6 @@ func NewChecksonCommand(streams output.IOStreams) *cobra.Command {
rootCmd.AddCommand(newShowCheckCmd())
rootCmd.AddCommand(newChannelCmd())

// use upper-case letters for shorthand params to avoid conflicts with local flags
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config-file", "C", "", fmt.Sprintf("config file. one of: %v", configPaths))
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "V", false, "verbose output")
rootCmd.PersistentFlags().Bool("dev-mode", false, "enable dev mode, communicating to local services")

Expand All @@ -54,110 +37,3 @@ func NewChecksonCommand(streams output.IOStreams) *cobra.Command {
rootCmd.SetErr(streams.ErrOut)
return rootCmd
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {

viper.Reset()

if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else if os.Getenv("CHECKSON_CONFIG") != "" {
viper.SetConfigFile(os.Getenv("CHECKSON_CONFIG"))
} else {
for _, path := range configPaths {
viper.AddConfigPath(os.ExpandEnv(path))
}
viper.SetConfigName("config")
}

if Verbose {
output.IoStreams.EnableDebug()
}

if Verbose && os.Getenv("SNAP_NAME") != "" {
output.Debugf("Running snap version %s on %s", os.Getenv("SNAP_VERSION"), os.Getenv("SNAP_ARCH"))
}

mapEnvVariables()

replacer := strings.NewReplacer("-", "_", ".", "_")
viper.SetEnvKeyReplacer(replacer)

viper.SetDefault("contexts.default.brokers", []string{"localhost:9092"})
viper.SetDefault("current-context", "default")

viper.SetConfigType("yml")
viper.AutomaticEnv() // read in environment variables that match

if err := readConfig(); err != nil {
output.Fail(err)
}
}

func mapEnvVariables() {
for short, long := range envMapping {
if os.Getenv(short) != "" && os.Getenv(long) == "" {
_ = os.Setenv(long, os.Getenv(short))
}
}
}

func readConfig() error {
var err error
if err = viper.ReadInConfig(); err == nil {
output.Debugf("Using config file: %s", viper.ConfigFileUsed())
return nil
}

_, isConfigFileNotFoundError := err.(viper.ConfigFileNotFoundError)
_, isOsPathError := err.(*os.PathError)

if !isConfigFileNotFoundError && !isOsPathError {
return errors.Errorf("Error reading config file: %s (%v)", viper.ConfigFileUsed(), err)
} else {
err = generateDefaultConfig()
if err != nil {
return errors.Wrap(err, "Error generating default config: ")
}
}

// We read generated config now
if err = viper.ReadInConfig(); err == nil {
output.Debugf("Using config file: %s", viper.ConfigFileUsed())
return nil
} else {
return errors.Errorf("Error reading config file: %s (%v)", viper.ConfigFileUsed(), err)
}
}

// generateDefaultConfig generates default config in case there is no config
func generateDefaultConfig() error {

cfgFile := filepath.Join(os.ExpandEnv(configPaths[0]), "config.yml")

if os.Getenv("CHECKSON_CONFIG") != "" {
// use config file provided via env
cfgFile = os.Getenv("CHECKSON_CONFIG")
} else if os.Getenv("SNAP_REAL_HOME") != "" {
// use different configFile when running in snap
for _, configPath := range configPaths {
if strings.Contains(configPath, "$SNAP_REAL_HOME") {
cfgFile = filepath.Join(os.ExpandEnv(configPath), "config.yml")
break
}
}
}

if err := os.MkdirAll(filepath.Dir(cfgFile), os.FileMode(0700)); err != nil {
return err
}

if err := viper.WriteConfigAs(cfgFile); err != nil {
return err
}

output.Debugf("generated default config at %s", cfgFile)
return nil
}
3 changes: 1 addition & 2 deletions generate_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ fi
if [ "$ARCH" == "$TARGET" ]; then
echo "generating completions... ${BIN_PATH}"

echo "" > /tmp/empty.yaml
${BIN_PATH} completion bash > ${SCRIPT_PATH}/checkson-completion.bash --config-file=/tmp/empty.yaml
${BIN_PATH} completion bash > ${SCRIPT_PATH}/checkson-completion.bash
else
echo "not generating: $ARCH $TARGET"
fi

0 comments on commit 51d9be6

Please sign in to comment.