Skip to content

Commit 01c2e8d

Browse files
author
Stephan Wentz
committed
Add registry commands
1 parent 25678df commit 01c2e8d

10 files changed

+592
-74
lines changed

cmd/registry.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// regCmd represents the reg command
8+
var regCmd = &cobra.Command{
9+
Use: "registry",
10+
Aliases: []string{"reg"},
11+
Short: `Describe and list container registries and tags`,
12+
Long: ``,
13+
PersistentPreRun: labPersistentPreRun,
14+
Run: func(cmd *cobra.Command, args []string) {
15+
if len(args) == 0 || len(args) > 2 {
16+
cmd.Help()
17+
return
18+
}
19+
20+
regListCmd.Run(cmd, args)
21+
},
22+
}
23+
24+
func init() {
25+
RootCmd.AddCommand(regCmd)
26+
}

cmd/registry_latest.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/MakeNowJust/heredoc/v2"
6+
"github.com/Masterminds/semver"
7+
"sort"
8+
"strconv"
9+
10+
"github.com/spf13/cobra"
11+
"github.com/xanzy/go-gitlab"
12+
lab "github.com/zaquestion/lab/internal/gitlab"
13+
)
14+
15+
var regLatestCmd = &cobra.Command{
16+
Use: "latest",
17+
Short: "Show latest tag",
18+
Example: heredoc.Doc(`
19+
lab reg latest
20+
lab reg latest foo
21+
lab reg latest 99`),
22+
PersistentPreRun: labPersistentPreRun,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
rn, registryName, err := parseArgsRemoteAndProject(args)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
num, err := strconv.Atoi(projectListConfig.Number)
30+
if projectListConfig.All || (err != nil) {
31+
num = -1
32+
}
33+
34+
project, err := lab.FindProject(rn)
35+
if err != nil {
36+
log.Fatal(err)
37+
return
38+
}
39+
40+
opt := gitlab.ListRegistryRepositoriesOptions{
41+
ListOptions: gitlab.ListOptions{
42+
PerPage: num,
43+
},
44+
Tags: gitlab.Bool(true),
45+
TagsCount: gitlab.Bool(true),
46+
}
47+
registries, err := lab.ContainerRegistryList(project.ID, &opt, 0)
48+
if err != nil {
49+
log.Fatal(err)
50+
return
51+
}
52+
53+
var registry *gitlab.RegistryRepository
54+
if len(registries) > 1 {
55+
if registryName == "" {
56+
log.Fatalf("Found more than one registry, please specify")
57+
regListCmd.Run(cmd, args)
58+
return
59+
}
60+
61+
registryId, err := strconv.Atoi(registryName)
62+
63+
for _, r := range registries {
64+
if r.Path == registryName || (err == nil && r.ID == registryId) {
65+
registry = r
66+
}
67+
}
68+
69+
if registry == nil {
70+
log.Fatalf("Registry %s not found", registryName)
71+
return
72+
73+
}
74+
} else {
75+
registry = registries[0]
76+
}
77+
78+
log.Debugf("Using registry %s (%d)\n", registry.Path, registry.ID)
79+
80+
vs := make([]*semver.Version, 0, len(registry.Tags))
81+
for _, t := range registry.Tags {
82+
v, err := semver.NewVersion(t.Name)
83+
if err != nil {
84+
//log.Warnf("Error parsing version: %s %s", err, t.Name)
85+
continue
86+
}
87+
vs = append(vs, v)
88+
}
89+
//fmt.Printf("%+v\n", vs)
90+
sort.Sort(semver.Collection(vs))
91+
fmt.Println(vs[len(vs)-1].String())
92+
},
93+
}
94+
95+
func init() {
96+
regCmd.AddCommand(regLatestCmd)
97+
}

