Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registry commands #798

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/spf13/cobra"
)

// regCmd represents the reg command
var regCmd = &cobra.Command{
Use: "registry",
Aliases: []string{"reg"},
Short: `Describe and list container registries and tags`,
Long: ``,
PersistentPreRun: labPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 || len(args) > 2 {
cmd.Help()
return
}

regListCmd.Run(cmd, args)
},
}

func init() {
RootCmd.AddCommand(regCmd)
}
97 changes: 97 additions & 0 deletions cmd/registry_latest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/Masterminds/semver"
"sort"
"strconv"

"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var regLatestCmd = &cobra.Command{
Use: "latest",
Short: "Show latest tag",
Example: heredoc.Doc(`
lab reg latest
lab reg latest foo
lab reg latest 99`),
PersistentPreRun: labPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, registryName, err := parseArgsRemoteAndProject(args)
if err != nil {
log.Fatal(err)
}

num, err := strconv.Atoi(projectListConfig.Number)
if projectListConfig.All || (err != nil) {
num = -1
}

project, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
return
}

opt := gitlab.ListRegistryRepositoriesOptions{
ListOptions: gitlab.ListOptions{
PerPage: num,
},
Tags: gitlab.Bool(true),
TagsCount: gitlab.Bool(true),
}
registries, err := lab.ContainerRegistryList(project.ID, &opt, 0)
if err != nil {
log.Fatal(err)
return
}

var registry *gitlab.RegistryRepository
if len(registries) > 1 {
if registryName == "" {
log.Errorf("Found more than one registry, please specify")
regListCmd.Run(cmd, args)
return
}

registryId, err := strconv.Atoi(registryName)

for _, r := range registries {
if r.Path == registryName || (err == nil && r.ID == registryId) {
registry = r
}
}

if registry == nil {
log.Errorf("Registry %s not found", registryName)
return

}
} else {
registry = registries[0]
}

log.Debugf("Using registry %s (%d)\n", registry.Path, registry.ID)

vs := make([]*semver.Version, 0, len(registry.Tags))
for _, t := range registry.Tags {
v, err := semver.NewVersion(t.Name)
if err != nil {
//log.Warnf("Error parsing version: %s %s", err, t.Name)
continue
}
vs = append(vs, v)
}
//fmt.Printf("%+v\n", vs)
sort.Sort(semver.Collection(vs))
fmt.Println(vs[len(vs)-1].String())
},
}

func init() {
regCmd.AddCommand(regLatestCmd)
}
58 changes: 58 additions & 0 deletions cmd/registry_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"strconv"

"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var regListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List your registries",
Example: heredoc.Doc(`
lab reg list`),
PersistentPreRun: labPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, _, err := parseArgsRemoteAndProject(args)
if err != nil {
log.Fatal(err)
}

num, err := strconv.Atoi(projectListConfig.Number)
if projectListConfig.All || (err != nil) {
num = -1
}

project, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
return
}

opt := gitlab.ListRegistryRepositoriesOptions{
ListOptions: gitlab.ListOptions{
PerPage: num,
},
Tags: gitlab.Bool(true),
TagsCount: gitlab.Bool(true),
}
registries, err := lab.ContainerRegistryList(project.ID, &opt, 0)
if err != nil {
log.Fatal(err)
return
}

for _, r := range registries {
fmt.Printf("!%d %s\n", r.ID, r.Path)
}
},
}

func init() {
regCmd.AddCommand(regListCmd)
}
24 changes: 24 additions & 0 deletions cmd/registry_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func Test_registryList(t *testing.T) {
t.Parallel()
repo := copyTestRepo(t)
cmd := exec.Command(labBinaryPath, "registry", "list")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}
projects := strings.Split(string(b), "\n")
t.Log(projects)
require.Equal(t, "lab-testing/www-gitlab-com", projects[0])
}
92 changes: 92 additions & 0 deletions cmd/registry_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cmd

import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"strconv"

"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var regShowCmd = &cobra.Command{
Use: "show",
Short: "Describe registry",
Example: heredoc.Doc(`
lab reg show
lab reg show foo
lab reg show 99`),
PersistentPreRun: labPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, registryName, err := parseArgsRemoteAndProject(args)
if err != nil {
log.Fatal(err)
}

num, err := strconv.Atoi(projectListConfig.Number)
if projectListConfig.All || (err != nil) {
num = -1
}

project, _ := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
return
}

opt := gitlab.ListRegistryRepositoriesOptions{
ListOptions: gitlab.ListOptions{
PerPage: num,
},
Tags: gitlab.Bool(true),
TagsCount: gitlab.Bool(true),
}
registries, err := lab.ContainerRegistryList(project.ID, &opt, 0)
if err != nil {
log.Fatal(err)
}

var registry *gitlab.RegistryRepository
if len(registries) > 1 {
if registryName == "" {
log.Errorf("Found more than one registry, please specify")
regListCmd.Run(cmd, args)
return
}

registryId, err := strconv.Atoi(registryName)

for _, r := range registries {
if r.Path == registryName || (err == nil && r.ID == registryId) {
registry = r
}
}

if registry == nil {
log.Errorf("Registry %s not found", registryName)
return

}
} else {
registry = registries[0]
}

log.Debugf("Using registry %s (%d)\n", registry.Path, registry.ID)

//fmt.Printf("%+v\n", registry)
fmt.Printf("ID: %d\n", registry.ID)
fmt.Printf("Name: %s\n", registry.Name)
fmt.Printf("Path: %s\n", registry.Path)
fmt.Printf("Location: %s\n", registry.Location)
fmt.Printf("Created: %s\n", registry.CreatedAt)
fmt.Printf("# tags: %d\n", registry.TagsCount)
if registry.CleanupPolicyStartedAt != nil {
fmt.Printf("CleanupPolicy started at: %s\n", registry.CleanupPolicyStartedAt)
}
},
}

func init() {
regCmd.AddCommand(regShowCmd)
}
Loading