Add cli support for global and organization secrets (#1113)

* Add cli support for global and organization secrets

* Fix usage of deprecated functions
This commit is contained in:
Lauris BH 2022-08-15 21:10:13 +03:00 committed by GitHub
parent ec6490e367
commit 366a1e18f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 189 additions and 64 deletions

View file

@ -1,9 +1,12 @@
package secret package secret
import ( import (
"strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common" "github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
) )
// Command exports the secret command. // Command exports the secret command.
@ -19,3 +22,25 @@ var Command = &cli.Command{
secretListCmd, secretListCmd,
}, },
} }
func parseTargetArgs(c *cli.Context) (global bool, owner, name string, err error) {
if c.Bool("global") {
return true, "", "", nil
}
orgName := c.String("organization")
repoName := c.String("repository")
if orgName == "" && repoName == "" {
repoName = c.Args().First()
}
if orgName == "" && !strings.Contains(repoName, "/") {
orgName = repoName
}
if orgName != "" {
return false, orgName, "", err
}
owner, name, err = internal.ParseRepo(repoName)
if err != nil {
return false, "", "", err
}
return false, owner, name, nil
}

View file

@ -1,7 +1,7 @@
package secret package secret
import ( import (
"io/ioutil" "os"
"strings" "strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -14,9 +14,17 @@ import (
var secretCreateCmd = &cli.Command{ var secretCreateCmd = &cli.Command{
Name: "add", Name: "add",
Usage: "adds a secret", Usage: "adds a secret",
ArgsUsage: "[repo/name]", ArgsUsage: "[org/repo|org]",
Action: secretCreate, Action: secretCreate,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.BoolFlag{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "repository", Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)", Usage: "repository name (e.g. octocat/hello-world)",
@ -41,14 +49,6 @@ var secretCreateCmd = &cli.Command{
} }
func secretCreate(c *cli.Context) error { func secretCreate(c *cli.Context) error {
reponame := c.String("repository")
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c) client, err := internal.NewClient(c)
if err != nil { if err != nil {
return err return err
@ -64,13 +64,27 @@ func secretCreate(c *cli.Context) error {
} }
if strings.HasPrefix(secret.Value, "@") { if strings.HasPrefix(secret.Value, "@") {
path := strings.TrimPrefix(secret.Value, "@") path := strings.TrimPrefix(secret.Value, "@")
out, ferr := ioutil.ReadFile(path) out, err := os.ReadFile(path)
if ferr != nil { if err != nil {
return ferr return err
} }
secret.Value = string(out) secret.Value = string(out)
} }
_, err = client.SecretCreate(owner, name, secret)
global, owner, repo, err := parseTargetArgs(c)
if err != nil {
return err
}
if global {
_, err = client.GlobalSecretCreate(secret)
return err
}
if repo == "" {
_, err = client.OrgSecretCreate(owner, secret)
return err
}
_, err = client.SecretCreate(owner, repo, secret)
return err return err
} }

View file

@ -8,14 +8,23 @@ import (
"github.com/woodpecker-ci/woodpecker/cli/common" "github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal" "github.com/woodpecker-ci/woodpecker/cli/internal"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
) )
var secretInfoCmd = &cli.Command{ var secretInfoCmd = &cli.Command{
Name: "info", Name: "info",
Usage: "display secret info", Usage: "display secret info",
ArgsUsage: "[repo/name]", ArgsUsage: "[org/repo|org]",
Action: secretInfo, Action: secretInfo,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.BoolFlag{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "repository", Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)", Usage: "repository name (e.g. octocat/hello-world)",
@ -31,24 +40,36 @@ var secretInfoCmd = &cli.Command{
func secretInfo(c *cli.Context) error { func secretInfo(c *cli.Context) error {
var ( var (
secretName = c.String("name") secretName = c.String("name")
repoName = c.String("repository")
format = c.String("format") + "\n" format = c.String("format") + "\n"
) )
if repoName == "" {
repoName = c.Args().First()
}
owner, name, err := internal.ParseRepo(repoName)
if err != nil {
return err
}
client, err := internal.NewClient(c) client, err := internal.NewClient(c)
if err != nil { if err != nil {
return err return err
} }
secret, err := client.Secret(owner, name, secretName)
global, owner, repo, err := parseTargetArgs(c)
if err != nil { if err != nil {
return err return err
} }
var secret *woodpecker.Secret
if global {
secret, err = client.GlobalSecret(secretName)
if err != nil {
return err
}
} else if repo == "" {
secret, err = client.OrgSecret(owner, secretName)
if err != nil {
return err
}
} else {
secret, err = client.Secret(owner, repo, secretName)
if err != nil {
return err
}
}
tmpl, err := template.New("_").Funcs(secretFuncMap).Parse(format) tmpl, err := template.New("_").Funcs(secretFuncMap).Parse(format)
if err != nil { if err != nil {
return err return err

View file

@ -9,14 +9,23 @@ import (
"github.com/woodpecker-ci/woodpecker/cli/common" "github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal" "github.com/woodpecker-ci/woodpecker/cli/internal"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
) )
var secretListCmd = &cli.Command{ var secretListCmd = &cli.Command{
Name: "ls", Name: "ls",
Usage: "list secrets", Usage: "list secrets",
ArgsUsage: "[repo/name]", ArgsUsage: "[org/name|org]",
Action: secretList, Action: secretList,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.BoolFlag{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "repository", Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)", Usage: "repository name (e.g. octocat/hello-world)",
@ -26,25 +35,36 @@ var secretListCmd = &cli.Command{
} }
func secretList(c *cli.Context) error { func secretList(c *cli.Context) error {
var ( format := c.String("format") + "\n"
format = c.String("format") + "\n"
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c) client, err := internal.NewClient(c)
if err != nil { if err != nil {
return err return err
} }
list, err := client.SecretList(owner, name)
global, owner, repo, err := parseTargetArgs(c)
if err != nil { if err != nil {
return err return err
} }
var list []*woodpecker.Secret
if global {
list, err = client.GlobalSecretList()
if err != nil {
return err
}
} else if repo == "" {
list, err = client.OrgSecretList(owner)
if err != nil {
return err
}
} else {
list, err = client.SecretList(owner, repo)
if err != nil {
return err
}
}
tmpl, err := template.New("_").Funcs(secretFuncMap).Parse(format) tmpl, err := template.New("_").Funcs(secretFuncMap).Parse(format)
if err != nil { if err != nil {
return err return err

View file

@ -10,9 +10,17 @@ import (
var secretDeleteCmd = &cli.Command{ var secretDeleteCmd = &cli.Command{
Name: "rm", Name: "rm",
Usage: "remove a secret", Usage: "remove a secret",
ArgsUsage: "[repo/name]", ArgsUsage: "[org/repo|org]",
Action: secretDelete, Action: secretDelete,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.BoolFlag{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "repository", Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)", Usage: "repository name (e.g. octocat/hello-world)",
@ -25,20 +33,23 @@ var secretDeleteCmd = &cli.Command{
} }
func secretDelete(c *cli.Context) error { func secretDelete(c *cli.Context) error {
var ( secretName := c.String("name")
secret = c.String("name")
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c) client, err := internal.NewClient(c)
if err != nil { if err != nil {
return err return err
} }
return client.SecretDelete(owner, name, secret)
global, owner, repo, err := parseTargetArgs(c)
if err != nil {
return err
}
if global {
return client.GlobalSecretDelete(secretName)
}
if repo == "" {
return client.OrgSecretDelete(owner, secretName)
}
return client.SecretDelete(owner, repo, secretName)
} }

View file

@ -1,7 +1,7 @@
package secret package secret
import ( import (
"io/ioutil" "os"
"strings" "strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -14,9 +14,17 @@ import (
var secretUpdateCmd = &cli.Command{ var secretUpdateCmd = &cli.Command{
Name: "update", Name: "update",
Usage: "update a secret", Usage: "update a secret",
ArgsUsage: "[repo/name]", ArgsUsage: "[org/repo|org]",
Action: secretUpdate, Action: secretUpdate,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.BoolFlag{
Name: "global",
Usage: "global secret",
},
&cli.StringFlag{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "repository", Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)", Usage: "repository name (e.g. octocat/hello-world)",
@ -41,14 +49,6 @@ var secretUpdateCmd = &cli.Command{
} }
func secretUpdate(c *cli.Context) error { func secretUpdate(c *cli.Context) error {
reponame := c.String("repository")
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c) client, err := internal.NewClient(c)
if err != nil { if err != nil {
return err return err
@ -61,12 +61,26 @@ func secretUpdate(c *cli.Context) error {
} }
if strings.HasPrefix(secret.Value, "@") { if strings.HasPrefix(secret.Value, "@") {
path := strings.TrimPrefix(secret.Value, "@") path := strings.TrimPrefix(secret.Value, "@")
out, ferr := ioutil.ReadFile(path) out, err := os.ReadFile(path)
if ferr != nil { if err != nil {
return ferr return err
} }
secret.Value = string(out) secret.Value = string(out)
} }
_, err = client.SecretUpdate(owner, name, secret)
global, owner, repo, err := parseTargetArgs(c)
if err != nil {
return err
}
if global {
_, err = client.GlobalSecretUpdate(secret)
return err
}
if repo == "" {
_, err = client.OrgSecretUpdate(owner, secret)
return err
}
_, err = client.SecretUpdate(owner, repo, secret)
return err return err
} }

View file

@ -478,6 +478,10 @@ adds a secret
**--name**="": secret name **--name**="": secret name
**--global**: global secret
**--organization**="": organization name (e.g. octocat)
**--repository**="": repository name (e.g. octocat/hello-world) **--repository**="": repository name (e.g. octocat/hello-world)
**--server, -s**="": server address **--server, -s**="": server address
@ -494,6 +498,10 @@ remove a secret
**--name**="": secret name **--name**="": secret name
**--global**: global secret
**--organization**="": organization name (e.g. octocat)
**--repository**="": repository name (e.g. octocat/hello-world) **--repository**="": repository name (e.g. octocat/hello-world)
**--server, -s**="": server address **--server, -s**="": server address
@ -512,6 +520,10 @@ update a secret
**--name**="": secret name **--name**="": secret name
**--global**: global secret
**--organization**="": organization name (e.g. octocat)
**--repository**="": repository name (e.g. octocat/hello-world) **--repository**="": repository name (e.g. octocat/hello-world)
**--server, -s**="": server address **--server, -s**="": server address
@ -528,6 +540,10 @@ display secret info
**--name**="": secret name **--name**="": secret name
**--global**: global secret
**--organization**="": organization name (e.g. octocat)
**--repository**="": repository name (e.g. octocat/hello-world) **--repository**="": repository name (e.g. octocat/hello-world)
**--server, -s**="": server address **--server, -s**="": server address
@ -540,6 +556,10 @@ list secrets
**--log-level**="": set logging level (default: info) **--log-level**="": set logging level (default: info)
**--global**: global secret
**--organization**="": organization name (e.g. octocat)
**--repository**="": repository name (e.g. octocat/hello-world) **--repository**="": repository name (e.g. octocat/hello-world)
**--server, -s**="": server address **--server, -s**="": server address