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
8 changes: 7 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ func main() {
Usage: "The authorization token.",
EnvVars: []string{"GITPLOY_TOKEN"},
},
&cli.StringFlag{
Name: "query",
Usage: "A GJSON query to use in filtering the response data",
},
Comment on lines +29 to +32
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the --query option to filtering the JSON response

},
Commands: []*cli.Command{
repoCommand,
},
Commands: []*cli.Command{},
}

err := app.Run(os.Args)
Expand Down
15 changes: 15 additions & 0 deletions cmd/cli/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

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

var repoCommand *cli.Command = &cli.Command{
Name: "repo",
Usage: "Manage repos.",
Subcommands: []*cli.Command{
repoListCommand,
repoGetCommand,
repoUpdateCommand,
},
}
40 changes: 40 additions & 0 deletions cmd/cli/repo_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"encoding/json"
"fmt"

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

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

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

output, err := json.MarshalIndent(repo, "", " ")
if err != nil {
return fmt.Errorf("Failed to marshal: %w", err)
}

if q := cli.String("query"); q != "" {
fmt.Println(gjson.GetBytes(output, q))
return nil
}

fmt.Println(string(output))
return nil
},
}
67 changes: 67 additions & 0 deletions cmd/cli/repo_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"encoding/json"
"fmt"

"github.com/tidwall/gjson"
"github.com/urfave/cli/v2"

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

var repoListCommand = &cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "Show own repositories.",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "all",
Usage: "Show all repositories.",
},
&cli.IntFlag{
Name: "page",
Value: 1,
Usage: "The page of list.",
},
&cli.IntFlag{
Name: "per-page",
Value: 30,
Usage: "The item count per page.",
},
},
Action: func(cli *cli.Context) error {
c := buildClient(cli)

var (
repos []*ent.Repo
err error
)

if cli.Bool("all") {
if repos, err = c.Repo.ListAll(cli.Context); err != nil {
return err
}
} else {
if repos, err = c.Repo.List(cli.Context, api.RepoListOptions{
ListOptions: api.ListOptions{Page: cli.Int("page"), PerPage: cli.Int("per-page")},
}); err != nil {
return err
}
}

output, err := json.MarshalIndent(repos, "", " ")
if err != nil {
return fmt.Errorf("Failed to marshal: %w", err)
}

if q := cli.String("query"); q != "" {
fmt.Println(gjson.GetBytes(output, q))
return nil
}

fmt.Println(string(output))
return nil
},
}
70 changes: 70 additions & 0 deletions cmd/cli/repo_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"encoding/json"
"fmt"
"strconv"

"github.com/AlekSi/pointer"
"github.com/gitploy-io/gitploy/pkg/api"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v2"
)

var repoUpdateCommand = &cli.Command{
Name: "update",
Usage: "Update the repository.",
ArgsUsage: "<owner>/<repo>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"C"},
Usage: "The path of configuration file.",
},
&cli.StringFlag{
Name: "active",
Aliases: []string{"A"},
Usage: "Activate or deactivate the repository. Ex 'true', 'false'",
},
},
Action: func(cli *cli.Context) error {
ns, n, err := splitFullName(cli.Args().First())
if err != nil {
return err
}

// Build the request body.
req := api.RepoUpdateRequest{}
if config := cli.String("config"); config != "" {
req.ConfigPath = pointer.ToString(config)
}

if active := cli.String("active"); active != "" {
b, err := strconv.ParseBool(active)
if err != nil {
return fmt.Errorf("'%s' is invalid format: %w", active, err)
}

req.Active = pointer.ToBool(b)
}

c := buildClient(cli)
repo, err := c.Repo.Update(cli.Context, ns, n, req)
if err != nil {
return err
}

output, err := json.MarshalIndent(repo, "", " ")
if err != nil {
return fmt.Errorf("Failed to marshal: %w", err)
}

if q := cli.String("query"); q != "" {
fmt.Println(gjson.GetBytes(output, q))
return nil
}

fmt.Println(string(output))
return nil
},
}
30 changes: 30 additions & 0 deletions cmd/cli/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"strings"

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

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

// buildClient returns a client to interact with a server.
func buildClient(cli *cli.Context) *api.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cli.String("token")},
)
tc := oauth2.NewClient(cli.Context, ts)

return api.NewClient(cli.String("host"), tc)
}

func splitFullName(name string) (string, string, error) {
ss := strings.Split(name, "/")
if len(ss) != 2 {
return "", "", fmt.Errorf("'%s' is invalid format", name)
}

return ss[0], ss[1], nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ 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/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
github.com/urfave/cli/v2 v2.3.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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/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=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E=
Expand Down
9 changes: 6 additions & 3 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type (
common *client

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

client struct {
Expand All @@ -31,9 +32,9 @@ type (
BaseURL *url.URL
}

// service struct {
// client *client
// }
service struct {
*client
}

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

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

return c
}

Expand Down
Loading