Skip to content

Commit 1327248

Browse files
committed
Enhance commands interface
1 parent bfcaac2 commit 1327248

File tree

27 files changed

+809
-421
lines changed

27 files changed

+809
-421
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# gh-milestone
22

3+
## 1.1.0
4+
5+
### Minor Changes
6+
7+
- Enhance the interface of commands. Strict values are now validated and better error messages are emitted.
8+
39
## 1.0.0
410

511
### Major Changes

internal/cmd/cmd.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/valeriobelli/gh-milestone/internal/pkg/utils/cmdutil"
9+
)
10+
11+
const version = "v1.1.0"
12+
13+
func Execute() {
14+
var rootCommand = &cobra.Command{
15+
Use: "milestone",
16+
Short: "Manage with Github Milestones",
17+
Long: "Work with Github milestones.",
18+
SilenceErrors: true,
19+
RunE: func(command *cobra.Command, args []string) error {
20+
versionFlag, _ := command.Flags().GetBool("version")
21+
22+
if versionFlag {
23+
fmt.Println(version)
24+
25+
return nil
26+
}
27+
28+
return command.Help()
29+
},
30+
}
31+
32+
rootCommand.SetHelpFunc(cmdutil.HelpFunction)
33+
rootCommand.SetUsageFunc(cmdutil.UsageFunction)
34+
35+
rootCommand.Flags().BoolP("version", "v", false, "Print the version of this extension")
36+
37+
rootCommand.AddCommand(newCreateCommand())
38+
rootCommand.AddCommand(newDeleteCommand())
39+
rootCommand.AddCommand(newEditCommand())
40+
rootCommand.AddCommand(newListCommand())
41+
rootCommand.AddCommand(newViewCommand())
42+
43+
if err := rootCommand.Execute(); err != nil {
44+
fmt.Println(err)
45+
46+
os.Exit(1)
47+
}
48+
}

internal/cmd/create.go

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,61 @@ package cmd
22

33
import (
44
"fmt"
5-
"time"
65

76
"github.com/MakeNowJust/heredoc/v2"
87
"github.com/spf13/cobra"
98
"github.com/valeriobelli/gh-milestone/internal/pkg/application/create"
9+
commands_create "github.com/valeriobelli/gh-milestone/internal/pkg/domain/commands/create"
1010
"github.com/valeriobelli/gh-milestone/internal/pkg/domain/constants"
11-
"github.com/valeriobelli/gh-milestone/internal/pkg/domain/github"
11+
"github.com/valeriobelli/gh-milestone/internal/pkg/utils/cmdutil"
1212
)
1313

