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

Global and organization registries #1672

Merged
merged 23 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
51 changes: 51 additions & 0 deletions cli/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
package registry

import (
"fmt"
"strconv"
"strings"

"github.com/urfave/cli/v2"

"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

// Command exports the registry command set.
Expand All @@ -30,3 +37,47 @@ var Command = &cli.Command{
registryListCmd,
},
}

func parseTargetArgs(client woodpecker.Client, c *cli.Context) (global bool, orgID, repoID int64, err error) {
if c.Bool("global") {
return true, -1, -1, nil
}

repoIDOrFullName := c.String("repository")
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
}

orgIDOrName := c.String("organization")
if orgIDOrName == "" && repoIDOrFullName == "" {
lafriks marked this conversation as resolved.
Show resolved Hide resolved
if err := cli.ShowSubcommandHelp(c); err != nil {
return false, -1, -1, err
}

return false, -1, -1, fmt.Errorf("missing arguments")
}

if orgIDOrName != "" && repoIDOrFullName == "" {
if orgID, err := strconv.ParseInt(orgIDOrName, 10, 64); err == nil {
return false, orgID, -1, nil
}

org, err := client.OrgLookup(orgIDOrName)
if err != nil {
return false, -1, -1, err
}

return false, org.ID, -1, nil
}

if orgIDOrName != "" && !strings.Contains(repoIDOrFullName, "/") {
repoIDOrFullName = orgIDOrName + "/" + repoIDOrFullName
}

repoID, err = internal.ParseRepo(client, repoIDOrFullName)
if err != nil {
return false, -1, -1, err
}

return false, -1, repoID, nil
}
36 changes: 25 additions & 11 deletions cli/registry/registry_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ var registryCreateCmd = &cli.Command{
ArgsUsage: "[repo-id|repo-full-name]",
Action: registryCreate,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Usage: "global registry",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Expand All @@ -50,22 +55,15 @@ var registryCreateCmd = &cli.Command{

func registryCreate(c *cli.Context) error {
var (
hostname = c.String("hostname")
username = c.String("username")
password = c.String("password")
repoIDOrFullName = c.String("repository")
hostname = c.String("hostname")
username = c.String("username")
password = c.String("password")
)
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
}

client, err := internal.NewClient(c)
if err != nil {
return err
}
repoID, err := internal.ParseRepo(client, repoIDOrFullName)
if err != nil {
return err
}
registry := &woodpecker.Registry{
Address: hostname,
Username: username,
Expand All @@ -79,6 +77,22 @@ func registryCreate(c *cli.Context) error {
}
registry.Password = string(out)
}

global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}

if global {
_, err = client.GlobalRegistryCreate(registry)
return err
}

if orgID != -1 {
_, err = client.OrgRegistryCreate(orgID, registry)
return err
}

if _, err := client.RegistryCreate(repoID, registry); err != nil {
return err
}
Expand Down
40 changes: 30 additions & 10 deletions cli/registry/registry_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var registryInfoCmd = &cli.Command{
Expand All @@ -30,6 +31,11 @@ var registryInfoCmd = &cli.Command{
ArgsUsage: "[repo-id|repo-full-name]",
Action: registryInfo,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Usage: "global registry",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Expand All @@ -42,25 +48,39 @@ var registryInfoCmd = &cli.Command{

func registryInfo(c *cli.Context) error {
var (
hostname = c.String("hostname")
repoIDOrFullName = c.String("repository")
format = c.String("format") + "\n"
hostname = c.String("hostname")
format = c.String("format") + "\n"
)
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
}

client, err := internal.NewClient(c)
if err != nil {
return err
}
repoID, err := internal.ParseRepo(client, repoIDOrFullName)

global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}
registry, err := client.Registry(repoID, hostname)
if err != nil {
return err

var registry *woodpecker.Registry
switch {
case global:
registry, err = client.GlobalRegistry(hostname)
if err != nil {
return err
}
case orgID != -1:
registry, err = client.Registry(orgID, hostname)
if err != nil {
return err
}
default:
registry, err = client.Registry(repoID, hostname)
if err != nil {
return err
}
}

tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
Expand Down
40 changes: 29 additions & 11 deletions cli/registry/registry_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"go.woodpecker-ci.org/woodpecker/v2/cli/common"
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
"go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker"
)

var registryListCmd = &cli.Command{
Expand All @@ -30,31 +31,48 @@ var registryListCmd = &cli.Command{
ArgsUsage: "[repo-id|repo-full-name]",
Action: registryList,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Usage: "global registry",
},
common.OrgFlag,
common.RepoFlag,
common.FormatFlag(tmplRegistryList, true),
},
}

func registryList(c *cli.Context) error {
var (
format = c.String("format") + "\n"
repoIDOrFullName = c.String("repository")
)
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
}
format := c.String("format") + "\n"

client, err := internal.NewClient(c)
if err != nil {
return err
}
repoID, err := internal.ParseRepo(client, repoIDOrFullName)

global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}
list, err := client.RegistryList(repoID)
if err != nil {
return err

var list []*woodpecker.Registry
switch {
case global:
list, err = client.GlobalRegistryList()
if err != nil {
return err
}
case orgID != -1:
list, err = client.OrgRegistryList(orgID)
if err != nil {
return err
}
default:
list, err = client.RegistryList(repoID)
if err != nil {
return err
}
}

tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
Expand Down
24 changes: 16 additions & 8 deletions cli/registry/registry_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ var registryDeleteCmd = &cli.Command{
ArgsUsage: "[repo-id|repo-full-name]",
Action: registryDelete,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Usage: "global registry",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Expand All @@ -37,20 +42,23 @@ var registryDeleteCmd = &cli.Command{
}

func registryDelete(c *cli.Context) error {
var (
hostname = c.String("hostname")
repoIDOrFullName = c.String("repository")
)
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
}
hostname := c.String("hostname")

client, err := internal.NewClient(c)
if err != nil {
return err
}
repoID, err := internal.ParseRepo(client, repoIDOrFullName)

global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}

if global {
return client.GlobalRegistryDelete(hostname)
}
if orgID != -1 {
return client.OrgRegistryDelete(orgID, hostname)
}
return client.RegistryDelete(repoID, hostname)
}
35 changes: 24 additions & 11 deletions cli/registry/registry_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ var registryUpdateCmd = &cli.Command{
ArgsUsage: "[repo-id|repo-full-name]",
Action: registryUpdate,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "global",
Usage: "global registry",
},
common.OrgFlag,
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Expand All @@ -50,22 +55,16 @@ var registryUpdateCmd = &cli.Command{

func registryUpdate(c *cli.Context) error {
var (
hostname = c.String("hostname")
username = c.String("username")
password = c.String("password")
repoIDOrFullName = c.String("repository")
hostname = c.String("hostname")
username = c.String("username")
password = c.String("password")
)
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
}

client, err := internal.NewClient(c)
if err != nil {
return err
}
repoID, err := internal.ParseRepo(client, repoIDOrFullName)
if err != nil {
return err
}

registry := &woodpecker.Registry{
Address: hostname,
Username: username,
Expand All @@ -79,6 +78,20 @@ func registryUpdate(c *cli.Context) error {
}
registry.Password = string(out)
}

global, orgID, repoID, err := parseTargetArgs(client, c)
if err != nil {
return err
}

if global {
_, err = client.GlobalRegistryUpdate(registry)
return err
}
if orgID != -1 {
_, err = client.OrgRegistryUpdate(orgID, registry)
return err
}
_, err = client.RegistryUpdate(repoID, registry)
return err
}
Loading