Skip to content
Open
Show file tree
Hide file tree
Changes from 10 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
90 changes: 63 additions & 27 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"

"github.com/kitops-ml/kitops/pkg/cmd/config"
"github.com/kitops-ml/kitops/pkg/cmd/dev"
"github.com/kitops-ml/kitops/pkg/cmd/diff"
"github.com/kitops-ml/kitops/pkg/cmd/info"
Expand Down Expand Up @@ -71,35 +72,51 @@ func RunCommand() *cobra.Command {
Short: shortDesc,
Long: longDesc,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
output.SetOut(cmd.OutOrStdout())
output.SetErr(cmd.ErrOrStderr())
if err := output.SetLogLevelFromString(opts.loglevel); err != nil {
return output.Fatalln(err)
}
output.SetProgressBars(opts.progressBars)

switch opts.verbosity {
case 0:
break
case 1:
output.Debugf("Setting verbosity to %s", output.LogLevelDebug)
output.SetLogLevel(output.LogLevelDebug)
case 2:
output.Debugf("Setting verbosity to %s", output.LogLevelTrace)
output.SetLogLevel(output.LogLevelTrace)
default:
output.Debugf("Setting verbosity to %s and disabling progress bars", output.LogLevelTrace)
output.SetLogLevel(output.LogLevelTrace)
output.SetProgressBars("none")
}

configHome, err := getConfigHome(opts)

configHome, err := getConfigHomePath(opts)
if err != nil {
output.Errorf("Failed to read base config directory")
output.Infof("Use the --config flag or set the $%s environment variable to provide a default", constants.KitopsHomeEnvVar)
output.Debugf("Error: %s", err)
return errors.New("exit")
}

configYamlPath := constants.ConfigYamlPath(configHome)
configStruct, loadErr := config.LoadConfigFileHelper(configYamlPath)
if loadErr != nil && !errors.Is(loadErr, os.ErrNotExist) {
return loadErr
}

output.SetOut(cmd.OutOrStdout())
output.SetErr(cmd.ErrOrStderr())
Comment thread
omarbassem128 marked this conversation as resolved.
Outdated

if cmd.Flags().Changed("log-level") || configStruct.LogLevel == "" {
if err := output.SetLogLevelFromString(opts.loglevel); err != nil {
return output.Fatalln(err)
}
} else {
if err := output.SetLogLevelFromString(configStruct.LogLevel); err != nil {
output.Errorf("Invalid log level: %s", err)
output.SetLogLevelFromString(opts.loglevel)
}
}

if cmd.Flags().Changed("progress") || configStruct.ProgressBars == "" {
output.SetProgressBars(opts.progressBars)
} else {
output.SetProgressBars(configStruct.ProgressBars)
}

if cmd.Flags().Changed("verbose") || configStruct.Verbosity == 0 {
setVerbosity(opts.verbosity)
} else {
setVerbosity(configStruct.Verbosity)
}

if !cmd.Flags().Changed("config") && configStruct.ConfigHome != "" && os.Getenv(constants.KitopsHomeEnvVar) == "" {
configHome = configStruct.ConfigHome
}

ctx := context.WithValue(cmd.Context(), constants.ConfigKey{}, configHome)
cache.SetCacheHome(constants.CachePath(configHome))
cmd.SetContext(ctx)
Expand Down Expand Up @@ -167,6 +184,7 @@ func addSubcommands(rootCmd *cobra.Command) {
rootCmd.AddCommand(diff.DiffCommand())
rootCmd.AddCommand(kitimport.ImportCommand())
rootCmd.AddCommand(kitcache.CacheCommand())
rootCmd.AddCommand(config.ConfigCommand())
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -178,7 +196,7 @@ func Execute() {
}
}

func getConfigHome(opts *rootOptions) (string, error) {
func getConfigHomePath(opts *rootOptions) (string, error) {
if opts.configHome != "" {
output.Debugf("Using config directory from flag: %s", opts.configHome)
absHome, err := filepath.Abs(opts.configHome)
Expand All @@ -198,10 +216,28 @@ func getConfigHome(opts *rootOptions) (string, error) {
return absHome, nil
}

defaultHome, err := constants.DefaultConfigPath()
if err != nil {
return "", err
defaultHome, defaultHomeErr := constants.DefaultConfigPath()
if defaultHomeErr != nil {
return "" ,defaultHomeErr
}

output.Debugf("Using default config directory: %s", defaultHome)
return defaultHome, nil
}

func setVerbosity(verbosity int) {
switch verbosity {
case 0:
break
case 1:
output.Debugf("Setting verbosity to %s", output.LogLevelDebug)
output.SetLogLevel(output.LogLevelDebug)
case 2:
output.Debugf("Setting verbosity to %s", output.LogLevelTrace)
output.SetLogLevel(output.LogLevelTrace)
default:
output.Debugf("Setting verbosity to %s and disabling progress bars", output.LogLevelTrace)
output.SetLogLevel(output.LogLevelTrace)
output.SetProgressBars("none")
}
}
130 changes: 130 additions & 0 deletions pkg/cmd/config/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package config

import (
"encoding/json"

"fmt"

"strings"

"github.com/kitops-ml/kitops/pkg/lib/util"
"github.com/kitops-ml/kitops/pkg/output"
"github.com/spf13/cobra"
)

type Config struct {
ConfigHome string `yaml:"configHome" json:"configHome"`
Verbosity int `yaml:"verbosity" json:"verbosity"`
LogLevel string `yaml:"logLevel" json:"logLevel"`
ProgressBars string `yaml:"progressBars" json:"progressBars"`
}

func ConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: configShortDesc,
Long: configLongDesc,
}
cmd.AddCommand(configSetCommand())
cmd.AddCommand(configGetCommand())
cmd.AddCommand(configListCommand())
cmd.AddCommand(configResetCommand())

return cmd
}

func configSetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "set KEY VALUE",
Short: setConfigShortDesc,
Long: setConfigLongDesc,
RunE: runSetCommand,
}
cmd.Args = cobra.ExactArgs(2)

return cmd
}

