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

Commit 6dcd6ae

Browse files
author
Noah Lee
authored
Add repo command to access Repo entities (#322)
* Add 'RepoService' structure * Add subcommands 'list', 'get', and 'update' * Add unit tests for RepoService
1 parent b5791fb commit 6dcd6ae

File tree

12 files changed

+469
-4
lines changed

12 files changed

+469
-4
lines changed

cmd/cli/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ func main() {
2626
Usage: "The authorization token.",
2727
EnvVars: []string{"GITPLOY_TOKEN"},
2828
},
29+
&cli.StringFlag{
30+
Name: "query",
31+
Usage: "A GJSON query to use in filtering the response data",
32+
},
33+
},
34+
Commands: []*cli.Command{
35+
repoCommand,
2936
},
30-
Commands: []*cli.Command{},
3137
}
3238

3339
err := app.Run(os.Args)

cmd/cli/repo.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 (
4+
"github.com/urfave/cli/v2"
5+
)
6+
7+
var repoCommand *cli.Command = &cli.Command{
8+
Name: "repo",
9+
Usage: "Manage repos.",
10+
Subcommands: []*cli.Command{
11+
repoListCommand,
12+
repoGetCommand,
13+
repoUpdateCommand,
14+
},
15+
}

cmd/cli/repo_get.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/tidwall/gjson"
8+
"github.com/urfave/cli/v2"
9+
)
10+
11+
var repoGetCommand = &cli.Command{
12+
Name: "get",
13+
Usage: "Show the repository.",
14+
ArgsUsage: "<owner>/<repo>",
15+
Action: func(cli *cli.Context) error {
16+
ns, n, err := splitFullName(cli.Args().First())
17+
if err != nil {
18+
return err
19+
}
20+
21+
c := buildClient(cli)
22+
repo, err := c.Repo.Get(cli.Context, ns, n)
23+
if err != nil {
24+
return err
25+
}
26+
27+
output, err := json.MarshalIndent(repo, "", " ")
28+
if err != nil {
29+
return fmt.Errorf("Failed to marshal: %w", err)
30+
}
31+
32+
if q := cli.String("query"); q != "" {
33+
fmt.Println(gjson.GetBytes(output, q))
34+
return nil
35+
}
36+
37+
fmt.Println(string(output))
38+
return nil
39+
},
40+
}

cmd/cli/repo_list.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/tidwall/gjson"
8+
"github.com/urfave/cli/v2"
9+
10+
"github.com/gitploy-io/gitploy/model/ent"
11+
"github.com/gitploy-io/gitploy/pkg/api"
12+
)
13+
14+
var repoListCommand = &cli.Command{
15+
Name: "list",
16+
Aliases: []string{"ls"},
17+
Usage: "Show own repositories.",
18+
Flags: []cli.Flag{
19+
&cli.BoolFlag{
20+
Name: "all",
21+
Usage: "Show all repositories.",
22+
},
23+
&cli.IntFlag{
24+
Name: "page",
25+
Value: 1,
26+
Usage: "The page of list.",
27+
},
28+
&cli.IntFlag{
29+
Name: "per-page",
30+
Value: 30,
31+
Usage: "The item count per page.",
32+
},
33+
},
34+
Action: func(cli *cli.Context) error {
35+
c := buildClient(cli)
36+
37+
var (
38+
repos []*ent.Repo
39+
err error
40+
)
41+
42+
if cli.Bool("all") {
43+
if repos, err = c.Repo.ListAll(cli.Context); err != nil {
44+
return err
45+
}
46+
} else {
47+
if repos, err = c.Repo.List(cli.Context, api.RepoListOptions{
48+
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
49+
}); err != nil {
50+
return err
51+
}
52+
}
53+
54+
output, err := json.MarshalIndent(repos, "", " ")
55+
if err != nil {
56+
return fmt.Errorf("Failed to marshal: %w", err)
57+
}
58+
59+
if q := cli.String("query"); q != "" {
60+
fmt.Println(gjson.GetBytes(output, q))
61+
return nil
62+
}
63+
64+
fmt.Println(string(output))
65+
return nil
66+
},
67+
}

