registry.go 4.8 KB

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