registry.go 5.3 KB

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