root.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package cmd
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "github.com/fatih/color"
  7. "github.com/spf13/cobra"
  8. "github.com/spf13/viper"
  9. "k8s.io/client-go/util/homedir"
  10. )
  11. // rootCmd represents the base command when called without any subcommands
  12. var rootCmd = &cobra.Command{
  13. Use: "porter",
  14. Short: "Porter is a dashboard for managing Kubernetes clusters.",
  15. Long: `Porter is a tool for creating, versioning, and updating Kubernetes deployments using a visual dashboard. For more information, visit github.com/porter-dev/porter`,
  16. }
  17. var home = homedir.HomeDir()
  18. // Execute adds all child commands to the root command and sets flags appropriately.
  19. // This is called by main.main(). It only needs to happen once to the rootCmd.
  20. func Execute() {
  21. viper.SetConfigName("porter")
  22. viper.SetConfigType("yaml")
  23. viper.AddConfigPath(filepath.Join(home, ".porter"))
  24. err := viper.ReadInConfig()
  25. if err != nil {
  26. if _, ok := err.(viper.ConfigFileNotFoundError); ok {
  27. // create blank config file
  28. err := ioutil.WriteFile(filepath.Join(home, ".porter", "porter.yaml"), []byte{}, 0644)
  29. if err != nil {
  30. color.New(color.FgRed).Printf("%v\n", err)
  31. os.Exit(1)
  32. }
  33. } else {
  34. // Config file was found but another error was produced
  35. color.New(color.FgRed).Printf("%v\n", err)
  36. os.Exit(1)
  37. }
  38. }
  39. if err := rootCmd.Execute(); err != nil {
  40. color.New(color.FgRed).Println(err)
  41. os.Exit(1)
  42. }
  43. }