registry.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "text/tabwriter"
  9. "github.com/fatih/color"
  10. api "github.com/porter-dev/porter/api/client"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/cli/cmd/utils"
  13. "github.com/spf13/cobra"
  14. )
  15. // registryCmd represents the "porter registry" base command when called
  16. // without any subcommands
  17. var registryCmd = &cobra.Command{
  18. Use: "registry",
  19. Aliases: []string{"registries"},
  20. Short: "Commands that read from a connected registry",
  21. }
  22. var registryListCmd = &cobra.Command{
  23. Use: "list",
  24. Short: "Lists the registries linked to a project",
  25. Run: func(cmd *cobra.Command, args []string) {
  26. err := checkLoginAndRun(args, listRegistries)
  27. if err != nil {
  28. os.Exit(1)
  29. }
  30. },
  31. }
  32. var registryDeleteCmd = &cobra.Command{
  33. Use: "delete [id]",
  34. Args: cobra.ExactArgs(1),
  35. Short: "Deletes the registry with the given id",
  36. Run: func(cmd *cobra.Command, args []string) {
  37. err := checkLoginAndRun(args, deleteRegistry)
  38. if err != nil {
  39. os.Exit(1)
  40. }
  41. },
  42. }
  43. var registryReposCmd = &cobra.Command{
  44. Use: "repo",
  45. Aliases: []string{"repos", "repository", "repositories"},
  46. Short: "Commands that perform operations on image registry repositories",
  47. }
  48. var registryReposListCmd = &cobra.Command{
  49. Use: "list",
  50. Short: "Lists the repositories in an image registry",
  51. Run: func(cmd *cobra.Command, args []string) {
  52. err := checkLoginAndRun(args, listRepos)
  53. if err != nil {
  54. os.Exit(1)
  55. }
  56. },
  57. }
  58. var registryImageCmd = &cobra.Command{
  59. Use: "image",
  60. Aliases: []string{"images"},
  61. Short: "Commands that perform operations on image in a repository",
  62. }
  63. var registryImageListCmd = &cobra.Command{
  64. Use: "list [repo_name]",
  65. Args: cobra.ExactArgs(1),
  66. Short: "Lists the images the specified image repository",
  67. Run: func(cmd *cobra.Command, args []string) {
  68. err := checkLoginAndRun(args, listImages)
  69. if err != nil {
  70. os.Exit(1)
  71. }
  72. },
  73. }
  74. func init() {
  75. rootCmd.AddCommand(registryCmd)
  76. registryCmd.PersistentFlags().AddFlagSet(utils.RegistryFlagSet)
  77. registryCmd.AddCommand(registryReposCmd)
  78. registryCmd.AddCommand(registryListCmd)
  79. registryCmd.AddCommand(registryDeleteCmd)
  80. registryReposCmd.AddCommand(registryReposListCmd)
  81. registryCmd.AddCommand(registryImageCmd)
  82. registryImageCmd.AddCommand(registryImageListCmd)
  83. }
  84. func listRegistries(user *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  85. pID := cliConf.Project
  86. // get the list of namespaces
  87. resp, err := client.ListRegistries(
  88. context.Background(),
  89. pID,
  90. )
  91. if err != nil {
  92. return err
  93. }
  94. registries := *resp
  95. w := new(tabwriter.Writer)
  96. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  97. fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "URL", "SERVICE")
  98. currRegistryID := cliConf.Registry
  99. for _, registry := range registries {
  100. if currRegistryID == registry.ID {
  101. color.New(color.FgGreen).Fprintf(w, "%d\t%s\t%s (current registry)\n", registry.ID, registry.URL, registry.Service)
  102. } else {
  103. fmt.Fprintf(w, "%d\t%s\t%s\n", registry.ID, registry.URL, registry.Service)
  104. }
  105. }
  106. w.Flush()
  107. return nil
  108. }
  109. func deleteRegistry(user *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  110. userResp, err := utils.PromptPlaintext(
  111. fmt.Sprintf(
  112. `Are you sure you'd like to delete the registry with id %s? %s `,
  113. args[0],
  114. color.New(color.FgCyan).Sprintf("[y/n]"),
  115. ),
  116. )
  117. if err != nil {
  118. return err
  119. }
  120. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  121. id, err := strconv.ParseUint(args[0], 10, 64)
  122. if err != nil {
  123. return err
  124. }
  125. err = client.DeleteProjectRegistry(context.Background(), cliConf.Project, uint(id))
  126. if err != nil {
  127. return err
  128. }
  129. color.New(color.FgGreen).Printf("Deleted registry with id %d\n", id)
  130. }
  131. return nil
  132. }
  133. func listRepos(user *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  134. pID := cliConf.Project
  135. rID := cliConf.Registry
  136. // get the list of namespaces
  137. resp, err := client.ListRegistryRepositories(
  138. context.Background(),
  139. pID,
  140. rID,
  141. )
  142. if err != nil {
  143. return err
  144. }
  145. repos := *resp
  146. w := new(tabwriter.Writer)
  147. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  148. fmt.Fprintf(w, "%s\t%s\n", "NAME", "CREATED_AT")
  149. for _, repo := range repos {
  150. fmt.Fprintf(w, "%s\t%s\n", repo.Name, repo.CreatedAt.String())
  151. }
  152. w.Flush()
  153. return nil
  154. }
  155. func listImages(user *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  156. pID := cliConf.Project
  157. rID := cliConf.Registry
  158. repoName := args[0]
  159. // get the list of namespaces
  160. resp, err := client.ListImages(
  161. context.Background(),
  162. pID,
  163. rID,
  164. repoName,
  165. )
  166. if err != nil {
  167. return err
  168. }
  169. imgs := *resp
  170. w := new(tabwriter.Writer)
  171. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  172. fmt.Fprintf(w, "%s\t%s\n", "IMAGE", "DIGEST")
  173. for _, img := range imgs {
  174. fmt.Fprintf(w, "%s\t%s\n", repoName+":"+img.Tag, img.Digest)
  175. }
  176. w.Flush()
  177. return nil
  178. }