registry.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. func init() {
  27. rootCmd.AddCommand(registryCmd)
  28. registryCmd.PersistentFlags().UintVar(
  29. &registryID,
  30. "registry-id",
  31. 0,
  32. "id of the registry",
  33. )
  34. registryCmd.AddCommand(registryCmdListRepos)
  35. }
  36. func listRepos(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  37. pID := getProjectID()
  38. rID := getRegistryID()
  39. // get the list of namespaces
  40. repos, err := client.ListRegistryRepositories(
  41. context.Background(),
  42. pID,
  43. rID,
  44. )
  45. if err != nil {
  46. return err
  47. }
  48. w := new(tabwriter.Writer)
  49. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  50. fmt.Fprintf(w, "%s\t%s\n", "NAME", "CREATED_AT")
  51. for _, repo := range repos {
  52. fmt.Fprintf(w, "%s\t%s\n", repo.Name, repo.CreatedAt.String())
  53. }
  54. w.Flush()
  55. return nil
  56. }