Skip to content

Commit

Permalink
Add the --repo/-R option (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriobelli committed Mar 8, 2023
1 parent c275ab1 commit b771909
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# gh-milestone

## 2.1.0

### Minor Changes

- Add --repo/-R option to every command. Resolves [#15](https://github.com/valeriobelli/gh-milestone/issues/15)

## 2.0.1

### Patch Changes
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ gh milestone ls
# List closed Milestones
gh milestone list --state closed

# List milestones of specific repo
gh milestone list --repo valeriobelli/foo-bar

# Search by a pattern
gh milestone list --query "Foo bar"

Expand All @@ -54,12 +57,15 @@ gh milestone list --json id,progressPercentage --json number --jq ".[0].id"

```bash
gh milestone edit <milestone number> --title "New title"
gh milestone edit <milestone number> --title "New title" --repo valeriobelli/foo-bar
```

### View a milestone

```bash
gh milestone view <milestone number>

gh milestone view <milestone number> --repo valeriobelli/foo-bar
```

### Delete milestone
Expand All @@ -70,4 +76,5 @@ gh milestone delete <milestone number>

# Automatic
gh milestone delete <milestone number> --confirm
```
gh milestone delete <milestone number> --confirm --repo valeriobelli/foo-bar
```
3 changes: 2 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/valeriobelli/gh-milestone/internal/pkg/utils/cmdutil"
)

const version = "v2.0.1"
const version = "v2.1.0"

func Execute() {
var rootCommand = &cobra.Command{
Expand All @@ -33,6 +33,7 @@ func Execute() {
rootCommand.SetUsageFunc(cmdutil.UsageFunction)

rootCommand.Flags().BoolP("version", "v", false, "Print the version of this extension")
rootCommand.PersistentFlags().StringP("repo", "R", "", "Select another repository using the [HOST/]OWNER/REPO format")

rootCommand.AddCommand(newCreateCommand())
rootCommand.AddCommand(newDeleteCommand())
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ func newCreateCommand() *cobra.Command {

description, _ := command.Flags().GetString("description")
title, _ := command.Flags().GetString("title")
repo, _ := command.Parent().PersistentFlags().GetString("repo")

return create.NewCreateMilestone(create.CreateMilestoneConfig{
Description: description,
DueDate: dueDate,
Repo: repo,
Title: title,
}).Execute()
},
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func newDeleteCommand() *cobra.Command {
milestoneId, _ := strconv.Atoi(args[0])

confirm, _ := command.Flags().GetBool("confirm")
repo, _ := command.Parent().PersistentFlags().GetString("repo")

return delete.NewDeleteMilestone(delete.DeleteMilestoneConfig{
Confirm: confirm,
Repo: repo,
}).Execute(milestoneId)
},
Args: func(command *cobra.Command, args []string) error {
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func newEditCommand() *cobra.Command {
description := description.GetValue()
state := state.GetValue()
title := title.GetValue()
repo, _ := command.Parent().PersistentFlags().GetString("repo")

if dueDate == nil && description == nil && state == nil && title == nil {
fmt.Println("Nothing to edit, exiting.")
Expand All @@ -55,6 +56,7 @@ func newEditCommand() *cobra.Command {
return edit.NewEditMilestone(edit.EditMilestoneConfig{
Description: description,
DueDate: dueDate,
Repo: repo,
State: state,
Title: title,
}).Execute(milestoneNumber)
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func newListCommand() *cobra.Command {
jsonFields, _ := command.Flags().GetStringSlice("json")
query, _ := command.Flags().GetString("query")
jq, _ := command.Flags().GetString("jq")
repo, _ := command.Parent().PersistentFlags().GetString("repo")

return list.NewListMilestones(list.ListMilestonesConfig{
Query: query,
Expand All @@ -94,6 +95,7 @@ func newListCommand() *cobra.Command {
First: first,
Jq: jq,
Json: jsonFields,
Repo: repo,
State: strings.ToUpper(state.String()),
}).Execute()
},
Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ func newViewCommand() *cobra.Command {

milestoneNumber, _ := strconv.Atoi(args[0])

repo, _ := command.Parent().PersistentFlags().GetString("repo")
web, _ := command.Flags().GetBool("web")

return view.NewViewMilestone(view.ViewMilestoneConfig{
Web: web,
Repo: repo,
Web: web,
}).Execute(milestoneNumber)
},
Args: func(command *cobra.Command, args []string) error {
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/application/create/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type CreateMilestoneConfig struct {
Description string
DueDate *time.Time
Repo string
Title string
}

Expand All @@ -27,7 +28,7 @@ func NewCreateMilestone(config CreateMilestoneConfig) *CreateMilestone {
}

func (cm CreateMilestone) Execute() error {
repoInfo, err := gh.RetrieveRepoInformation()
repoInfo, err := gh.RetrieveRepoInformation(cm.config.Repo)

if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/application/delete/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type DeleteMilestoneConfig struct {
Confirm bool
Repo string
}

type DeleteMilestone struct {
Expand All @@ -24,7 +25,7 @@ func NewDeleteMilestone(config DeleteMilestoneConfig) *DeleteMilestone {
}

func (em DeleteMilestone) Execute(number int) error {
repoInfo, err := gh.RetrieveRepoInformation()
repoInfo, err := gh.RetrieveRepoInformation(em.config.Repo)

if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/application/edit/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type EditMilestoneConfig struct {
Description *string
DueDate *time.Time
Repo string
State *string
Title *string
}
Expand All @@ -28,7 +29,7 @@ func NewEditMilestone(config EditMilestoneConfig) *EditMilestone {
}

func (em EditMilestone) Execute(number int) error {
repoInfo, err := gh.RetrieveRepoInformation()
repoInfo, err := gh.RetrieveRepoInformation(em.config.Repo)

if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/application/list/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ListMilestonesConfig struct {
OrderBy MilestonesOrderBy
Json []string
Query string
Repo string
State string
}

Expand All @@ -52,7 +53,7 @@ func NewListMilestones(config ListMilestonesConfig) *ListMilestones {
}

func (l ListMilestones) Execute() error {
repoInfo, err := gh.RetrieveRepoInformation()
repoInfo, err := gh.RetrieveRepoInformation(l.config.Repo)

if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/application/view/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var query struct {
}

type ViewMilestoneConfig struct {
Web bool
Repo string
Web bool
}

type ViewMilestone struct {
Expand All @@ -37,7 +38,7 @@ func NewViewMilestone(config ViewMilestoneConfig) *ViewMilestone {
}

func (vm ViewMilestone) Execute(number int) error {
repoInfo, err := gh.RetrieveRepoInformation()
repoInfo, err := gh.RetrieveRepoInformation(vm.config.Repo)

if err != nil {
return err
Expand Down
39 changes: 38 additions & 1 deletion internal/pkg/infrastructure/gh/retrieve_repo_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gh

import (
"errors"
"fmt"
"regexp"
"strings"
)

Expand All @@ -10,7 +12,42 @@ type RepoInfo struct {
Owner string
}

func RetrieveRepoInformation() (*RepoInfo, error) {
func extractRepoInformationFromRepoString(repoString string) (*RepoInfo, error) {
if repoString == "" {
return nil, nil
}

pattern, err := regexp.Compile("(?:(?:https://www.|https://|www.)?github.com/)?([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)")

if err != nil {
return nil, err
}

subStr := pattern.FindStringSubmatch(repoString)

if len(subStr) == 0 {
return nil, fmt.Errorf("the repo option \"%s\" does not respect the format [HOST/]OWNER/REPO", repoString)
}

fmt.Printf("%v", subStr)

return &RepoInfo{
Name: subStr[2],
Owner: subStr[1],
}, nil
}

func RetrieveRepoInformation(repoString string) (*RepoInfo, error) {
repoInfoFromRepoString, err := extractRepoInformationFromRepoString(repoString)

if err != nil {
return nil, err
}

if repoInfoFromRepoString != nil {
return repoInfoFromRepoString, nil
}

owner, err := Execute([]string{"repo", "view", "--json", "owner", "--jq", ".owner.login"})

if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/utils/cmdutil/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func HelpFunction(command *cobra.Command, args []string) {
bold.Println("FLAGS")
fmt.Print(command.LocalFlags().FlagUsages())

fmt.Println()

bold.Println("INHERITED FLAGS")
fmt.Print(command.InheritedFlags().FlagUsages())

if command.Example != "" {
fmt.Println()
bold.Println("EXAMPLES")
Expand Down

0 comments on commit b771909

Please sign in to comment.