all.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. return rootCmd, nil
  47. }
  48. // overrideConfigWithFlags grabs the runtime value of registered flags, and overrides the values in CLIConfig.
  49. // It was done this way to reduce the size of a refactor, as the codebase conflates initialisation of the commands, with the runtime values.
  50. func overrideConfigWithFlags(cmd *cobra.Command, config config.CLIConfig) config.CLIConfig {
  51. type flag struct {
  52. // stringName is the name of the flag which is a string
  53. stringName string
  54. // stringConfigTarget is the pointer to the string in the config struct
  55. stringConfigTarget *string
  56. // uintName is the name of the flag which is a uint
  57. uintName string
  58. // uintConfigTarget is the pointer to the uint in the config struct
  59. uintConfigTarget *uint
  60. }
  61. flagsToOverride := []flag{
  62. {
  63. stringName: "driver",
  64. stringConfigTarget: &config.Driver,
  65. },
  66. {
  67. stringName: "host",
  68. stringConfigTarget: &config.Host,
  69. },
  70. {
  71. stringName: "token",
  72. stringConfigTarget: &config.Token,
  73. },
  74. {
  75. stringName: "kubeconfig",
  76. stringConfigTarget: &config.Kubeconfig,
  77. },
  78. {
  79. uintName: "project",
  80. uintConfigTarget: &config.Project,
  81. },
  82. {
  83. uintName: "cluster",
  84. uintConfigTarget: &config.Cluster,
  85. },
  86. {
  87. uintName: "registry",
  88. uintConfigTarget: &config.Registry,
  89. },
  90. {
  91. uintName: "helm_repo",
  92. uintConfigTarget: &config.HelmRepo,
  93. },
  94. }
  95. for _, fl := range flagsToOverride {
  96. if fl.stringName != "" {
  97. st, _ := cmd.Flags().GetString(fl.stringName)
  98. if st != "" {
  99. *fl.stringConfigTarget = st
  100. }
  101. }
  102. if fl.uintName != "" {
  103. ui, _ := cmd.Flags().GetUint(fl.uintName)
  104. if ui != 0 {
  105. *fl.uintConfigTarget = ui
  106. }
  107. }
  108. }
  109. return config
  110. }