Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cmd/cli/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

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

var deploymentCommand = &cli.Command{
Name: "deployment",
Aliases: []string{"d"},
Usage: "Manage deployments.",
Subcommands: []*cli.Command{
deploymentListCommand,
deploymentGetCommand,
deploymentDeployCommand,
deploymentUpdateCommand,
},
}
48 changes: 48 additions & 0 deletions cmd/cli/deployment_deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

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

"github.com/gitploy-io/gitploy/pkg/api"
)

var deploymentDeployCommand = &cli.Command{
Name: "deploy",
Usage: "Deploy a specific ref(branch, SHA, tag) to the environment.",
ArgsUsage: "<owner>/<repo>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "type",
Usage: "The type of the ref: 'commit', 'branch', or 'tag'.",
Required: true,
},
&cli.StringFlag{
Name: "env",
Usage: "The name of the environment.",
Required: true,
},
&cli.StringFlag{
Name: "ref",
Usage: "The specific ref. It can be any any named branch, tag, or SHA.",
Required: true,
},
},
Action: func(cli *cli.Context) error {
ns, n, err := splitFullName(cli.Args().First())
if err != nil {
return err
}

c := buildClient(cli)
d, err := c.Deployment.Create(cli.Context, ns, n, api.DeploymentCreateRequest{
Type: cli.String("type"),
Ref: cli.String("ref"),
Env: cli.String("env"),
})
if err != nil {
return err
}

return printJson(d, cli.String("query"))
},
}
33 changes: 33 additions & 0 deletions cmd/cli/deployment_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"strconv"

"github.com/urfave/cli/v2"
)

var deploymentGetCommand = &cli.Command{
Name: "get",
Usage: "Show the deployment",
ArgsUsage: "<owner>/<repo> <number>",
Action: func(cli *cli.Context) error {
// Validate arguments.
ns, n, err := splitFullName(cli.Args().First())
if err != nil {
return err
}

number, err := strconv.Atoi(cli.Args().Get(1))
if err != nil {
return err
}

c := buildClient(cli)
d, err := c.Deployment.Get(cli.Context, ns, n, number)
if err != nil {
return err
}

return printJson(d, cli.String("query"))
},
}
54 changes: 54 additions & 0 deletions cmd/cli/deployment_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

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

"github.com/gitploy-io/gitploy/model/ent/deployment"
"github.com/gitploy-io/gitploy/pkg/api"
)

var deploymentListCommand = &cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "Show the deployments under the repository.",
ArgsUsage: "<owner>/<repo>",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "page",
Value: 1,
Usage: "The page of list.",
},
&cli.IntFlag{
Name: "per-page",
Value: 30,
Usage: "The item count per page.",
},
&cli.StringFlag{
Name: "env",
Usage: "The name of environment. It only shows deployments for the environment.",
},
&cli.StringFlag{
Name: "status",
Usage: "The deployment status: 'waiting', 'created', 'queued', 'running', 'success', or 'failure'. It only shows deployments the status is matched. ",
},
},
Action: func(cli *cli.Context) error {
c := buildClient(cli)

ns, n, err := splitFullName(cli.Args().First())
if err != nil {
return err
}

ds, err := c.Deployment.List(cli.Context, ns, n, api.DeploymentListOptions{
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
Env: cli.String("env"),
Status: deployment.Status(cli.String("status")),
})
if err != nil {
return err
}

return printJson(ds, cli.String("query"))
},
}
32 changes: 32 additions & 0 deletions cmd/cli/deployment_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"strconv"

"github.com/urfave/cli/v2"
)

var deploymentUpdateCommand = &cli.Command{
Name: "update",
Usage: "Trigger the deployment which has approved by reviews.",
ArgsUsage: "<owner>/<repo> <number>",
Action: func(cli *cli.Context) error {
ns, n, err := splitFullName(cli.Args().First())
if err != nil {
return err
}

number, err := strconv.Atoi(cli.Args().Get(1))
if err != nil {
return err
}

c := buildClient(cli)
d, err := c.Deployment.Update(cli.Context, ns, n, number)
if err != nil {
return err
}

return printJson(d, cli.String("query"))
},
}
1 change: 1 addition & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
},
Commands: []*cli.Command{
repoCommand,
deploymentCommand,
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var repoCommand *cli.Command = &cli.Command{
Name: "repo",
Usage: "Manage repos.",
Usage: "Manage repositories.",
Subcommands: []*cli.Command{
repoListCommand,
repoGetCommand,
Expand Down
19 changes: 19 additions & 0 deletions cmd/cli/shared.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"encoding/json"
"fmt"
"strings"

"github.com/tidwall/gjson"
"github.com/urfave/cli/v2"
"golang.org/x/oauth2"

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

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

return ss[0], ss[1], nil
}

// printJson prints the object as JSON-format.
func printJson(v interface{}, query string) error {
output, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Errorf("Failed to marshal: %w", err)
}

if query != "" {
fmt.Println(gjson.GetBytes(output, query))
return nil
}

fmt.Println(string(output))
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/tidwall/gjson v1.13.0 // indirect
github.com/tidwall/gjson v1.14.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M=
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.0 h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=
github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type (
common *client

// Services used for talking to different parts of the Gitploy API.
Repo *RepoService
Repo *RepoService
Deployment *DeploymentService
}

client struct {
Expand Down Expand Up @@ -54,6 +55,7 @@ func NewClient(host string, httpClient *http.Client) *Client {
}

c.Repo = &RepoService{client: c.common}
c.Deployment = &DeploymentService{client: c.common}

return c
}
Expand Down
Loading