-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Stephan Wentz
committed
Feb 18, 2022
1 parent
9abab2c
commit e4d0f81
Showing
10 changed files
with
592 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.