14-
func NewCreateCommand() *cobra.Command {
15-
var getDescription = func(command *cobra.Command) string {
16-
description, err := command.Flags().GetString("description")
17-
18-
if err != nil {
19-
return ""
20-
}
21-
22-
return description
23-
}
24-
25-
var getDueDate = func(command *cobra.Command) (*time.Time, error) {
26-
dueDate, err := command.Flags().GetString("due-date")
27-
28-
if err != nil {
29-
return nil, err
30-
}
31-
32-
if dueDate == "" {
33-
return nil, nil
34-
}
35-
36-
parsedDate, err := github.NewDueDate(dueDate)
37-
38-
if err != nil {
39-
return nil, err
40-
}
41-
42-
return &parsedDate.Time, nil
43-
}
44-
45-
var getTitle = func(command *cobra.Command) string {
46-
title, err := command.Flags().GetString("title")
47-
48-
if err != nil {
49-
return ""
50-
}
51-
52-
return title
53-
}
14+
func newCreateCommand() *cobra.Command {
15+
dueDate := commands_create.NewDueDateFlag()
5416

5517
createCommand := &cobra.Command{
5618
Use: "create",
5719
Short: "Create a milestone",
58-
Long: heredoc.Doc(`
59-
Create a mileston on Github.
60-
61-
Optionally, this command permits to create a Milestone interactively when flags
62-
for required fields are not defined.
63-
`),
6420
Example: heredoc.Doc(`
6521
# create a new milestone with a title by using flags
66-
$ gh milestones create --title v1.0.0
22+
$ gh milestone create --title v1.0.0
6723
6824
# create a new milestone by using flags
69-
gh milestones create --title v1.0.0 --description "# This is a description" --due-date 2022-06-01
25+
gh milestone create --title v1.0.0 --description "# This is a description" --due-date 2022-06-01
7026
7127
# create a new milestone interactively
72-
gh milestones create
28+
gh milestone create
7329
`),
74-
Run: func(command *cobra.Command, args []string) {
75-
description := getDescription(command)
76-
dueDate, err := getDueDate(command)
77-
title := getTitle(command)
30+
Long: heredoc.Doc(`
31+
Create a milestone on Github.
7832
79-
if err != nil {
80-
fmt.Println(err.Error())
33+
Optionally, this command permits to create a Milestone interactively when flags
34+
for required fields are not defined.
35+
`),
36+
RunE: func(command *cobra.Command, args []string) error {
37+
dueDate, err := dueDate.GetValue()
8138

82-
return
39+
if err != nil {
40+
return err
8341
}
8442

85-
create.NewCreateMilestone(create.CreateMilestoneConfig{
43+
description, _ := command.Flags().GetString("description")
44+
title, _ := command.Flags().GetString("title")
45+
46+
return create.NewCreateMilestone(create.CreateMilestoneConfig{
8647
Description: description,
8748
DueDate: dueDate,
8849
Title: title,
8950
}).Execute()
9051
},
9152
}
9253

54+
createCommand.SetHelpFunc(cmdutil.HelpFunction)
55+
createCommand.SetUsageFunc(cmdutil.UsageFunction)
56+
9357
createCommand.Flags().StringP("description", "d", "", "Set the description")
94-
createCommand.Flags().StringP("due-date", "u", "", fmt.Sprintf("Set the due date [%s]", constants.DateFormat))
58+
createCommand.Flags().VarP(dueDate, "due-date", "u", fmt.Sprintf("Set the due date [%s]", constants.DateFormat))
9559
createCommand.Flags().StringP("title", "t", "", "Set the title")
9660

9761
return createCommand
9862
}
99-
100-
func init() {
101-
rootCommand.AddCommand(NewCreateCommand())
102-
}

internal/cmd/delete.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
11
package cmd
22

33
import (
4-
"errors"
54
"fmt"
65
"strconv"
76

7+
"github.com/MakeNowJust/heredoc/v2"
88
"github.com/spf13/cobra"
99
"github.com/valeriobelli/gh-milestone/internal/pkg/application/delete"
10+
"github.com/valeriobelli/gh-milestone/internal/pkg/utils/cmdutil"
1011
)
1112

12-
func NewDeleteCommand() *cobra.Command {
13+
func newDeleteCommand() *cobra.Command {
1314
deleteCommand := &cobra.Command{
14-
Use: "delete",
15-
Short: "Delete milestone",
16-
Run: func(command *cobra.Command, args []string) {
17-
if len(args) == 0 {
18-
command.Help()
19-
20-
return
15+
Use: "delete",
16+
Example: heredoc.Doc(`
17+
# autoconfirm the deletion
18+
$ gh milestone delete 42 --confirm
19+
`),
20+
Long: "Delete a milestone",
21+
Short: "Delete a milestone",
22+
RunE: func(command *cobra.Command, args []string) error {
23+
if len(args) < 1 {
24+
return command.Help()
2125
}
2226

23-
milestoneNumber, err := strconv.Atoi(args[0])
24-
25-
if err != nil {
26-
fmt.Println(err.Error())
27-
28-
return
29-
}
27+
milestoneId, _ := strconv.Atoi(args[0])
3028

3129
confirm, _ := command.Flags().GetBool("confirm")
3230

33-
delete.NewDeleteMilestone(delete.DeleteMilestoneConfig{
31+
return delete.NewDeleteMilestone(delete.DeleteMilestoneConfig{
3432
Confirm: confirm,
35-
}).Execute(milestoneNumber)
33+
}).Execute(milestoneId)
3634
},
37-
Args: func(cmd *cobra.Command, args []string) error {
35+
Args: func(command *cobra.Command, args []string) error {
3836
if len(args) < 1 {
3937
return nil
4038
}
4139

42-
_, err := strconv.Atoi(args[0])
40+
milestoneId := args[0]
41+
42+
_, err := strconv.Atoi(milestoneId)
4343

4444
if err != nil {
45-
return errors.New("A numeric identifier is needed to delete the milestone")
45+
return fmt.Errorf(
46+
"the value \"%s\" is not a valid numeric identifier needed to edit a Milestone",
47+
milestoneId,
48+
)
4649
}
4750

4851
return nil
4952
},
5053
}
5154

55+
deleteCommand.SetHelpFunc(cmdutil.HelpFunction)
56+
deleteCommand.SetUsageFunc(cmdutil.UsageFunction)
57+
5258
deleteCommand.Flags().BoolP("confirm", "c", false, "Confirm deletion without prompting")
5359

5460
return deleteCommand
5561
}
56-
57-
func init() {
58-
rootCommand.AddCommand(NewDeleteCommand())
59-
}

0 commit comments

Comments
 (0)