Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit aa47b05

Browse files
author
Noah Lee
authored
Add the 'deployment' command into CLI (#325)
* Upgrade the 'GJSON' package * Add the 'deployment' command * Add the subcommands: 'get', 'list', 'deploy', and 'update' * Add unit tests for deployment
1 parent 6dcd6ae commit aa47b05

14 files changed

+444
-4
lines changed

cmd/cli/deployment.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "github.com/urfave/cli/v2"
4+
5+
var deploymentCommand = &cli.Command{
6+
Name: "deployment",
7+
Aliases: []string{"d"},
8+
Usage: "Manage deployments.",
9+
Subcommands: []*cli.Command{
10+
deploymentListCommand,
11+
deploymentGetCommand,
12+
deploymentDeployCommand,
13+
deploymentUpdateCommand,
14+
},
15+
}

cmd/cli/deployment_deploy.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"github.com/urfave/cli/v2"
5+
6+
"github.com/gitploy-io/gitploy/pkg/api"
7+
)
8+
9+
var deploymentDeployCommand = &cli.Command{
10+
Name: "deploy",
11+
Usage: "Deploy a specific ref(branch, SHA, tag) to the environment.",
12+
ArgsUsage: "<owner>/<repo>",
13+
Flags: []cli.Flag{
14+
&cli.StringFlag{
15+
Name: "type",
16+
Usage: "The type of the ref: 'commit', 'branch', or 'tag'.",
17+
Required: true,
18+
},
19+
&cli.StringFlag{
20+
Name: "env",
21+
Usage: "The name of the environment.",
22+
Required: true,
23+
},
24+
&cli.StringFlag{
25+
Name: "ref",
26+
Usage: "The specific ref. It can be any any named branch, tag, or SHA.",
27+
Required: true,
28+
},
29+
},
30+
Action: func(cli *cli.Context) error {
31+
ns, n, err := splitFullName(cli.Args().First())
32+
if err != nil {
33+
return err
34+
}
35+
36+
c := buildClient(cli)
37+
d, err := c.Deployment.Create(cli.Context, ns, n, api.DeploymentCreateRequest{
38+
Type: cli.String("type"),
39+
Ref: cli.String("ref"),
40+
Env: cli.String("env"),
41+
})
42+
if err != nil {
43+
return err
44+
}
45+
46+
return printJson(d, cli.String("query"))
47+
},
48+
}

cmd/cli/deployment_get.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/urfave/cli/v2"
7+
)
8+
9+
var deploymentGetCommand = &cli.Command{
10+
Name: "get",
11+
Usage: "Show the deployment",
12+
ArgsUsage: "<owner>/<repo> <number>",
13+
Action: func(cli *cli.Context) error {
14+
// Validate arguments.
15+
ns, n, err := splitFullName(cli.Args().First())
16+
if err != nil {
17+
return err
18+
}
19+
20+
number, err := strconv.Atoi(cli.Args().Get(1))
21+
if err != nil {
22+
return err
23+
}
24+
25+
c := buildClient(cli)
26+
d, err := c.Deployment.Get(cli.Context, ns, n, number)
27+
if err != nil {
28+
return err
29+
}
30+
31+
return printJson(d, cli.String("query"))
32+
},
33+
}

