Skip to content

Commit

Permalink
update gerritctl, include account group change
Browse files Browse the repository at this point in the history
  • Loading branch information
shijl0925 committed Jun 16, 2024
1 parent 7c02a1f commit f740c4d
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cli/gerritctl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $ pushd ~/.config/gerritctl/
$ vi config.json
{
"Url": "https://gerrit.mydomain.com",
"Username": "xxx",
"Account": "xxx",
"Password": "yyy"
}
$ popd
Expand Down
104 changes: 104 additions & 0 deletions cli/gerritctl/cmd/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cmd

import (
"fmt"
"github.com/shijl0925/go-gerrit"
"github.com/spf13/cobra"
"os"
)

// Account Commands
var account = &cobra.Command{
Use: "account",
Short: "account related commands",
}

var accountList = &cobra.Command{
Use: "list",
Short: "list accounts",
Run: func(cmd *cobra.Command, args []string) {
Limit, _ := cmd.Flags().GetInt("limit")
Start, _ := cmd.Flags().GetInt("start")
AdditionalFields, _ := cmd.Flags().GetStringSlice("additional_fields")

option := gerrit.QueryAccountOptions{}
option.Start = Start
option.Limit = Limit
option.Query = []string{"is:active"}
option.AdditionalFields = AdditionalFields //[]string{"DETAILS"}
accounts, _, err := gerritMod.Instance.Accounts.Query(gerritMod.Context, &option)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

for _, account := range *accounts {
fmt.Printf("✅ Account AccountID: %d.\n", account.AccountID)
if Verbose {
if out, err := ToIndentJSON(account); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", out)
}
}
}
},
}

var accountGet = &cobra.Command{
Use: "show",
Short: "Retrieve the account.",
Run: func(cmd *cobra.Command, args []string) {
accountID, _ := cmd.Flags().GetString("account_id")
account, _, err := gerritMod.Instance.Accounts.Get(gerritMod.Context, accountID)
if err != nil {
fmt.Printf("❌ Unable to find the specific account: %s.\n %v", accountID, err)
os.Exit(1)
}
fmt.Printf("✅ Account AccountID: %d.\n", account.Raw.AccountID)
if Verbose {
if out, err := ToIndentJSON(*account.Raw); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", out)
}
}
},
}

var accountCreate = &cobra.Command{
Use: "create",
Short: "Create a new account.",
Run: func(cmd *cobra.Command, args []string) {
email, _ := cmd.Flags().GetString("email")
username, _ := cmd.Flags().GetString("username")
input := gerrit.AccountInput{
Email: email,
Username: username,
}
if _, _, err := gerritMod.Instance.Accounts.Create(gerritMod.Context, username, &input); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("✅ Create new account,Email: %s.\n", email)
},
}

func init() {
rootCmd.AddCommand(account)

account.AddCommand(accountList)
accountList.Flags().IntP("limit", "l", 25, "Limit the number of accounts returned.")
accountList.Flags().IntP("start", "s", 0, "Skip the first N accounts.")
accountList.Flags().StringSliceP("additional_fields", "f", []string{}, "Additional fields to be returned.")

account.AddCommand(accountGet)
accountGet.Flags().StringP("account_id", "a", "", "The account ID.")
accountGet.MarkFlagRequired("account_id")

account.AddCommand(accountCreate)
accountCreate.Flags().StringP("email", "e", "", "The email address of the new account.")
accountCreate.Flags().StringP("username", "u", "", "The username of the new account.")
accountCreate.MarkFlagRequired("email")
accountCreate.MarkFlagRequired("username")
}
136 changes: 136 additions & 0 deletions cli/gerritctl/cmd/change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package cmd

import (
"fmt"
"github.com/shijl0925/go-gerrit"
"github.com/spf13/cobra"
"os"
)

// Change Commands
var change = &cobra.Command{
Use: "change",
Short: "change related commands",
}

