config.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/briandowns/spinner"
  12. "github.com/fatih/color"
  13. api "github.com/porter-dev/porter/api/client"
  14. "github.com/porter-dev/porter/api/types"
  15. cliConfig "github.com/porter-dev/porter/cli/cmd/config"
  16. "github.com/porter-dev/porter/cli/cmd/utils"
  17. "github.com/spf13/cobra"
  18. )
  19. var cliConf = cliConfig.GetCLIConfig()
  20. var configCmd = &cobra.Command{
  21. Use: "config",
  22. Short: "Commands that control local configuration settings",
  23. Run: func(cmd *cobra.Command, args []string) {
  24. if err := printConfig(); err != nil {
  25. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  26. os.Exit(1)
  27. }
  28. },
  29. }
  30. var configSetProjectCmd = &cobra.Command{
  31. Use: "set-project [id]",
  32. Args: cobra.MaximumNArgs(1),
  33. Short: "Saves the project id in the default configuration",
  34. Run: func(cmd *cobra.Command, args []string) {
  35. if len(args) == 0 {
  36. err := checkLoginAndRun(args, listAndSetProject)
  37. if err != nil {
  38. os.Exit(1)
  39. }
  40. } else {
  41. projID, err := strconv.ParseUint(args[0], 10, 64)
  42. if err != nil {
  43. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  44. os.Exit(1)
  45. }
  46. err = cliConf.SetProject(uint(projID))
  47. if err != nil {
  48. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  49. os.Exit(1)
  50. }
  51. }
  52. },
  53. }
  54. var configSetClusterCmd = &cobra.Command{
  55. Use: "set-cluster [id]",
  56. Args: cobra.MaximumNArgs(1),
  57. Short: "Saves the cluster id in the default configuration",
  58. Run: func(cmd *cobra.Command, args []string) {
  59. if len(args) == 0 {
  60. err := checkLoginAndRun(args, listAndSetCluster)
  61. if err != nil {
  62. os.Exit(1)
  63. }
  64. } else {
  65. clusterID, err := strconv.ParseUint(args[0], 10, 64)
  66. if err != nil {
  67. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  68. os.Exit(1)
  69. }
  70. err = cliConf.SetCluster(uint(clusterID))
  71. if err != nil {
  72. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  73. os.Exit(1)
  74. }
  75. }
  76. },
  77. }
  78. var configSetRegistryCmd = &cobra.Command{
  79. Use: "set-registry [id]",
  80. Args: cobra.MaximumNArgs(1),
  81. Short: "Saves the registry id in the default configuration",
  82. Run: func(cmd *cobra.Command, args []string) {
  83. if len(args) == 0 {
  84. err := checkLoginAndRun(args, listAndSetRegistry)
  85. if err != nil {
  86. os.Exit(1)
  87. }
  88. } else {
  89. registryID, err := strconv.ParseUint(args[0], 10, 64)
  90. if err != nil {
  91. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  92. os.Exit(1)
  93. }
  94. err = cliConf.SetRegistry(uint(registryID))
  95. if err != nil {
  96. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  97. os.Exit(1)
  98. }
  99. }
  100. },
  101. }
  102. var configSetHelmRepoCmd = &cobra.Command{
  103. Use: "set-helmrepo [id]",
  104. Args: cobra.ExactArgs(1),
  105. Short: "Saves the helm repo id in the default configuration",
  106. Run: func(cmd *cobra.Command, args []string) {
  107. hrID, err := strconv.ParseUint(args[0], 10, 64)
  108. if err != nil {
  109. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  110. os.Exit(1)
  111. }
  112. err = cliConf.SetHelmRepo(uint(hrID))
  113. if err != nil {
  114. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  115. os.Exit(1)
  116. }
  117. },
  118. }
  119. var configSetHostCmd = &cobra.Command{
  120. Use: "set-host [host]",
  121. Args: cobra.ExactArgs(1),
  122. Short: "Saves the host in the default configuration",
  123. Run: func(cmd *cobra.Command, args []string) {
  124. err := cliConf.SetHost(args[0])
  125. if err != nil {
  126. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  127. os.Exit(1)
  128. }
  129. },
  130. }
  131. var configSetKubeconfigCmd = &cobra.Command{
  132. Use: "set-kubeconfig [kubeconfig-path]",
  133. Args: cobra.ExactArgs(1),
  134. Short: "Saves the path to kubeconfig in the default configuration",
  135. Run: func(cmd *cobra.Command, args []string) {
  136. err := cliConf.SetKubeconfig(args[0])
  137. if err != nil {
  138. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  139. os.Exit(1)
  140. }
  141. },
  142. }
  143. var configCreateProfileCmd = &cobra.Command{
  144. Use: "create-profile [NAME]",
  145. Args: cobra.ExactArgs(1),
  146. Short: "Creates a new Porter CLI profile",
  147. Run: func(cmd *cobra.Command, args []string) {
  148. err := cliConf.CreateProfile(args[0])
  149. if err != nil {
  150. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  151. os.Exit(1)
  152. }
  153. },
  154. }
  155. var configListProfilesCmd = &cobra.Command{
  156. Use: "list-profiles",
  157. Aliases: []string{"list-profile"},
  158. Args: cobra.NoArgs,
  159. Short: "List all Porter CLI profiles",
  160. Run: func(cmd *cobra.Command, args []string) {
  161. profiles, err := cliConf.ListProfiles()
  162. if err != nil {
  163. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  164. os.Exit(1)
  165. }
  166. for _, profile := range profiles {
  167. fmt.Println(profile)
  168. }
  169. },
  170. }
  171. var configSetProfileCmd = &cobra.Command{
  172. Use: "set-profile [NAME]",
  173. Args: cobra.ExactArgs(1),
  174. Short: "Use a particular Porter CLI profile",
  175. Run: func(cmd *cobra.Command, args []string) {
  176. err := cliConf.SetProfile(args[0])
  177. if err != nil {
  178. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  179. os.Exit(1)
  180. }
  181. },
  182. }
  183. var configDeleteProfileCmd = &cobra.Command{
  184. Use: "delete-profile [NAME]",
  185. Args: cobra.ExactArgs(1),
  186. Short: "Delete a Porter CLI profile",
  187. Run: func(cmd *cobra.Command, args []string) {
  188. err := cliConf.DeleteProfile(args[0])
  189. if err != nil {
  190. color.New(color.FgRed).Printf("An error occurred: %v\n", err)
  191. os.Exit(1)
  192. }
  193. },
  194. }
  195. func init() {
  196. rootCmd.AddCommand(configCmd)
  197. configCmd.AddCommand(configSetProjectCmd)
  198. configCmd.AddCommand(configSetClusterCmd)
  199. configCmd.AddCommand(configSetHostCmd)
  200. configCmd.AddCommand(configSetRegistryCmd)
  201. configCmd.AddCommand(configSetHelmRepoCmd)
  202. configCmd.AddCommand(configSetKubeconfigCmd)
  203. configCmd.AddCommand(configCreateProfileCmd)
  204. configCmd.AddCommand(configListProfilesCmd)
  205. configCmd.AddCommand(configSetProfileCmd)
  206. configCmd.AddCommand(configDeleteProfileCmd)
  207. }
  208. func printConfig() error {
  209. config, err := ioutil.ReadFile(filepath.Join(home, ".porter", "porter.yaml"))
  210. if err != nil {
  211. return err
  212. }
  213. fmt.Print(string(config))
  214. return nil
  215. }
  216. func listAndSetProject(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  217. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  218. s.Color("cyan")
  219. s.Suffix = " Loading list of projects"
  220. s.Start()
  221. resp, err := client.ListUserProjects(context.Background())
  222. s.Stop()
  223. if err != nil {
  224. return err
  225. }
  226. var projID uint64
  227. if len(*resp) > 1 {
  228. // only give the option to select when more than one option exists
  229. projName, err := utils.PromptSelect("Select a project with ID", func() []string {
  230. var names []string
  231. for _, proj := range *resp {
  232. names = append(names, fmt.Sprintf("%s - %d", proj.Name, proj.ID))
  233. }
  234. return names
  235. }())
  236. if err != nil {
  237. return err
  238. }
  239. projID, _ = strconv.ParseUint(strings.Split(projName, " - ")[1], 10, 64)
  240. } else {
  241. projID = uint64((*resp)[0].ID)
  242. }
  243. cliConf.SetProject(uint(projID))
  244. return nil
  245. }
  246. func listAndSetCluster(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  247. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  248. s.Color("cyan")
  249. s.Suffix = " Loading list of clusters"
  250. s.Start()
  251. resp, err := client.ListProjectClusters(context.Background(), cliConf.Project)
  252. s.Stop()
  253. if err != nil {
  254. return err
  255. }
  256. var clusterID uint64
  257. if len(*resp) > 1 {
  258. clusterName, err := utils.PromptSelect("Select a cluster with ID", func() []string {
  259. var names []string
  260. for _, cluster := range *resp {
  261. names = append(names, fmt.Sprintf("%s - %d", cluster.Name, cluster.ID))
  262. }
  263. return names
  264. }())
  265. if err != nil {
  266. return err
  267. }
  268. clusterID, _ = strconv.ParseUint(strings.Split(clusterName, " - ")[1], 10, 64)
  269. } else {
  270. clusterID = uint64((*resp)[0].ID)
  271. }
  272. cliConf.SetCluster(uint(clusterID))
  273. return nil
  274. }
  275. func listAndSetRegistry(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  276. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  277. s.Color("cyan")
  278. s.Suffix = " Loading list of registries"
  279. s.Start()
  280. resp, err := client.ListRegistries(context.Background(), cliConf.Project)
  281. s.Stop()
  282. if err != nil {
  283. return err
  284. }
  285. var regID uint64
  286. if len(*resp) > 1 {
  287. regName, err := utils.PromptSelect("Select a registry with ID", func() []string {
  288. var names []string
  289. for _, cluster := range *resp {
  290. names = append(names, fmt.Sprintf("%s - %d", cluster.Name, cluster.ID))
  291. }
  292. return names
  293. }())
  294. if err != nil {
  295. return err
  296. }
  297. regID, _ = strconv.ParseUint(strings.Split(regName, " - ")[1], 10, 64)
  298. } else {
  299. regID = uint64((*resp)[0].ID)
  300. }
  301. cliConf.SetRegistry(uint(regID))
  302. return nil
  303. }