helmfile2argo/cmd/root.go

72 lines
1.7 KiB
Go

/*
Copyright © 2026 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"os"
"fmt"
"errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Use: "helmfile2argo",
Short: "Convert a helmfile to a set of argocd applications",
Long: `Takes a helmfile .yaml or .yaml.gotmpl file as input and outputs
a set of .yaml files describing argocd Applications that are built from the
releases section.`,
}
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func initializeConfig(cmd *cobra.Command) error {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search for a config file with the name "config" (without extension).
viper.AddConfigPath("/etc/helmfile2argo/")
viper.AddConfigPath("$HOME/.helmfile2argo")
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
}
if err := viper.ReadInConfig(); err != nil {
// It's okay if the config file doesn't exist.
var configFileNotFoundError viper.ConfigFileNotFoundError
if !errors.As(err, &configFileNotFoundError) {
return err
}
}
err := viper.BindPFlags(cmd.Flags())
if err != nil {
return err
}
fmt.Println("Configuration initialized. Using config file:", viper.ConfigFileUsed())
return nil
}
func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.helmfile2argo/config)")
initializeConfig(rootCmd)
}