/
root.go
80 lines (68 loc) · 2.17 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cmd
import (
database "database-sqlc"
"database/sql"
"fmt"
"github.com/govcms-tests/govcms-cli/pkg/settings"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log/slog"
"os"
)
// var RootCmd *cobra.Command
var cfgFile string
var appConfig settings.Config // Rename config to appConfig
var AppFs afero.Fs
var installationManager *database.InstallationManager
func NewRootCmd(appFs afero.Fs, db *sql.DB) *cobra.Command {
AppFs = appFs
installationManager = database.NewInstallationManager(db, appFs)
cmd := &cobra.Command{
Use: "govcms",
Short: "A CLI tool to help with the development and maintenance of the GovCMS platform",
Long: "A CLI tool to help with the development and maintenance of the GovCMS platform",
Version: "0.2.1",
}
cobra.OnInitialize(initConfig)
cmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Set config file, default is $HOME/.govcms-cli.yaml")
cmd.SetVersionTemplate("GovCMS CLI version " + cmd.Version + "\n")
cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
cmd.AddCommand(removeCmd)
cmd.AddCommand(NewGetCmd())
cmd.AddCommand(upCmd)
cmd.AddCommand(downCmd)
cmd.AddCommand(checkCmd)
cmd.AddCommand(listCmd)
// Register the persistent pre-run function
cmd.PersistentPreRunE = preRun
return cmd
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
// Load configuration settings from settings.go
var err error
appConfig, err = settings.LoadConfig()
if err != nil {
fmt.Println("Error loading configuration:", err)
os.Exit(1)
}
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".govcms-cli" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".govcms-cli")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
configString := viper.ConfigFileUsed()
slog.Debug("Using config file: " + configString)
}
}