Skip to content

Commit

Permalink
separate pack list and registry list commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pkazmierczak committed May 22, 2023
1 parent c02eac9 commit 55265c0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 31 deletions.
47 changes: 28 additions & 19 deletions internal/cli/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,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
}
}

Expand All @@ -98,7 +89,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,
},
Expand All @@ -110,6 +101,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.
Expand Down
92 changes: 92 additions & 0 deletions internal/cli/list.go
Original file line number Diff line number Diff line change
@@ -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())
}
5 changes: 5 additions & 0 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 3 additions & 12 deletions internal/cli/registry_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit 55265c0

Please sign in to comment.