cmd/registry_list.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/MakeNowJust/heredoc/v2"
6+
"strconv"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/xanzy/go-gitlab"
10+
lab "github.com/zaquestion/lab/internal/gitlab"
11+
)
12+
13+
var regListCmd = &cobra.Command{
14+
Use: "list",
15+
Aliases: []string{"ls"},
16+
Short: "List your registries",
17+
Example: heredoc.Doc(`
18+
lab reg list`),
19+
PersistentPreRun: labPersistentPreRun,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
rn, _, err := parseArgsRemoteAndProject(args)
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
26+
num, err := strconv.Atoi(projectListConfig.Number)
27+
if projectListConfig.All || (err != nil) {
28+
num = -1
29+
}
30+
31+
project, err := lab.FindProject(rn)
32+
if err != nil {
33+
log.Fatal(err)
34+
return
35+
}
36+
37+
opt := gitlab.ListRegistryRepositoriesOptions{
38+
ListOptions: gitlab.ListOptions{
39+
PerPage: num,
40+
},
41+
Tags: gitlab.Bool(true),
42+
TagsCount: gitlab.Bool(true),
43+
}
44+
registries, err := lab.ContainerRegistryList(project.ID, &opt, 0)
45+
if err != nil {
46+
log.Fatal(err)
47+
return
48+
}
49+
50+
for _, r := range registries {
51+
fmt.Printf("!%d %s\n", r.ID, r.Path)
52+
}
53+
},
54+
}
55+
56+
func init() {
57+
regCmd.AddCommand(regListCmd)
58+
}

cmd/registry_list_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmd
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_registryList(t *testing.T) {
12+
t.Parallel()
13+
repo := copyTestRepo(t)
14+
cmd := exec.Command(labBinaryPath, "registry", "list")
15+
cmd.Dir = repo
16+
17+
b, err := cmd.CombinedOutput()
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
projects := strings.Split(string(b), "\n")
22+
t.Log(projects)
23+
require.Equal(t, "lab-testing/www-gitlab-com", projects[0])
24+
}

cmd/registry_show.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/MakeNowJust/heredoc/v2"
6+
"strconv"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/xanzy/go-gitlab"
10+
lab "github.com/zaquestion/lab/internal/gitlab"
11+
)
12+
13+
var regShowCmd = &cobra.Command{
14+
Use: "show",
15+
Short: "Describe registry",
16+
Example: heredoc.Doc(`
17+
lab reg show
18+
lab reg show foo
19+
lab reg show 99`),
20+
PersistentPreRun: labPersistentPreRun,
21+
Run: func(cmd *cobra.Command, args []string) {
22+
rn, registryName, err := parseArgsRemoteAndProject(args)
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
27+
num, err := strconv.Atoi(projectListConfig.Number)
28+
if projectListConfig.All || (err != nil) {
29+
num = -1
30+
}
31+
32+
project, _ := lab.FindProject(rn)
33+
if err != nil {
34+
log.Fatal(err)
35+
return
36+
}
37+
38+
opt := gitlab.ListRegistryRepositoriesOptions{
39+
ListOptions: gitlab.ListOptions{
40+
PerPage: num,
41+
},
42+
Tags: gitlab.Bool(true),
43+
TagsCount: gitlab.Bool(true),
44+
}
45+
registries, err := lab.ContainerRegistryList(project.ID, &opt, 0)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
50+
var registry *gitlab.RegistryRepository
51+
if len(registries) > 1 {
52+
if registryName == "" {
53+
log.Fatalf("Found more than one registry, please specify")
54+
regListCmd.Run(cmd, args)
55+
return
56+
}
57+
58+
registryId, err := strconv.Atoi(registryName)
59+
60+
for _, r := range registries {
61+
if r.Path == registryName || (err == nil && r.ID == registryId) {
62+
registry = r
63+
}
64+
}
65+
66+
if registry == nil {
67+
log.Fatalf("Registry %s not found", registryName)
68+
return
69+
70+
}
71+
} else {
72+
registry = registries[0]
73+
}
74+
75+
log.Debugf("Using registry %s (%d)\n", registry.Path, registry.ID)
76+
77+
//fmt.Printf("%+v\n", registry)
78+
fmt.Printf("ID: %d\n", registry.ID)
79+
fmt.Printf("Name: %s\n", registry.Name)
80+
fmt.Printf("Path: %s\n", registry.Path)
81+
fmt.Printf("Location: %s\n", registry.Location)
82+
fmt.Printf("Created: %s\n", registry.CreatedAt)
83+
fmt.Printf("# tags: %d\n", registry.TagsCount)
84+
if registry.CleanupPolicyStartedAt != nil {
85+
fmt.Printf("CleanupPolicy started at: %s\n", registry.CleanupPolicyStartedAt)
86+
}
87+
},
88+
}
89+
90+
func init() {
91+
regCmd.AddCommand(regShowCmd)
92+
}

0 commit comments

Comments
 (0)