config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. func init() {
  132. rootCmd.AddCommand(configCmd)
  133. configCmd.AddCommand(configSetProjectCmd)
  134. configCmd.AddCommand(configSetClusterCmd)
  135. configCmd.AddCommand(configSetHostCmd)
  136. configCmd.AddCommand(configSetRegistryCmd)
  137. configCmd.AddCommand(configSetHelmRepoCmd)
  138. }
  139. func printConfig() error {
  140. config, err := ioutil.ReadFile(filepath.Join(home, ".porter", "porter.yaml"))
  141. if err != nil {
  142. return err
  143. }
  144. fmt.Println(string(config))
  145. return nil
  146. }
  147. func listAndSetProject(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  148. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  149. s.Color("cyan")
  150. s.Suffix = " Loading list of projects"
  151. s.Start()
  152. resp, err := client.ListUserProjects(context.Background())
  153. s.Stop()
  154. if err != nil {
  155. return err
  156. }
  157. var projID uint64
  158. if len(*resp) > 1 {
  159. // only give the option to select when more than one option exists
  160. projName, err := utils.PromptSelect("Select a project with ID", func() []string {
  161. var names []string
  162. for _, proj := range *resp {
  163. names = append(names, fmt.Sprintf("%s - %d", proj.Name, proj.ID))
  164. }
  165. return names
  166. }())
  167. if err != nil {
  168. return err
  169. }
  170. projID, _ = strconv.ParseUint(strings.Split(projName, " - ")[1], 10, 64)
  171. } else {
  172. projID = uint64((*resp)[0].ID)
  173. }
  174. cliConf.SetProject(uint(projID))
  175. return nil
  176. }
  177. func listAndSetCluster(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  178. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  179. s.Color("cyan")
  180. s.Suffix = " Loading list of clusters"
  181. s.Start()
  182. resp, err := client.ListProjectClusters(context.Background(), cliConf.Project)
  183. s.Stop()
  184. if err != nil {
  185. return err
  186. }
  187. var clusterID uint64
  188. if len(*resp) > 1 {
  189. clusterName, err := utils.PromptSelect("Select a cluster with ID", func() []string {
  190. var names []string
  191. for _, cluster := range *resp {
  192. names = append(names, fmt.Sprintf("%s - %d", cluster.Name, cluster.ID))
  193. }
  194. return names
  195. }())
  196. if err != nil {
  197. return err
  198. }
  199. clusterID, _ = strconv.ParseUint(strings.Split(clusterName, " - ")[1], 10, 64)
  200. } else {
  201. clusterID = uint64((*resp)[0].ID)
  202. }
  203. cliConf.SetCluster(uint(clusterID))
  204. return nil
  205. }
  206. func listAndSetRegistry(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  207. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  208. s.Color("cyan")
  209. s.Suffix = " Loading list of registries"
  210. s.Start()
  211. resp, err := client.ListRegistries(context.Background(), cliConf.Project)
  212. s.Stop()
  213. if err != nil {
  214. return err
  215. }
  216. var regID uint64
  217. if len(*resp) > 1 {
  218. regName, err := utils.PromptSelect("Select a registry with ID", func() []string {
  219. var names []string
  220. for _, cluster := range *resp {
  221. names = append(names, fmt.Sprintf("%s - %d", cluster.Name, cluster.ID))
  222. }
  223. return names
  224. }())
  225. if err != nil {
  226. return err
  227. }
  228. regID, _ = strconv.ParseUint(strings.Split(regName, " - ")[1], 10, 64)
  229. } else {
  230. regID = uint64((*resp)[0].ID)
  231. }
  232. cliConf.SetRegistry(uint(regID))
  233. return nil
  234. }