Skip to content

Commit

Permalink
Add cron feature (#934)
Browse files Browse the repository at this point in the history
https://woodpecker-ci.org/docs/usage/cron

Co-authored-by: Anbraten <anton@ju60.de>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 31, 2022
1 parent 65587e3 commit 383f273
Show file tree
Hide file tree
Showing 63 changed files with 1,604 additions and 123 deletions.
21 changes: 21 additions & 0 deletions cli/cron/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cron

import (
"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
)

// Command exports the cron command set.
var Command = &cli.Command{
Name: "cron",
Usage: "manage cron jobs",
Flags: common.GlobalFlags,
Subcommands: []*cli.Command{
cronCreateCmd,
cronDeleteCmd,
cronUpdateCmd,
cronInfoCmd,
cronListCmd,
},
}
72 changes: 72 additions & 0 deletions cli/cron/cron_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cron

import (
"html/template"
"os"

"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
)

var cronCreateCmd = &cli.Command{
Name: "add",
Usage: "adds a cron",
ArgsUsage: "[repo/name]",
Action: cronCreate,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "cron name",
Required: true,
},
&cli.StringFlag{
Name: "branch",
Usage: "cron branch",
},
&cli.StringFlag{
Name: "schedule",
Usage: "cron schedule",
Required: true,
},
common.FormatFlag(tmplCronList, true),
),
}

func cronCreate(c *cli.Context) error {
var (
jobName = c.String("name")
branch = c.String("branch")
schedule = c.String("schedule")
reponame = c.String("repository")
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)
if err != nil {
return err
}
cron := &woodpecker.Cron{
Name: jobName,
Branch: branch,
Schedule: schedule,
}
cron, err = client.CronCreate(owner, name, cron)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, cron)
}
55 changes: 55 additions & 0 deletions cli/cron/cron_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cron

import (
"html/template"
"os"

"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var cronInfoCmd = &cli.Command{
Name: "info",
Usage: "display cron info",
ArgsUsage: "[repo/name]",
Action: cronInfo,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "id",
Usage: "cron id",
Required: true,
},
common.FormatFlag(tmplCronList, true),
),
}

func cronInfo(c *cli.Context) error {
var (
jobID = c.Int64("id")
reponame = c.String("repository")
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)
if err != nil {
return err
}
cron, err := client.CronGet(owner, name, jobID)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, cron)
}
62 changes: 62 additions & 0 deletions cli/cron/cron_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cron

import (
"html/template"
"os"

"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var cronListCmd = &cli.Command{
Name: "ls",
Usage: "list registries",
ArgsUsage: "[repo/name]",
Action: cronList,
Flags: append(common.GlobalFlags,
common.RepoFlag,
common.FormatFlag(tmplCronList, true),
),
}

func cronList(c *cli.Context) error {
var (
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)
if err != nil {
return err
}
list, err := client.CronList(owner, name)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
for _, cron := range list {
if err := tmpl.Execute(os.Stdout, cron); err != nil {
return err
}
}
return nil
}

// template for build list information
var tmplCronList = "\x1b[33m{{ .Name }} \x1b[0m" + `
ID: {{ .ID }}
Branch: {{ .Branch }}
Schedule: {{ .Schedule }}
NextExec: {{ .NextExec }}
`
50 changes: 50 additions & 0 deletions cli/cron/cron_rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cron

import (
"fmt"

"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)

var cronDeleteCmd = &cli.Command{
Name: "rm",
Usage: "remove a cron",
ArgsUsage: "[repo/name]",
Action: cronDelete,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "id",
Usage: "cron id",
Required: true,
},
),
}

func cronDelete(c *cli.Context) error {
var (
jobID = c.Int64("id")
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)
if err != nil {
return err
}
err = client.CronDelete(owner, name, jobID)
if err != nil {
return err
}

fmt.Println("Success")
return nil
}
77 changes: 77 additions & 0 deletions cli/cron/cron_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cron

import (
"html/template"
"os"

"github.com/urfave/cli/v2"

"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
)

var cronUpdateCmd = &cli.Command{
Name: "update",
Usage: "update a cron",
ArgsUsage: "[repo/name]",
Action: cronUpdate,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "id",
Usage: "cron id",
Required: true,
},
&cli.StringFlag{
Name: "name",
Usage: "cron name",
},
&cli.StringFlag{
Name: "branch",
Usage: "cron branch",
},
&cli.StringFlag{
Name: "schedule",
Usage: "cron schedule",
},
common.FormatFlag(tmplCronList, true),
),
}

func cronUpdate(c *cli.Context) error {
var (
reponame = c.String("repository")
jobID = c.Int64("id")
jobName = c.String("name")
branch = c.String("branch")
schedule = c.String("schedule")
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)
if err != nil {
return err
}
cron := &woodpecker.Cron{
ID: jobID,
Name: jobName,
Branch: branch,
Schedule: schedule,
}
cron, err = client.CronUpdate(owner, name, cron)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, cron)
}
5 changes: 1 addition & 4 deletions cli/secret/secret_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ var secretCreateCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "secret name",
Expand Down
5 changes: 1 addition & 4 deletions cli/secret/secret_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ var secretInfoCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "secret name",
Expand Down
5 changes: 1 addition & 4 deletions cli/secret/secret_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ var secretListCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
common.FormatFlag(tmplSecretList, true),
),
}
Expand Down
5 changes: 1 addition & 4 deletions cli/secret/secret_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ var secretDeleteCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "secret name",
Expand Down
Loading

0 comments on commit 383f273

Please sign in to comment.