grafana-backuper/internal/cmd/root.go

103 lines
3.2 KiB
Go
Raw Normal View History

package cmd
import (
"fmt"
"os"
"strings"
"git.ar21.de/yolokube/grafana-backuper/internal/config"
"git.ar21.de/yolokube/grafana-backuper/internal/logger"
"git.ar21.de/yolokube/grafana-backuper/internal/version"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
const envVarPrefix = "GB"
func NewRootCommand(c *config.Config) *cobra.Command {
rootCmd := &cobra.Command{
Use: "grafana-backuper",
Short: "Grafana Backuper CLI",
Long: "A command-line tool to back up and restore Grafana dashboards",
Version: version.Version(),
RunE: func(cmd *cobra.Command, _ []string) error {
if len(c.Sequence) == 0 {
return cmd.Help()
}
for _, command := range c.Sequence {
log.Info().Str("function", command).Msg("Executing function")
cmd.SetArgs([]string{command})
if err := cmd.Execute(); err != nil {
return err
}
}
return nil
},
2024-07-30 21:20:18 +02:00
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
initializeConfig(cmd)
// c.Output = os.Stdout
cmd.SetOut(os.Stdout)
2024-07-30 21:20:18 +02:00
zerolog.SetGlobalLevel(zerolog.InfoLevel)
2024-07-30 21:20:18 +02:00
if c.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
2024-08-19 20:22:41 +02:00
} else if c.Quiet {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}
if c.JSONFormat {
log.Logger = logger.JSONLoggerLayout(os.Stderr)
c.Logger = logger.JSONLoggerLayout(os.Stdout)
} else {
log.Logger = logger.CliLoggerLayout(os.Stderr)
c.Logger = logger.CliLoggerLayout(os.Stdout)
2024-07-30 21:20:18 +02:00
}
},
SilenceUsage: true,
}
rootCmd.PersistentFlags().BoolVarP(&c.Debug, "debug", "d", false, "Debug output")
rootCmd.PersistentFlags().BoolVar(&c.JSONFormat, "json", false, "JSON output")
2024-08-19 20:22:41 +02:00
rootCmd.PersistentFlags().BoolVar(&c.Quiet, "quiet", false, "Quiet output (only errors)")
rootCmd.PersistentFlags().StringVar(&c.GrafanaURL, "grafana-url", "", "Grafana URL to access the API")
rootCmd.PersistentFlags().StringVar(&c.GrafanaToken, "grafana-token", "", "Grafana auth token to access the API")
rootCmd.PersistentFlags().StringVar(&c.GitBranch, "git-branch", "main", "Git branch name")
rootCmd.PersistentFlags().StringVar(&c.GitRepo, "git-repo", "", "Complete Git repository URL")
rootCmd.PersistentFlags().StringVar(&c.GitUser, "git-user", "", "Git user name")
rootCmd.PersistentFlags().StringVar(&c.GitPass, "git-pass", "", "Git user password")
rootCmd.PersistentFlags().StringVar(&c.GitEmail, "git-email", "", "Git email address")
rootCmd.PersistentFlags().StringVar(&c.GPGKey, "signing-key", "", "Path to the GPG signing key")
rootCmd.Flags().StringSliceVar(&c.Sequence, "sequence", nil, "Command sequence to execute multiple functions")
return rootCmd
}
func initializeConfig(cmd *cobra.Command) {
v := viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.SetEnvPrefix(envVarPrefix)
v.AutomaticEnv()
bindFlags(cmd, v)
}
func bindFlags(cmd *cobra.Command, v *viper.Viper) {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
// Apply the viper config value to the flag when the flag is not set and viper has a value
if !flag.Changed && v.IsSet(flag.Name) {
err := cmd.Flags().Set(
flag.Name,
fmt.Sprintf("%v", v.Get(flag.Name)),
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
}
})
}