root.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // check that the .porter folder exists; create if not
  22. porterDir := filepath.Join(home, ".porter")
  23. if _, err := os.Stat(porterDir); os.IsNotExist(err) {
  24. os.Mkdir(porterDir, 0700)
  25. } else if err != nil {
  26. color.New(color.FgRed).Printf("%v\n", err)
  27. os.Exit(1)
  28. }
  29. viper.SetConfigName("porter")
  30. viper.SetConfigType("yaml")
  31. viper.AddConfigPath(porterDir)
  32. err := viper.ReadInConfig()
  33. if err != nil {
  34. if _, ok := err.(viper.ConfigFileNotFoundError); ok {
  35. // create blank config file
  36. err := ioutil.WriteFile(filepath.Join(home, ".porter", "porter.yaml"), []byte{}, 0644)
  37. if err != nil {
  38. color.New(color.FgRed).Printf("%v\n", err)
  39. os.Exit(1)
  40. }
  41. } else {
  42. // Config file was found but another error was produced
  43. color.New(color.FgRed).Printf("%v\n", err)
  44. os.Exit(1)
  45. }
  46. }
  47. if err := rootCmd.Execute(); err != nil {
  48. color.New(color.FgRed).Println(err)
  49. os.Exit(1)
  50. }
  51. }