2017-04-29 17:03:45 +00:00
|
|
|
package secret
|
2016-06-27 21:06:02 +00:00
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
2017-04-29 17:03:45 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/drone/internal"
|
2017-04-11 17:06:45 +00:00
|
|
|
)
|
2016-06-27 21:06:02 +00:00
|
|
|
|
|
|
|
var secretListCmd = cli.Command{
|
2017-03-16 10:14:02 +00:00
|
|
|
Name: "ls",
|
2017-04-11 17:06:45 +00:00
|
|
|
Usage: "list secrets",
|
2017-03-16 10:14:02 +00:00
|
|
|
Action: secretList,
|
2017-04-11 17:06:45 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "repository",
|
|
|
|
Usage: "repository name (e.g. octocat/hello-world)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "format",
|
|
|
|
Usage: "format output",
|
|
|
|
Value: tmplSecretList,
|
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
},
|
2016-06-27 21:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func secretList(c *cli.Context) error {
|
2017-04-11 17:06:45 +00:00
|
|
|
var (
|
|
|
|
format = c.String("format") + "\n"
|
|
|
|
reponame = c.String("repository")
|
|
|
|
)
|
|
|
|
if reponame == "" {
|
|
|
|
reponame = c.Args().First()
|
|
|
|
}
|
2017-04-29 17:03:45 +00:00
|
|
|
owner, name, err := internal.ParseRepo(reponame)
|
2016-06-27 21:06:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-29 17:03:45 +00:00
|
|
|
client, err := internal.NewClient(c)
|
2016-06-27 21:06:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-11 17:06:45 +00:00
|
|
|
list, err := client.SecretList(owner, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpl, err := template.New("_").Funcs(secretFuncMap).Parse(format)
|
|
|
|
if err != nil {
|
2016-06-27 21:06:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-04-11 17:06:45 +00:00
|
|
|
for _, registry := range list {
|
|
|
|
tmpl.Execute(os.Stdout, registry)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// template for secret list items
|
|
|
|
var tmplSecretList = "\x1b[33m{{ .Name }} \x1b[0m" + `
|
|
|
|
Events: {{ list .Events }}
|
|
|
|
{{- if .Images }}
|
|
|
|
Images: {{ list .Images }}
|
|
|
|
{{- else }}
|
|
|
|
Images: <any>
|
|
|
|
{{- end }}
|
|
|
|
`
|
2016-06-27 21:06:02 +00:00
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
var secretFuncMap = template.FuncMap{
|
|
|
|
"list": func(s []string) string {
|
|
|
|
return strings.Join(s, ", ")
|
|
|
|
},
|
2016-06-27 21:06:02 +00:00
|
|
|
}
|