2018-03-06 09:12:29 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-06-17 15:41:58 -04:00
|
|
|
"sort"
|
2018-03-06 09:12:29 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var listCommand = cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Aliases: []string{"ls"},
|
|
|
|
Usage: "list all repositories",
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-06-17 15:41:58 -04:00
|
|
|
if len(c.Args()) < 1 {
|
|
|
|
return fmt.Errorf("pass the domain of the registry")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the registry client.
|
|
|
|
r, err := createRegistryClient(c, c.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-06 09:12:29 -05:00
|
|
|
// Get the repositories via catalog.
|
|
|
|
repos, err := r.Catalog("")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-17 15:41:58 -04:00
|
|
|
sort.Strings(repos)
|
2018-03-06 09:12:29 -05:00
|
|
|
|
2018-06-17 15:41:58 -04:00
|
|
|
fmt.Printf("Repositories for %s\n", r.Domain)
|
2018-03-06 09:12:29 -05:00
|
|
|
|
|
|
|
var (
|
2018-06-18 09:53:30 -04:00
|
|
|
l sync.Mutex
|
2018-06-17 15:41:58 -04:00
|
|
|
wg sync.WaitGroup
|
|
|
|
repoTags = map[string][]string{}
|
2018-03-06 09:12:29 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
wg.Add(len(repos))
|
|
|
|
for _, repo := range repos {
|
|
|
|
go func(repo string) {
|
2018-06-17 15:41:58 -04:00
|
|
|
// Get the tags.
|
2018-03-06 09:12:29 -05:00
|
|
|
tags, err := r.Tags(repo)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Get tags of [%s] error: %s", repo, err)
|
|
|
|
}
|
2018-06-17 15:41:58 -04:00
|
|
|
// Sort the tags
|
|
|
|
sort.Strings(tags)
|
2018-06-18 09:53:30 -04:00
|
|
|
|
|
|
|
// Lock on the write to the map.
|
|
|
|
l.Lock()
|
2018-06-17 15:41:58 -04:00
|
|
|
repoTags[repo] = tags
|
2018-06-18 09:53:30 -04:00
|
|
|
l.Unlock()
|
2018-03-06 09:12:29 -05:00
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}(repo)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-06-17 15:41:58 -04:00
|
|
|
// Setup the tab writer.
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
|
|
|
|
|
|
|
|
// Print header.
|
|
|
|
fmt.Fprintln(w, "REPO\tTAGS")
|
|
|
|
|
|
|
|
// Sort the repos.
|
|
|
|
for _, repo := range repos {
|
|
|
|
w.Write([]byte(fmt.Sprintf("%s\t%s\n", repo, strings.Join(repoTags[repo], ", "))))
|
|
|
|
}
|
|
|
|
|
2018-03-06 09:12:29 -05:00
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|