registry.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "text/tabwriter"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. "github.com/spf13/cobra"
  9. )
  10. // registryCmd represents the "porter registry" base command when called
  11. // without any subcommands
  12. var registryCmd = &cobra.Command{
  13. Use: "registry",
  14. Short: "Commands that read from a connected registry",
  15. }
  16. var registryCmdListRepos = &cobra.Command{
  17. Use: "repos list",
  18. Short: "Lists the repositories in a registry",
  19. Run: func(cmd *cobra.Command, args []string) {
  20. err := checkLoginAndRun(args, listRepos)
  21. if err != nil {
  22. os.Exit(1)
  23. }
  24. },
  25. }
  26. var registryCmdListImages = &cobra.Command{
  27. Use: "images list [REPO_NAME]",
  28. Short: "Lists the images in an image repository",
  29. Run: func(cmd *cobra.Command, args []string) {
  30. err := checkLoginAndRun(args, listImages)
  31. if err != nil {
  32. os.Exit(1)
  33. }
  34. },
  35. }
  36. func init() {
  37. rootCmd.AddCommand(registryCmd)
  38. registryCmd.PersistentFlags().UintVar(
  39. &registryID,
  40. "registry-id",
  41. 0,
  42. "id of the registry",
  43. )
  44. registryCmd.AddCommand(registryCmdListRepos)
  45. registryCmd.AddCommand(registryCmdListImages)
  46. }
  47. func listRepos(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  48. pID := getProjectID()
  49. rID := getRegistryID()
  50. // get the list of namespaces
  51. repos, err := client.ListRegistryRepositories(
  52. context.Background(),
  53. pID,
  54. rID,
  55. )
  56. if err != nil {
  57. return err
  58. }
  59. w := new(tabwriter.Writer)
  60. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  61. fmt.Fprintf(w, "%s\t%s\n", "NAME", "CREATED_AT")
  62. for _, repo := range repos {
  63. fmt.Fprintf(w, "%s\t%s\n", repo.Name, repo.CreatedAt.String())
  64. }
  65. w.Flush()
  66. return nil
  67. }
  68. func listImages(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  69. pID := getProjectID()
  70. rID := getRegistryID()
  71. repoName := args[1]
  72. // get the list of namespaces
  73. imgs, err := client.ListImages(
  74. context.Background(),
  75. pID,
  76. rID,
  77. repoName,
  78. )
  79. if err != nil {
  80. return err
  81. }
  82. w := new(tabwriter.Writer)
  83. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  84. fmt.Fprintf(w, "%s\t%s\n", "IMAGE", "DIGEST")
  85. for _, img := range imgs {
  86. fmt.Fprintf(w, "%s\t%s\n", repoName+":"+img.Tag, img.Digest)
  87. }
  88. w.Flush()
  89. return nil
  90. }