This repository was archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Add repo command to access Repo entities
#322
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc0317b
Add 'RepoService' structure
0d565fc
Add 'list' subcommand
e4f2958
Add 'get' subcommand
0812730
Add 'update' subcommand
ce2127c
Add 'active' option to the 'update' subcommand
f546346
Add unit tests for RepoService
836f65f
Fix lint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the
--queryoption to filtering the JSON response