cmd/cli/repo_update.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/AlekSi/pointer"
9+
"github.com/gitploy-io/gitploy/pkg/api"
10+
"github.com/tidwall/gjson"
11+
"github.com/urfave/cli/v2"
12+
)
13+
14+
var repoUpdateCommand = &cli.Command{
15+
Name: "update",
16+
Usage: "Update the repository.",
17+
ArgsUsage: "<owner>/<repo>",
18+
Flags: []cli.Flag{
19+
&cli.StringFlag{
20+
Name: "config",
21+
Aliases: []string{"C"},
22+
Usage: "The path of configuration file.",
23+
},
24+
&cli.StringFlag{
25+
Name: "active",
26+
Aliases: []string{"A"},
27+
Usage: "Activate or deactivate the repository. Ex 'true', 'false'",
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+
// Build the request body.
37+
req := api.RepoUpdateRequest{}
38+
if config := cli.String("config"); config != "" {
39+
req.ConfigPath = pointer.ToString(config)
40+
}
41+
42+
if active := cli.String("active"); active != "" {
43+
b, err := strconv.ParseBool(active)
44+
if err != nil {
45+
return fmt.Errorf("'%s' is invalid format: %w", active, err)
46+
}
47+
48+
req.Active = pointer.ToBool(b)
49+
}
50+
51+
c := buildClient(cli)
52+
repo, err := c.Repo.Update(cli.Context, ns, n, req)
53+
if err != nil {
54+
return err
55+
}
56+
57+
output, err := json.MarshalIndent(repo, "", " ")
58+
if err != nil {
59+
return fmt.Errorf("Failed to marshal: %w", err)
60+
}
61+
62+
if q := cli.String("query"); q != "" {
63+
fmt.Println(gjson.GetBytes(output, q))
64+
return nil
65+
}
66+
67+
fmt.Println(string(output))
68+
return nil
69+
},
70+
}

cmd/cli/shared.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/urfave/cli/v2"
8+
"golang.org/x/oauth2"
9+
10+
"github.com/gitploy-io/gitploy/pkg/api"
11+
)
12+
13+
// buildClient returns a client to interact with a server.
14+
func buildClient(cli *cli.Context) *api.Client {
15+
ts := oauth2.StaticTokenSource(
16+
&oauth2.Token{AccessToken: cli.String("token")},
17+
)
18+
tc := oauth2.NewClient(cli.Context, ts)
19+
20+
return api.NewClient(cli.String("host"), tc)
21+
}
22+
23+
func splitFullName(name string) (string, string, error) {
24+
ss := strings.Split(name, "/")
25+
if len(ss) != 2 {
26+
return "", "", fmt.Errorf("'%s' is invalid format", name)
27+
}
28+
29+
return ss[0], ss[1], nil
30+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ 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
65+
github.com/tidwall/match v1.1.1 // indirect
66+
github.com/tidwall/pretty v1.2.0 // indirect
6467
github.com/ugorji/go/codec v1.2.6 // indirect
6568
github.com/urfave/cli/v2 v2.3.0 // indirect
6669
go.uber.org/atomic v1.7.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
446446
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
447447
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
448448
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
449+
github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M=
450+
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
451+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
452+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
453+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
454+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
449455
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
450456
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
451457
github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E=

pkg/api/client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type (
2020
common *client
2121

2222
// Services used for talking to different parts of the Gitploy API.
23+
Repo *RepoService
2324
}
2425

2526
client struct {
@@ -31,9 +32,9 @@ type (
3132
BaseURL *url.URL
3233
}
3334

34-
// service struct {
35-
// client *client
36-
// }
35+
service struct {
36+
*client
37+
}
3738

3839
ErrorResponse struct {
3940
Code string `json:"code"`
@@ -52,6 +53,8 @@ func NewClient(host string, httpClient *http.Client) *Client {
5253
common: &client{httpClient: httpClient, BaseURL: baseURL},
5354
}
5455

56+
c.Repo = &RepoService{client: c.common}
57+
5558
return c
5659
}
5760

0 commit comments

Comments
 (0)