1
0
mirror of https://github.com/drone/drone-cli.git synced 2026-01-27 05:18:02 +01:00
drone-cli/drone/encrypt/encrypt.go

66 lines
1.3 KiB
Go
Raw Normal View History

2018-08-07 01:29:03 +02:00
package encrypt
2018-09-21 01:04:58 +02:00
import (
"fmt"
"io/ioutil"
"strings"
2018-08-07 01:29:03 +02:00
2018-09-21 01:04:58 +02:00
"github.com/drone/drone-cli/drone/internal"
"github.com/drone/drone-go/drone"
"github.com/urfave/cli"
)
// Command is an encryption cli.Command
2018-08-07 01:29:03 +02:00
var Command = cli.Command{
2018-09-21 01:04:58 +02:00
Name: "encrypt",
Usage: "encrypt a secret",
ArgsUsage: "<repo/name> <string>",
Action: encryptSecret,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "allow-pull-request",
2018-10-21 21:18:43 +02:00
Usage: "permit read access to pull requests",
},
cli.BoolFlag{
Name: "allow-push-on-pull-request",
Usage: "permit write access to pull requests (e.g. allow docker push)",
2018-09-21 01:04:58 +02:00
},
2018-08-07 01:29:03 +02:00
},
}
2018-09-21 01:04:58 +02:00
func encryptSecret(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := internal.ParseRepo(repo)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
plaintext := c.Args().Get(1)
if strings.HasPrefix(plaintext, "@") {
2018-10-21 22:36:29 +02:00
path := strings.TrimPrefix(plaintext, "@")
data, err := ioutil.ReadFile(path)
2018-09-21 01:04:58 +02:00
if err != nil {
return err
}
plaintext = string(data)
}
secret := &drone.Secret{
2018-10-21 21:18:43 +02:00
Data: plaintext,
PullRequest: c.Bool("allow-pull-request"),
PullRequestPush: c.Bool("allow-push-on-pull-request"),
2018-09-21 01:04:58 +02:00
}
encrypted, err := client.Encrypt(owner, name, secret)
if err != nil {
return err
}
fmt.Println(encrypted)
return nil
}