Skip to content

Commit

Permalink
Use cobra feature to check Args
Browse files Browse the repository at this point in the history
Using a cobra feature allow a reduction of code.
  • Loading branch information
Guilhem Bonnefille committed Nov 14, 2024
1 parent 99d951d commit 9671b4a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 16 deletions.
11 changes: 3 additions & 8 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,13 @@ var newCmd = &cobra.Command{
Use: "new [path]",
Short: "Create new content for a campaign",
Example: "paperboy new the-announcement.md",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}

if len(args) < 1 {
return newUserError("please provide a path")
}

path := cfg.AppFs.ContentPath(args[0])
return writeTemplate(cfg.AppFs, path, contentTemplate, map[string]string{
"Date": time.Now().Format(time.RFC3339),
Expand All @@ -94,16 +91,13 @@ var newListCmd = &cobra.Command{
Use: "list [path]",
Short: "Create a new recipient list",
Example: "paperboy new list in-the-know",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}

if len(args) < 1 {
return newUserError("please provide a path")
}

path := cfg.AppFs.ListPath(args[0])
return writeTemplate(cfg.AppFs, path, listTemplate, nil, false)
},
Expand All @@ -112,6 +106,7 @@ var newListCmd = &cobra.Command{
var newProjectCmd = &cobra.Command{
Use: "project [path]",
Short: "Create new project directory",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
path := "."
if len(args) > 0 {
Expand Down
5 changes: 1 addition & 4 deletions cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ const (
var previewCmd = &cobra.Command{
Use: "preview [content] [list]",
Short: "Preview campaign in browser",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}

if len(args) != 2 {
return newUserError("Invalid arguments")
}

// Start server, notifies channel when listening
return startAPIServer(cfg, func(mux *http.ServeMux, serverReady chan bool) error {

Expand Down
5 changes: 1 addition & 4 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var sendCmd = &cobra.Command{
Use: "send [content] [list]",
Short: "Send campaign to recipients",
Example: "paperboy send the-announcement in-the-know",
Args: cobra.ExactArgs(2),
// Uncomment the following line if your bare application
// has an action associated with it:
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -23,10 +24,6 @@ var sendCmd = &cobra.Command{
return err
}

if len(args) != 2 {
return newUserError("Invalid arguments")
}

ctx := withSignalTrap(context.Background())
return mail.LoadAndSendCampaign(ctx, cfg, args[0], args[1])
},
Expand Down

0 comments on commit 9671b4a

Please sign in to comment.