|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/mitchellh/go-homedir" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | + |
| 14 | + "github.com/plumber-cd/terraform-backend-git/cmd/discovery" |
| 15 | + "github.com/plumber-cd/terraform-backend-git/pid" |
| 16 | + "github.com/plumber-cd/terraform-backend-git/server" |
| 17 | +) |
| 18 | + |
| 19 | +var cfgFile string |
| 20 | + |
| 21 | +// rootCmd main command that just starts the server and keeps listening on port until terminated |
| 22 | +var rootCmd = &cobra.Command{ |
| 23 | + Use: "terraform-backend-git", |
| 24 | + Short: "Terraform HTTP backend implementation that uses Git as storage", |
| 25 | + // will use known storage types in this repository and start a local HTTP server |
| 26 | + Run: func(cmd *cobra.Command, args []string) { |
| 27 | + if err := pid.LockPidFile(); err != nil { |
| 28 | + log.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + server.Start() |
| 32 | + }, |
| 33 | +} |
| 34 | + |
| 35 | +// BackendsCmds is a list of backend types available via cmd wrapper |
| 36 | +var BackendsCmds = make([]*cobra.Command, 0) |
| 37 | + |
| 38 | +// WrappersCmds is a list of wrapper commands available to run wrapped into a backend wrapper |
| 39 | +// i.e. backand wrapper "git" will start an http backend in Git storage mode |
| 40 | +// and "terraform" wrapper started from it will use terraform while that backend is running |
| 41 | +var WrappersCmds = make([]*cobra.Command, 0) |
| 42 | + |
| 43 | +func Exec() { |
| 44 | + if err := discovery.Root().Execute(); err != nil { |
| 45 | + // If the error was coming from a wrapper, must respect it's exit code |
| 46 | + exitErr, ok := err.(*exec.ExitError) |
| 47 | + if ok { |
| 48 | + os.Exit(exitErr.ExitCode()) |
| 49 | + } |
| 50 | + |
| 51 | + fmt.Println(err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func init() { |
| 57 | + // keep the output clean as in wrapper mode it'll mess out with Terraform own output |
| 58 | + log.SetFlags(0) |
| 59 | + log.SetPrefix("[terraform-backend-git]: ") |
| 60 | + |
| 61 | + cobra.OnInitialize(initConfig) |
| 62 | + |
| 63 | + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is terraform-backend-git.hcl)") |
| 64 | + |
| 65 | + rootCmd.PersistentFlags().StringP("address", "a", "127.0.0.1:6061", "Specify the listen address") |
| 66 | + viper.BindPFlag("address", rootCmd.PersistentFlags().Lookup("address")) |
| 67 | + viper.SetDefault("address", "127.0.0.1:6061") |
| 68 | + rootCmd.PersistentFlags().BoolP("access-logs", "l", false, "Log HTTP requests to the console") |
| 69 | + viper.BindPFlag("accessLogs", rootCmd.PersistentFlags().Lookup("access-logs")) |
| 70 | + viper.SetDefault("accessLogs", false) |
| 71 | + |
| 72 | + discovery.RegisterRoot(rootCmd) |
| 73 | +} |
| 74 | + |
| 75 | +func initConfig() { |
| 76 | + viper.SetConfigType("hcl") |
| 77 | + viper.SetConfigName("terraform-backend-git") |
| 78 | + |
| 79 | + if cfgFile != "" { |
| 80 | + viper.SetConfigFile(cfgFile) |
| 81 | + } else { |
| 82 | + home, err := homedir.Dir() |
| 83 | + if err != nil { |
| 84 | + log.Fatal(err) |
| 85 | + } |
| 86 | + viper.AddConfigPath(home) |
| 87 | + |
| 88 | + cwd, err := os.Getwd() |
| 89 | + if err != nil { |
| 90 | + log.Fatal(err) |
| 91 | + } |
| 92 | + viper.AddConfigPath(cwd) |
| 93 | + } |
| 94 | + |
| 95 | + viper.AutomaticEnv() |
| 96 | + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) |
| 97 | + viper.SetEnvPrefix("TF_BACKEND_GIT") |
| 98 | + |
| 99 | + if err := viper.ReadInConfig(); err == nil { |
| 100 | + log.Println("Using config file:", viper.ConfigFileUsed()) |
| 101 | + } |
| 102 | +} |
0 commit comments