var changeQuery = &cobra.Command{
Use: "query",
Short: "Query changes.",
Run: func(cmd *cobra.Command, args []string) {
Limit, _ := cmd.Flags().GetInt("limit")
Start, _ := cmd.Flags().GetInt("start")
Query, _ := cmd.Flags().GetStringSlice("query")
AdditionalFields, _ := cmd.Flags().GetStringSlice("additional_fields")

option := gerrit.QueryChangeOptions{}
option.Start = Start
option.Limit = Limit

option.Query = Query
option.AdditionalFields = AdditionalFields

changes, _, err := gerritMod.Instance.Changes.Query(gerritMod.Context, &option)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

for _, change := range *changes {
fmt.Printf("✅ Change ChangeID: %s.\n", change.ID)
if Verbose {
if out, err := ToIndentJSON(change); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", out)
}
}
}
},
}

var changeCreate = &cobra.Command{
Use: "create",
Short: "Create a new change.",
Run: func(cmd *cobra.Command, args []string) {
projectName, _ := cmd.Flags().GetString("project_name")
branchName, _ := cmd.Flags().GetString("branch_name")
subject, _ := cmd.Flags().GetString("subject")
input := gerrit.ChangeInput{
Project: projectName,
Branch: branchName,
Subject: subject,
}
if change, _, err := gerritMod.Instance.Changes.Create(gerritMod.Context, &input); err != nil {
fmt.Println(err)
os.Exit(1)
} else {
fmt.Printf("✅ Create new change,ChangeID: %s.\n", change.Raw.ID)
if Verbose {
if out, err := ToIndentJSON(change); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", out)
}
}
}
},
}

var changeGet = &cobra.Command{
Use: "show",
Short: "Retrieve a change.",
Run: func(cmd *cobra.Command, args []string) {
changeID, _ := cmd.Flags().GetString("change_id")
change, _, err := gerritMod.Instance.Changes.Get(gerritMod.Context, changeID)
if err != nil {
fmt.Printf("❌ Unable to find the specific change: %s.\n %v", changeID, err)
}
fmt.Printf("✅ Change ChangeID: %s.\n", change.Raw.ID)
if Verbose {
if out, err := ToIndentJSON(change); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", out)
}
}
},
}

var changeDelete = &cobra.Command{
Use: "delete",
Short: "Delete a change.",
Run: func(cmd *cobra.Command, args []string) {
changeID, _ := cmd.Flags().GetString("change_id")
if _, _, err := gerritMod.Instance.Changes.Delete(gerritMod.Context, changeID); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("✅ Delete change,ChangeID: %s.\n", changeID)
},
}

func init() {
rootCmd.AddCommand(change)

change.AddCommand(changeQuery)
changeQuery.Flags().IntP("limit", "l", 25, "limit")
changeQuery.Flags().IntP("start", "s", 0, "start")
changeQuery.Flags().StringSliceP("query", "q", []string{"is:open"}, "query")
changeQuery.Flags().StringSliceP("additional_fields", "a", []string{}, "additional fields")

change.AddCommand(changeCreate)
changeCreate.Flags().StringP("project_name", "p", "", "project name")
changeCreate.Flags().StringP("branch_name", "b", "", "branch name")
changeCreate.Flags().StringP("subject", "s", "", "subject")
changeCreate.MarkFlagRequired("project_name")
changeCreate.MarkFlagRequired("branch_name")
changeCreate.MarkFlagRequired("subject")

change.AddCommand(changeGet)
changeGet.Flags().StringP("change_id", "c", "", "change id")
changeGet.MarkFlagRequired("change_id")

change.AddCommand(changeDelete)
changeDelete.Flags().StringP("change_id", "c", "", "change id")
changeDelete.MarkFlagRequired("change_id")
}
95 changes: 95 additions & 0 deletions cli/gerritctl/cmd/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"fmt"
"github.com/shijl0925/go-gerrit"
"github.com/spf13/cobra"
"os"
)