cmd/cli/deployment_list.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"github.com/urfave/cli/v2"
5+
6+
"github.com/gitploy-io/gitploy/model/ent/deployment"
7+
"github.com/gitploy-io/gitploy/pkg/api"
8+
)
9+
10+
var deploymentListCommand = &cli.Command{
11+
Name: "list",
12+
Aliases: []string{"ls"},
13+
Usage: "Show the deployments under the repository.",
14+
ArgsUsage: "<owner>/<repo>",
15+
Flags: []cli.Flag{
16+
&cli.IntFlag{
17+
Name: "page",
18+
Value: 1,
19+
Usage: "The page of list.",
20+
},
21+
&cli.IntFlag{
22+
Name: "per-page",
23+
Value: 30,
24+
Usage: "The item count per page.",
25+
},
26+
&cli.StringFlag{
27+
Name: "env",
28+
Usage: "The name of environment. It only shows deployments for the environment.",
29+
},
30+
&cli.StringFlag{
31+
Name: "status",
32+
Usage: "The deployment status: 'waiting', 'created', 'queued', 'running', 'success', or 'failure'. It only shows deployments the status is matched. ",
33+
},
34+
},
35+
Action: func(cli *cli.Context) error {
36+
c := buildClient(cli)
37+
38+
ns, n, err := splitFullName(cli.Args().First())
39+
if err != nil {
40+
return err
41+
}
42+
43+
ds, err := c.Deployment.List(cli.Context, ns, n, api.DeploymentListOptions{
44+
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
45+
Env: cli.String("env"),
46+
Status: deployment.Status(cli.String("status")),
47+
})
48+
if err != nil {
49+
return err
50+
}
51+
52+
return printJson(ds, cli.String("query"))
53+
},
54+
}

cmd/cli/deployment_update.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/urfave/cli/v2"
7+
)
8+
9+
var deploymentUpdateCommand = &cli.Command{
10+
Name: "update",
11+
Usage: "Trigger the deployment which has approved by reviews.",
12+
ArgsUsage: "<owner>/<repo> <number>",
13+
Action: func(cli *cli.Context) error {
14+
ns, n, err := splitFullName(cli.Args().First())
15+
if err != nil {
16+
return err
17+
}
18+
19+
number, err := strconv.Atoi(cli.Args().Get(1))
20+
if err != nil {
21+
return err
22+
}
23+
24+
c := buildClient(cli)
25+
d, err := c.Deployment.Update(cli.Context, ns, n, number)
26+
if err != nil {
27+
return err
28+
}
29+
30+
return printJson(d, cli.String("query"))
31+
},
32+
}

cmd/cli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
},
3434
Commands: []*cli.Command{
3535
repoCommand,
36+
deploymentCommand,
3637
},
3738
}
3839

cmd/cli/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
var repoCommand *cli.Command = &cli.Command{
88
Name: "repo",
9-
Usage: "Manage repos.",
9+
Usage: "Manage repositories.",
1010
Subcommands: []*cli.Command{
1111
repoListCommand,
1212
repoGetCommand,

cmd/cli/shared.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"strings"
67

8+
"github.com/tidwall/gjson"
79
"github.com/urfave/cli/v2"
810
"golang.org/x/oauth2"
911

@@ -20,6 +22,7 @@ func buildClient(cli *cli.Context) *api.Client {
2022
return api.NewClient(cli.String("host"), tc)
2123
}
2224

25+
// splitFullName splits the full name into namespace, and name.
2326
func splitFullName(name string) (string, string, error) {
2427
ss := strings.Split(name, "/")
2528
if len(ss) != 2 {
@@ -28,3 +31,19 @@ func splitFullName(name string) (string, string, error) {
2831

2932
return ss[0], ss[1], nil
3033
}
34+
35+
// printJson prints the object as JSON-format.
36+
func printJson(v interface{}, query string) error {
37+
output, err := json.MarshalIndent(v, "", " ")
38+
if err != nil {
39+
return fmt.Errorf("Failed to marshal: %w", err)
40+
}
41+
42+
if query != "" {
43+
fmt.Println(gjson.GetBytes(output, query))
44+
return nil
45+
}
46+
47+
fmt.Println(string(output))
48+
return nil
49+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ require (
6161
github.com/russross/blackfriday/v2 v2.0.1 // indirect
6262
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
6363
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
64-
github.com/tidwall/gjson v1.13.0 // indirect
64+
github.com/tidwall/gjson v1.14.0 // indirect
6565
github.com/tidwall/match v1.1.1 // indirect
6666
github.com/tidwall/pretty v1.2.0 // indirect
6767
github.com/ugorji/go/codec v1.2.6 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
448448
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
449449
github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M=
450450
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
451+
github.com/tidwall/gjson v1.14.0 h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=
452+
github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
451453
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
452454
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
453455
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=

0 commit comments

Comments
 (0)