config.go 7.0 KB

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