2019-04-06 19:32:14 +00:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
2022-08-15 18:10:13 +00:00
|
|
|
"os"
|
2019-04-06 19:32:14 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
2021-09-21 14:36:41 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/internal"
|
2021-10-02 22:27:43 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
var secretCreateCmd = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "add",
|
|
|
|
Usage: "adds a secret",
|
2022-08-15 18:10:13 +00:00
|
|
|
ArgsUsage: "[org/repo|org]",
|
2019-04-06 19:32:14 +00:00
|
|
|
Action: secretCreate,
|
2021-10-27 19:03:14 +00:00
|
|
|
Flags: append(common.GlobalFlags,
|
2022-08-15 18:10:13 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "global",
|
|
|
|
Usage: "global secret",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "organization",
|
|
|
|
Usage: "organization name (e.g. octocat)",
|
|
|
|
},
|
2022-08-31 22:36:32 +00:00
|
|
|
common.RepoFlag,
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "name",
|
|
|
|
Usage: "secret name",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "value",
|
|
|
|
Usage: "secret value",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringSliceFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "event",
|
|
|
|
Usage: "secret limited to these events",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringSliceFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "image",
|
|
|
|
Usage: "secret limited to these images",
|
|
|
|
},
|
2022-10-27 02:21:07 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "plugins-only",
|
|
|
|
Usage: "secret limited to plugins",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
),
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func secretCreate(c *cli.Context) error {
|
|
|
|
client, err := internal.NewClient(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-16 12:58:13 +00:00
|
|
|
|
2021-10-02 22:27:43 +00:00
|
|
|
secret := &woodpecker.Secret{
|
2022-10-27 02:21:07 +00:00
|
|
|
Name: strings.ToLower(c.String("name")),
|
|
|
|
Value: c.String("value"),
|
|
|
|
Images: c.StringSlice("image"),
|
|
|
|
PluginsOnly: c.Bool("plugins-only"),
|
|
|
|
Events: c.StringSlice("event"),
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
if len(secret.Events) == 0 {
|
|
|
|
secret.Events = defaultSecretEvents
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(secret.Value, "@") {
|
|
|
|
path := strings.TrimPrefix(secret.Value, "@")
|
2022-08-15 18:10:13 +00:00
|
|
|
out, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
secret.Value = string(out)
|
|
|
|
}
|
2022-08-15 18:10:13 +00:00
|
|
|
|
|
|
|
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)
|
2019-04-06 19:32:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultSecretEvents = []string{
|
2021-10-02 22:27:43 +00:00
|
|
|
woodpecker.EventPush,
|
|
|
|
woodpecker.EventTag,
|
|
|
|
woodpecker.EventDeploy,
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|