func runSetCommand(cmd *cobra.Command, args []string) error {
if err := setConfig(args[0], args[1]); err != nil {
return output.Fatalf("%s", err)
}
return nil
}

func configGetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "get KEY",
Short: getConfigShortDesc,
Long: getConfigLongDesc,
RunE: runGetCommand,
}
cmd.Args = cobra.ExactArgs(1)

return cmd
}

func runGetCommand(cmd *cobra.Command, args []string) error {
val, err := getConfig(args[0])
if err != nil {
return output.Fatalf("%s", err)
}
fmt.Fprintln(output.GetOut(), val)
return nil
}

func configListCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: listConfigShortDesc,
Long: listConfigLongDesc,
RunE: runListCommand,
}
cmd.Args = cobra.NoArgs

return cmd
}

func runListCommand(cmd *cobra.Command, args []string) error {
configStruct, err := listConfig()
if err != nil {
return output.Fatalf("%s", err)
}

list, jsonErr := json.MarshalIndent(configStruct, "", " ")

if jsonErr != nil {
return jsonErr
}

output.Infoln(string(list))

return nil
}

func configResetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "reset",
Short: resetConfigShortDesc,
Long: resetConfigLongDesc,
RunE: runResetCommand,
}
cmd.Args = cobra.NoArgs

return cmd
}

func runResetCommand(cmd *cobra.Command, args []string) error {
warning := "Warning: this action is destructive and cannot be undone.Proceed? (y/N): "

choice, choiceErr := util.PromptForInput(warning, false)
if choiceErr != nil {
return choiceErr
}

if !strings.EqualFold(choice, "y") && !strings.EqualFold(choice, "yes") {
return nil
}

return resetConfig()
}
Loading