From b5490cd67ead54134a0505e7e54d84deeafa1e3c Mon Sep 17 00:00:00 2001 From: Eoin McAfee Date: Fri, 28 May 2021 16:56:13 +0100 Subject: [PATCH] Add support for organization level templates --- drone/template/template_add.go | 19 ++++++++++++++++--- drone/template/template_info.go | 14 +++++++++++--- drone/template/template_list.go | 5 +++-- drone/template/template_rm.go | 11 ++++++++--- drone/template/template_set.go | 13 +++++++++---- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/drone/template/template_add.go b/drone/template/template_add.go index 2ca76b4..fadb219 100644 --- a/drone/template/template_add.go +++ b/drone/template/template_add.go @@ -1,6 +1,7 @@ package template import ( + "errors" "github.com/drone/drone-cli/drone/internal" "github.com/drone/drone-go/drone" "github.com/urfave/cli" @@ -11,13 +12,17 @@ import ( var templateCreateCmd = cli.Command{ Name: "add", Usage: "adds a template", - ArgsUsage: "[name]", + ArgsUsage: "[namespace] [name] [data]", Action: templateCreate, Flags: []cli.Flag{ cli.StringFlag{ Name: "name", Usage: "template name", }, + cli.StringFlag{ + Name: "namespace", + Usage: "organization namespace", + }, cli.StringFlag{ Name: "data", Usage: "template file data", @@ -31,7 +36,15 @@ func templateCreate(c *cli.Context) error { return err } template := &drone.Template{ - Name: c.String("name"), + Namespace: c.String("namespace"), + Name: c.String("name"), + } + + if template.Name == "" { + return errors.New("Missing template name") + } + if template.Namespace == "" { + return errors.New("Missing namespace") } if strings.HasPrefix(c.String("data"), "@") { @@ -40,7 +53,7 @@ func templateCreate(c *cli.Context) error { if ferr != nil { return ferr } - template.Data = out + template.Data = string(out) } _, err = client.TemplateCreate(template) return err diff --git a/drone/template/template_info.go b/drone/template/template_info.go index c07b0b1..c14d40b 100644 --- a/drone/template/template_info.go +++ b/drone/template/template_info.go @@ -13,9 +13,13 @@ import ( var templateInfoCmd = cli.Command{ Name: "info", Usage: "display template info", - ArgsUsage: "[name]", + ArgsUsage: "[namespace] [name]", Action: templateInfo, Flags: []cli.Flag{ + cli.StringFlag{ + Name: "namespace", + Usage: "organization namespace", + }, cli.StringFlag{ Name: "name", Usage: "template name", @@ -30,18 +34,22 @@ var templateInfoCmd = cli.Command{ func templateInfo(c *cli.Context) error { var ( + namespace = c.String("namespace") templateName = c.String("name") format = c.String("format") + "\n" ) if templateName == "" { return errors.New("Missing template name") } + if namespace == "" { + return errors.New("Missing namespace") + } client, err := internal.NewClient(c) if err != nil { return err } - templates, err := client.Template(templateName) + templates, err := client.Template(namespace, templateName) if err != nil { return err } @@ -52,6 +60,6 @@ func templateInfo(c *cli.Context) error { return tmpl.Execute(os.Stdout, templates) } -var tmplTemplateInfoList = "\x1b[33m{{ .Name }} \x1b[0m" + ` +var tmplTemplateInfoList = "\x1b[33m{{ .Name }} \x1b[0m" + "\x1b [33m{{ .Namespace }} \x1b[0m" + ` Data: {{ .Data }} ` diff --git a/drone/template/template_list.go b/drone/template/template_list.go index 6f46d9d..bedd727 100644 --- a/drone/template/template_list.go +++ b/drone/template/template_list.go @@ -24,13 +24,14 @@ var templateListCmd = cli.Command{ func templateList(c *cli.Context) error { var ( - format = c.String("format") + "\n" + namespace = c.Args().First() + format = c.String("format") + "\n" ) client, err := internal.NewClient(c) if err != nil { return err } - list, err := client.TemplateList() + list, err := client.TemplateList(namespace) if err != nil { return err } diff --git a/drone/template/template_rm.go b/drone/template/template_rm.go index 42a5139..0c13f06 100644 --- a/drone/template/template_rm.go +++ b/drone/template/template_rm.go @@ -8,23 +8,28 @@ import ( var templateDeleteCmd = cli.Command{ Name: "rm", Usage: "remove a template", - ArgsUsage: "[name]", + ArgsUsage: "[namespace] [name]", Action: templateDelete, Flags: []cli.Flag{ cli.StringFlag{ Name: "name", Usage: "template name", }, + cli.StringFlag{ + Name: "namespace", + Usage: "organization name", + }, }, } func templateDelete(c *cli.Context) error { var ( - name = c.String("name") + namespace = c.String("namespace") + name = c.String("name") ) client, err := internal.NewClient(c) if err != nil { return err } - return client.TemplateDelete(name) + return client.TemplateDelete(namespace, name) } diff --git a/drone/template/template_set.go b/drone/template/template_set.go index 3cb245e..8269764 100644 --- a/drone/template/template_set.go +++ b/drone/template/template_set.go @@ -11,13 +11,17 @@ import ( var templateUpdateCmd = cli.Command{ Name: "update", Usage: "update a template", - ArgsUsage: "[name]", + ArgsUsage: "[namespace] [name] [data]", Action: templateUpdate, Flags: []cli.Flag{ cli.StringFlag{ Name: "name", Usage: "template name", }, + cli.StringFlag{ + Name: "namespace", + Usage: "organization name", + }, cli.StringFlag{ Name: "data", Usage: "template file data", @@ -31,7 +35,8 @@ func templateUpdate(c *cli.Context) error { return err } template := &drone.Template{ - Name: c.String("name"), + Name: c.String("name"), + Namespace: c.String("namespace"), } if strings.HasPrefix(c.String("data"), "@") { path := strings.TrimPrefix(c.String("data"), "@") @@ -39,8 +44,8 @@ func templateUpdate(c *cli.Context) error { if ferr != nil { return ferr } - template.Data = out + template.Data = string(out) } - _, err = client.TemplateUpdate(template.Name, template) + _, err = client.TemplateUpdate(template.Namespace, template.Name, template) return err }