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

Commit 2bbdcad

Browse files
author
Noah Lee
authored
Add the config command to CLI (#328)
* Fix the interface of func * Add the 'config' command
1 parent 2b05288 commit 2bbdcad

File tree

11 files changed

+112
-6
lines changed

11 files changed

+112
-6
lines changed

cmd/cli/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import "github.com/urfave/cli/v2"
4+
5+
var configCommand = &cli.Command{
6+
Name: "config",
7+
Usage: "Manage config.",
8+
Subcommands: []*cli.Command{
9+
configGetCommand,
10+
},
11+
}

cmd/cli/config_get.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"github.com/urfave/cli/v2"
5+
)
6+
7+
var configGetCommand = &cli.Command{
8+
Name: "get",
9+
Usage: "Show the pipeline configurations.",
10+
ArgsUsage: "<owner>/<repo>",
11+
Action: func(cli *cli.Context) error {
12+
ns, n, err := splitFullName(cli.Args().First())
13+
if err != nil {
14+
return err
15+
}
16+
17+
c := buildClient(cli)
18+
config, err := c.Config.Get(cli.Context, ns, n)
19+
if err != nil {
20+
return err
21+
}
22+
23+
return printJson(cli, config)
24+
},
25+
}

cmd/cli/deployment_deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ var deploymentDeployCommand = &cli.Command{
4343
return err
4444
}
4545

46-
return printJson(d, cli.String("query"))
46+
return printJson(cli, d)
4747
},
4848
}

cmd/cli/deployment_get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ var deploymentGetCommand = &cli.Command{
2828
return err
2929
}
3030

31-
return printJson(d, cli.String("query"))
31+
return printJson(cli, d)
3232
},
3333
}

cmd/cli/deployment_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ var deploymentListCommand = &cli.Command{
4949
return err
5050
}
5151

52-
return printJson(ds, cli.String("query"))
52+
return printJson(cli, ds)
5353
},
5454
}

cmd/cli/deployment_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ var deploymentUpdateCommand = &cli.Command{
2727
return err
2828
}
2929

30-
return printJson(d, cli.String("query"))
30+
return printJson(cli, d)
3131
},
3232
}

cmd/cli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func main() {
3737
Commands: []*cli.Command{
3838
repoCommand,
3939
deploymentCommand,
40+
configCommand,
4041
},
4142
}
4243

cmd/cli/shared.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ func splitFullName(name string) (string, string, error) {
3333
}
3434

3535
// printJson prints the object as JSON-format.
36-
func printJson(v interface{}, query string) error {
36+
func printJson(cli *cli.Context, v interface{}) error {
3737
output, err := json.MarshalIndent(v, "", " ")
3838
if err != nil {
3939
return fmt.Errorf("Failed to marshal: %w", err)
4040
}
4141

42-
if query != "" {
42+
if query := cli.String("query"); query != "" {
4343
fmt.Println(gjson.GetBytes(output, query))
4444
return nil
4545
}

pkg/api/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type (
2222
// Services used for talking to different parts of the Gitploy API.
2323
Repo *RepoService
2424
Deployment *DeploymentService
25+
Config *ConfigService
2526
}
2627

2728
client struct {
@@ -56,6 +57,7 @@ func NewClient(host string, httpClient *http.Client) *Client {
5657

5758
c.Repo = &RepoService{client: c.common}
5859
c.Deployment = &DeploymentService{client: c.common}
60+
c.Config = &ConfigService{client: c.common}
5961

6062
return c
6163
}

pkg/api/config.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/gitploy-io/gitploy/model/extent"
8+
)
9+
10+
type (
11+
ConfigService service
12+
)
13+
14+
func (s *ConfigService) Get(ctx context.Context, namespace, name string) (*extent.Config, error) {
15+
req, err := s.client.NewRequest(
16+
"GET",
17+
fmt.Sprintf("api/v1/repos/%s/%s/config", namespace, name),
18+
nil,
19+
)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
var config *extent.Config
25+
if err := s.client.Do(ctx, req, &config); err != nil {
26+
return nil, err
27+
}
28+
29+
return config, nil
30+
}

0 commit comments

Comments
 (0)