all.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package commands
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/porter-dev/porter/cli/cmd/config"
  6. "github.com/porter-dev/porter/cli/cmd/utils"
  7. "github.com/spf13/cobra"
  8. )
  9. func rootFunc(cmd *cobra.Command, args []string) error {
  10. ctx := cmd.Context()
  11. flagsConfig := parseRootConfigFlags(cmd)
  12. profile, err := cmd.Flags().GetString("profile")
  13. if err != nil {
  14. return fmt.Errorf("error getting profile flag: %w", err)
  15. }
  16. cliConf, currentProfile, err := config.InitAndLoadConfig(ctx, profile, flagsConfig)
  17. if err != nil {
  18. fmt.Println("unwrapped", errors.Unwrap(err))
  19. return fmt.Errorf("error whilst initialising config: %w", err)
  20. }
  21. cmd.PersistentFlags().AddFlagSet(utils.DefaultFlagSet)
  22. cmd.AddCommand(registerCommand_App(cliConf, currentProfile))
  23. cmd.AddCommand(registerCommand_Apply(cliConf, currentProfile))
  24. cmd.AddCommand(registerCommand_Auth(cliConf, currentProfile))
  25. cmd.AddCommand(registerCommand_Cluster(cliConf, currentProfile))
  26. cmd.AddCommand(registerCommand_Config(cliConf, currentProfile))
  27. cmd.AddCommand(registerCommand_Connect(cliConf, currentProfile))
  28. cmd.AddCommand(registerCommand_Create(cliConf, currentProfile))
  29. cmd.AddCommand(registerCommand_Delete(cliConf, currentProfile))
  30. cmd.AddCommand(registerCommand_Deploy(cliConf, currentProfile))
  31. cmd.AddCommand(registerCommand_Docker(cliConf, currentProfile))
  32. cmd.AddCommand(registerCommand_Get(cliConf, currentProfile))
  33. cmd.AddCommand(registerCommand_Helm(cliConf, currentProfile))
  34. cmd.AddCommand(registerCommand_Job(cliConf, currentProfile))
  35. cmd.AddCommand(registerCommand_Kubectl(cliConf, currentProfile))
  36. cmd.AddCommand(registerCommand_List(cliConf, currentProfile))
  37. cmd.AddCommand(registerCommand_Logs(cliConf, currentProfile))
  38. cmd.AddCommand(registerCommand_Open(cliConf, currentProfile))
  39. cmd.AddCommand(registerCommand_PortForward(cliConf, currentProfile))
  40. cmd.AddCommand(registerCommand_Project(cliConf, currentProfile))
  41. cmd.AddCommand(registerCommand_Registry(cliConf, currentProfile))
  42. cmd.AddCommand(registerCommand_Run(cliConf, currentProfile))
  43. cmd.AddCommand(registerCommand_Server(cliConf, currentProfile))
  44. cmd.AddCommand(registerCommand_Stack(cliConf, currentProfile))
  45. cmd.AddCommand(registerCommand_Update(cliConf, currentProfile))
  46. cmd.AddCommand(registerCommand_Version(cliConf, currentProfile))
  47. return nil
  48. }
  49. // parseRootConfigFlags grabs the runtime value of registered root level persisted flags.
  50. func parseRootConfigFlags(cmd *cobra.Command) 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. var profile string
  62. var config config.CLIConfig
  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. stringName: "profile",
  82. stringConfigTarget: &profile,
  83. },
  84. {
  85. uintName: "project",
  86. uintConfigTarget: &config.Project,
  87. },
  88. {
  89. uintName: "cluster",
  90. uintConfigTarget: &config.Cluster,
  91. },
  92. {
  93. uintName: "registry",
  94. uintConfigTarget: &config.Registry,
  95. },
  96. {
  97. uintName: "helm_repo",
  98. uintConfigTarget: &config.HelmRepo,
  99. },
  100. }
  101. for _, fl := range flagsToOverride {
  102. if fl.stringName != "" {
  103. st, _ := cmd.Flags().GetString(fl.stringName)
  104. if st != "" {
  105. *fl.stringConfigTarget = st
  106. }
  107. }
  108. if fl.uintName != "" {
  109. ui, _ := cmd.Flags().GetUint(fl.uintName)
  110. if ui != 0 {
  111. *fl.uintConfigTarget = ui
  112. }
  113. }
  114. }
  115. return config
  116. }