all.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package commands
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/cli/cmd/config"
  5. "github.com/porter-dev/porter/cli/cmd/utils"
  6. "github.com/spf13/cobra"
  7. )
  8. // RegisterCommands initiates config and sets up all commands.
  9. // Error returned here is a placeholder as the register commands do not currently return errors, and handle exits themselves. This may change at a later date.
  10. func RegisterCommands() (*cobra.Command, error) {
  11. cliConf, err := config.InitAndLoadConfig()
  12. if err != nil {
  13. return nil, fmt.Errorf("error loading porter config: %w", err)
  14. }
  15. rootCmd := &cobra.Command{
  16. Use: "porter",
  17. Short: "Porter is a dashboard for managing Kubernetes clusters.",
  18. 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`,
  19. }
  20. rootCmd.PersistentFlags().AddFlagSet(utils.DefaultFlagSet)
  21. rootCmd.AddCommand(registerCommand_App(cliConf))
  22. rootCmd.AddCommand(registerCommand_Apply(cliConf))
  23. rootCmd.AddCommand(registerCommand_Auth(cliConf))
  24. rootCmd.AddCommand(registerCommand_Cluster(cliConf))
  25. rootCmd.AddCommand(registerCommand_Config(cliConf))
  26. rootCmd.AddCommand(registerCommand_Connect(cliConf))
  27. rootCmd.AddCommand(registerCommand_Create(cliConf))
  28. rootCmd.AddCommand(registerCommand_Delete(cliConf))
  29. rootCmd.AddCommand(registerCommand_Deploy(cliConf))
  30. rootCmd.AddCommand(registerCommand_Docker(cliConf))
  31. rootCmd.AddCommand(registerCommand_Get(cliConf))
  32. rootCmd.AddCommand(registerCommand_Helm(cliConf))
  33. rootCmd.AddCommand(registerCommand_Job(cliConf))
  34. rootCmd.AddCommand(registerCommand_Kubectl(cliConf))
  35. rootCmd.AddCommand(registerCommand_List(cliConf))
  36. rootCmd.AddCommand(registerCommand_Logs(cliConf))
  37. rootCmd.AddCommand(registerCommand_Open(cliConf))
  38. rootCmd.AddCommand(registerCommand_PortForward(cliConf))
  39. rootCmd.AddCommand(registerCommand_Project(cliConf))
  40. rootCmd.AddCommand(registerCommand_Registry(cliConf))
  41. rootCmd.AddCommand(registerCommand_Run(cliConf))
  42. rootCmd.AddCommand(registerCommand_Server(cliConf))
  43. rootCmd.AddCommand(registerCommand_Stack(cliConf))
  44. rootCmd.AddCommand(registerCommand_Update(cliConf))
  45. rootCmd.AddCommand(registerCommand_Version(cliConf))
  46. rootCmd.AddCommand(registerCommand_Env(cliConf))
  47. rootCmd.AddCommand(registerCommand_Datastore(cliConf))
  48. return rootCmd, nil
  49. }
  50. // overrideConfigWithFlags grabs the runtime value of registered flags, and overrides the values in CLIConfig.
  51. // It was done this way to reduce the size of a refactor, as the codebase conflates initialisation of the commands, with the runtime values.
  52. func overrideConfigWithFlags(cmd *cobra.Command, config config.CLIConfig) config.CLIConfig {
  53. type flag struct {
  54. // stringName is the name of the flag which is a string
  55. stringName string
  56. // stringConfigTarget is the pointer to the string in the config struct
  57. stringConfigTarget *string
  58. // uintName is the name of the flag which is a uint
  59. uintName string
  60. // uintConfigTarget is the pointer to the uint in the config struct
  61. uintConfigTarget *uint
  62. }
  63. flagsToOverride := []flag{
  64. {
  65. stringName: "driver",
  66. stringConfigTarget: &config.Driver,
  67. },
  68. {
  69. stringName: "host",
  70. stringConfigTarget: &config.Host,
  71. },
  72. {
  73. stringName: "token",
  74. stringConfigTarget: &config.Token,
  75. },
  76. {
  77. stringName: "kubeconfig",
  78. stringConfigTarget: &config.Kubeconfig,
  79. },
  80. {
  81. uintName: "project",
  82. uintConfigTarget: &config.Project,
  83. },
  84. {
  85. uintName: "cluster",
  86. uintConfigTarget: &config.Cluster,
  87. },
  88. {
  89. uintName: "registry",
  90. uintConfigTarget: &config.Registry,
  91. },
  92. {
  93. uintName: "helm_repo",
  94. uintConfigTarget: &config.HelmRepo,
  95. },
  96. }
  97. for _, fl := range flagsToOverride {
  98. if fl.stringName != "" {
  99. st, _ := cmd.Flags().GetString(fl.stringName)
  100. if st != "" {
  101. *fl.stringConfigTarget = st
  102. }
  103. }
  104. if fl.uintName != "" {
  105. ui, _ := cmd.Flags().GetUint(fl.uintName)
  106. if ui != 0 {
  107. *fl.uintConfigTarget = ui
  108. }
  109. }
  110. }
  111. return config
  112. }