// Group Commands
var group = &cobra.Command{
Use: "group",
Short: "group related commands",
}

var groupList = &cobra.Command{
Use: "list",
Short: "List the groups.",
Run: func(cmd *cobra.Command, args []string) {
Limit, _ := cmd.Flags().GetInt("limit")
Skip, _ := cmd.Flags().GetInt("skip")

option := gerrit.ListGroupsOptions{}
option.Skip = Skip
option.Limit = Limit

groups, _, err := gerritMod.Instance.Groups.List(gerritMod.Context, &option)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for name, group := range groups {
fmt.Printf("✅ Group Name: %s, GroupID: %d.\n", name, group.GroupID)
if Verbose {
if out, err := ToIndentJSON(group); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", out)
}
}
}
},
}

var groupCreate = &cobra.Command{
Use: "create",
Short: "Create a new group.",
Run: func(cmd *cobra.Command, args []string) {
groupName, _ := cmd.Flags().GetString("name")
input := gerrit.GroupInput{
Name: groupName,
}
if _, _, err := gerritMod.Instance.Groups.Create(gerritMod.Context, groupName, &input); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("✅ Create new group,Name: %s.\n", groupName)
},
}

var groupGet = &cobra.Command{
Use: "show",
Short: "Retrieve the group.",
Run: func(cmd *cobra.Command, args []string) {
groupID, _ := cmd.Flags().GetString("group_id")
group, _, err := gerritMod.Instance.Groups.Get(gerritMod.Context, groupID)
if err != nil {
fmt.Printf("❌ Unable to find the specific group: %s.\n %v", groupID, err)
}
fmt.Printf("✅ Group GroupID: %d.\n", group.Raw.GroupID)
if Verbose {
if out, err := ToIndentJSON(*group.Raw); err != nil {
fmt.Println(err)
} else {
fmt.Printf("%+v\n", out)
}
}
},
}

func init() {
rootCmd.AddCommand(group)

group.AddCommand(groupList)
groupList.Flags().IntP("limit", "l", 25, "limit")
groupList.Flags().IntP("skip", "s", 0, "skip")

group.AddCommand(groupCreate)
groupCreate.Flags().StringP("name", "n", "", "group name")
groupCreate.MarkFlagRequired("name")

group.AddCommand(groupGet)
groupGet.Flags().StringP("group_id", "g", "", "group id")
groupGet.MarkFlagRequired("group_id")
}
3 changes: 1 addition & 2 deletions cli/gerritctl/cmd/project.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"fmt"
"github.com/shijl0925/go-gerrit"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -385,7 +384,7 @@ func init() {

project.AddCommand(projectList)
projectList.Flags().BoolP("all", "a", false, "List all projects")
projectList.Flags().IntP("limit", "l", 0, "Limit the number of projects to be included in the results")
projectList.Flags().IntP("limit", "l", 25, "Limit the number of projects to be included in the results")
projectList.Flags().IntP("skip", "S", 0, "Skip the first N projects in the results")
projectList.Flags().BoolP("description", "d", false, "Include the project description in the results")
projectList.Flags().StringP("prefix", "p", "", "Only include projects with the given prefix")
Expand Down
8 changes: 4 additions & 4 deletions cli/gerritctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type GerritMod struct {
//
// Returns
func (g *GerritMod) Init(config Config) error {
g.Username = config.Username
g.Username = config.Account
g.Url = config.Url
g.Password = config.Password
g.Context = context.Background()
Expand All @@ -67,9 +67,9 @@ func (g *GerritMod) Init(config Config) error {

// Config is focused in the configuration json file
type Config struct {
Url string `mapstructure: Url`
Username string `mapstructure: Username`
Password string `mapstructure: Password`
Url string
Account string
Password string
ConfigPath string
ConfigFileName string
}
Expand Down

0 comments on commit f740c4d

Please sign in to comment.