registry.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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().AddFlagSet(registryFlagSet)
  76. registryCmd.AddCommand(registryReposCmd)
  77. registryCmd.AddCommand(registryListCmd)
  78. registryCmd.AddCommand(registryDeleteCmd)
  79. registryReposCmd.AddCommand(registryReposListCmd)
  80. registryCmd.AddCommand(registryImageCmd)
  81. registryImageCmd.AddCommand(registryImageListCmd)
  82. }
  83. func listRegistries(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  84. pID := config.Project
  85. // get the list of namespaces
  86. registries, err := client.ListRegistries(
  87. context.Background(),
  88. pID,
  89. )
  90. if err != nil {
  91. return err
  92. }
  93. w := new(tabwriter.Writer)
  94. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  95. fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "URL", "SERVICE")
  96. currRegistryID := config.Registry
  97. for _, registry := range registries {
  98. if currRegistryID == registry.ID {
  99. color.New(color.FgGreen).Fprintf(w, "%d\t%s\t%s (current registry)\n", registry.ID, registry.URL, registry.Service)
  100. } else {
  101. fmt.Fprintf(w, "%d\t%s\t%s\n", registry.ID, registry.URL, registry.Service)
  102. }
  103. }
  104. w.Flush()
  105. return nil
  106. }
  107. func deleteRegistry(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  108. userResp, err := utils.PromptPlaintext(
  109. fmt.Sprintf(
  110. `Are you sure you'd like to delete the registry with id %s? %s `,
  111. args[0],
  112. color.New(color.FgCyan).Sprintf("[y/n]"),
  113. ),
  114. )
  115. if err != nil {
  116. return err
  117. }
  118. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  119. id, err := strconv.ParseUint(args[0], 10, 64)
  120. if err != nil {
  121. return err
  122. }
  123. err = client.DeleteProjectRegistry(context.Background(), config.Project, uint(id))
  124. if err != nil {
  125. return err
  126. }
  127. color.New(color.FgGreen).Printf("Deleted registry with id %d\n", id)
  128. }
  129. return nil
  130. }
  131. func listRepos(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  132. pID := config.Project
  133. rID := config.Registry
  134. // get the list of namespaces
  135. repos, err := client.ListRegistryRepositories(
  136. context.Background(),
  137. pID,
  138. rID,
  139. )
  140. if err != nil {
  141. return err
  142. }
  143. w := new(tabwriter.Writer)
  144. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  145. fmt.Fprintf(w, "%s\t%s\n", "NAME", "CREATED_AT")
  146. for _, repo := range repos {
  147. fmt.Fprintf(w, "%s\t%s\n", repo.Name, repo.CreatedAt.String())
  148. }
  149. w.Flush()
  150. return nil
  151. }
  152. func listImages(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  153. pID := config.Project
  154. rID := config.Registry
  155. repoName := args[0]
  156. // get the list of namespaces
  157. imgs, err := client.ListImages(
  158. context.Background(),
  159. pID,
  160. rID,
  161. repoName,
  162. )
  163. if err != nil {
  164. return err
  165. }
  166. w := new(tabwriter.Writer)
  167. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  168. fmt.Fprintf(w, "%s\t%s\n", "IMAGE", "DIGEST")
  169. for _, img := range imgs {
  170. fmt.Fprintf(w, "%s\t%s\n", repoName+":"+img.Tag, img.Digest)
  171. }
  172. w.Flush()
  173. return nil
  174. }