From 5046a963529b506da442f358bc22b9ecd4d09037 Mon Sep 17 00:00:00 2001 From: Piotr Kazmierczak <470696+pkazmierczak@users.noreply.github.com> Date: Wed, 10 May 2023 16:52:04 +0200 Subject: [PATCH] separate pack list and registry list commands --- internal/cli/helpers.go | 47 ++++++++++-------- internal/cli/list.go | 92 +++++++++++++++++++++++++++++++++++ internal/cli/main.go | 5 ++ internal/cli/registry_list.go | 15 ++---- 4 files changed, 128 insertions(+), 31 deletions(-) create mode 100644 internal/cli/list.go diff --git a/internal/cli/helpers.go b/internal/cli/helpers.go index 7d6dbfa..e9ae18e 100644 --- a/internal/cli/helpers.go +++ b/internal/cli/helpers.go @@ -48,36 +48,27 @@ func generatePackManager(c *baseCommand, client *v1.Client, packCfg *cache.PackC } func registryTable() *terminal.Table { - return terminal.NewTable("PACK NAME", "REF", "LOCAL_REF", "METADATA VERSION", "REGISTRY", "REGISTRY_URL") + return terminal.NewTable("REGISTRY NAME", "REF", "LOCAL_REF", "REGISTRY_URL") } -func emptyRegistryTableRow(cachedRegistry *cache.Registry) []terminal.TableEntry { +func packTable() *terminal.Table { + return terminal.NewTable("PACK NAME", "METADATA VERSION", "REGISTRY NAME") +} + +func registryTableRow(cachedRegistry *cache.Registry) []terminal.TableEntry { return []terminal.TableEntry{ - // blank pack name - { - Value: "", - }, - // blank revision { - Value: "", - }, - // blank local ref - { - Value: "", + Value: cachedRegistry.Name, }, - // blank metadata version { - Value: "", + Value: cachedRegistry.Ref, }, - // CachedRegistry name - user defined alias or registry URL slug { - Value: cachedRegistry.Name, + Value: cachedRegistry.LocalRef, }, - // The cachedRegistry URL from where the registryPack was cloned { Value: cachedRegistry.Source, }, - // TODO: The app version } } @@ -99,7 +90,7 @@ func registryPackRow(cachedRegistry *cache.Registry, cachedPack *cache.Pack) []t { Value: cachedPack.Metadata.Pack.Version, }, - // CachedRegistry name - user defined alias or registry URL slug + // CachedRegistry name user defined alias or registry URL slug { Value: cachedRegistry.Name, }, @@ -111,6 +102,24 @@ func registryPackRow(cachedRegistry *cache.Registry, cachedPack *cache.Pack) []t } } +func packRow(cachedRegistry *cache.Registry, cachedPack *cache.Pack) []terminal.TableEntry { + return []terminal.TableEntry{ + // The Name of the registryPack + { + Value: cachedPack.Name(), + }, + // The metadata version + { + Value: cachedPack.Metadata.Pack.Version, + }, + // CachedRegistry name user defined alias or registry URL slug + { + Value: cachedRegistry.Name, + }, + // TODO: The app version + } +} + // TODO: This needs to be on a domain specific pkg rather than a UI helpers file. // This will be possible once we create a logger interface that can be passed // between layers. diff --git a/internal/cli/list.go b/internal/cli/list.go new file mode 100644 index 0000000..aa87f1e --- /dev/null +++ b/internal/cli/list.go @@ -0,0 +1,92 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package cli + +import ( + "github.com/hashicorp/nomad-pack/internal/pkg/cache" + "github.com/hashicorp/nomad-pack/internal/pkg/flag" + "github.com/posener/complete" +) + +// ListCommand lists all registries and pack that have been downloaded +// to the current machine. +type ListCommand struct { + *baseCommand +} + +func (c *ListCommand) Run(args []string) int { + c.cmdKey = "registry list" + // Initialize. If we fail, we just exit since Init handles the UI. + if err := c.Init( + WithNoArgs(args), + WithNoConfig(), + WithClient(false), + ); err != nil { + return 1 + } + + // Get the global cache dir - may be configurable in the future, so using this + // helper function rather than a direct reference to the CONST. + globalCache, err := cache.NewCache(&cache.CacheConfig{ + Path: cache.DefaultCachePath(), + Logger: c.ui, + }) + if err != nil { + return 1 + } + + // Load the list of registries. + err = globalCache.Load() + if err != nil { + return 1 + } + + // Initialize a table for a nice glint UI rendering + table := packTable() + + // Iterate over the registries and build a table row for each cachedRegistry/pack + // entry at each ref. Hierarchically, this should equate to the default + // cachedRegistry and all its peers. + for _, cachedRegistry := range globalCache.Registries() { + for _, registryPack := range cachedRegistry.Packs { + tableRow := packRow(cachedRegistry, registryPack) + // append table row + table.Rows = append(table.Rows, tableRow) + } + } + + // Display output table + c.ui.Table(table) + + return 0 +} + +func (c *ListCommand) Flags() *flag.Sets { + return c.flagSet(0, nil) +} + +func (c *ListCommand) AutocompleteArgs() complete.Predictor { + return complete.PredictNothing +} + +func (c *ListCommand) AutocompleteFlags() complete.Flags { + return c.Flags().Completions() +} + +func (c *ListCommand) Synopsis() string { + return "List packs available in the local environment." +} + +func (c *ListCommand) Help() string { + c.Example = ` + # List all available packs + nomad-pack list + ` + return formatHelp(` + Usage: nomad-pack list + + List nomad packs. + +` + c.GetExample() + c.Flags().Help()) +} diff --git a/internal/cli/main.go b/internal/cli/main.go index 893c5cc..3da7b50 100644 --- a/internal/cli/main.go +++ b/internal/cli/main.go @@ -142,6 +142,11 @@ func Commands( baseCommand: baseCommand, }, nil }, + "list": func() (cli.Command, error) { + return &ListCommand{ + baseCommand: baseCommand, + }, nil + }, "stop": func() (cli.Command, error) { return &StopCommand{ baseCommand: baseCommand, diff --git a/internal/cli/registry_list.go b/internal/cli/registry_list.go index 0b426cf..4be4cfc 100644 --- a/internal/cli/registry_list.go +++ b/internal/cli/registry_list.go @@ -48,19 +48,10 @@ func (c *RegistryListCommand) Run(args []string) int { // Iterate over the registries and build a table row for each cachedRegistry/pack // entry at each ref. Hierarchically, this should equate to the default // cachedRegistry and all its peers. - for _, cachedRegistry := range globalCache.Registries() { - // If no packs, just show registry. - if cachedRegistry.Packs == nil || len(cachedRegistry.Packs) == 0 { - tableRow := emptyRegistryTableRow(cachedRegistry) - // append table row + if len(globalCache.Registries()) >= 0 { + for _, registry := range globalCache.Registries() { + tableRow := registryTableRow(registry) table.Rows = append(table.Rows, tableRow) - } else { - // Show registry/pack combo for each pack. - for _, registryPack := range cachedRegistry.Packs { - tableRow := registryPackRow(cachedRegistry, registryPack) - // append table row - table.Rows = append(table.Rows, tableRow) - } } }