2
0

all.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_Create(cliConf))
  27. rootCmd.AddCommand(registerCommand_Delete(cliConf))
  28. rootCmd.AddCommand(registerCommand_Deploy(cliConf))
  29. rootCmd.AddCommand(registerCommand_Docker(cliConf))
  30. rootCmd.AddCommand(registerCommand_Get(cliConf))
  31. rootCmd.AddCommand(registerCommand_Helm(cliConf))
  32. rootCmd.AddCommand(registerCommand_Job(cliConf))
  33. rootCmd.AddCommand(registerCommand_Kubectl(cliConf))
  34. rootCmd.AddCommand(registerCommand_List(cliConf))
  35. rootCmd.AddCommand(registerCommand_Logs(cliConf))
  36. rootCmd.AddCommand(registerCommand_Open(cliConf))
  37. rootCmd.AddCommand(registerCommand_Project(cliConf))
  38. rootCmd.AddCommand(registerCommand_Registry(cliConf))
  39. rootCmd.AddCommand(registerCommand_Run(cliConf))
  40. rootCmd.AddCommand(registerCommand_Server(cliConf))
  41. rootCmd.AddCommand(registerCommand_Stack(cliConf))
  42. rootCmd.AddCommand(registerCommand_Target(cliConf))
  43. rootCmd.AddCommand(registerCommand_Update(cliConf))
  44. rootCmd.AddCommand(registerCommand_Version(cliConf))
  45. rootCmd.AddCommand(registerCommand_Env(cliConf))
  46. rootCmd.AddCommand(registerCommand_Datastore(cliConf))
  47. return rootCmd, nil
  48. }
  49. // overrideConfigWithFlags grabs the runtime value of registered flags, and overrides the values in CLIConfig.
  50. // It was done this way to reduce the size of a refactor, as the codebase conflates initialisation of the commands, with the runtime values.
  51. func overrideConfigWithFlags(cmd *cobra.Command, config config.CLIConfig) config.CLIConfig {
  52. type flag struct {
  53. // stringName is the name of the flag which is a string
  54. stringName string
  55. // stringConfigTarget is the pointer to the string in the config struct
  56. stringConfigTarget *string
  57. // uintName is the name of the flag which is a uint
  58. uintName string
  59. // uintConfigTarget is the pointer to the uint in the config struct
  60. uintConfigTarget *uint
  61. }
  62. flagsToOverride := []flag{
  63. {
  64. stringName: "driver",
  65. stringConfigTarget: &config.Driver,
  66. },
  67. {
  68. stringName: "host",
  69. stringConfigTarget: &config.Host,
  70. },
  71. {
  72. stringName: "token",
  73. stringConfigTarget: &config.Token,
  74. },
  75. {
  76. stringName: "kubeconfig",
  77. stringConfigTarget: &config.Kubeconfig,
  78. },
  79. {
  80. uintName: "project",
  81. uintConfigTarget: &config.Project,
  82. },
  83. {
  84. uintName: "cluster",
  85. uintConfigTarget: &config.Cluster,
  86. },
  87. {
  88. uintName: "registry",
  89. uintConfigTarget: &config.Registry,
  90. },
  91. {
  92. uintName: "helm_repo",
  93. uintConfigTarget: &config.HelmRepo,
  94. },
  95. }
  96. for _, fl := range flagsToOverride {
  97. if fl.stringName != "" {
  98. st, _ := cmd.Flags().GetString(fl.stringName)
  99. if st != "" {
  100. *fl.stringConfigTarget = st
  101. }
  102. }
  103. if fl.uintName != "" {
  104. ui, _ := cmd.Flags().GetUint(fl.uintName)
  105. if ui != 0 {
  106. *fl.uintConfigTarget = ui
  107. }
  108. }
  109. }
  110. return config
  111. }