registry.go 5.1 KB

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