Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func main() {
commands.SchemaCommand,
commands.GrepCommand,
commands.ConfigCommand,
commands.IosCommand,
}
err = app.Run(os.Args)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions commands/ios.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package commands

import (
"fmt"

"github.com/gookit/color"
"github.com/pkg/browser"
"github.com/urfave/cli/v2"
)

const appStoreURL = "https://apps.apple.com/us/app/shelltime-xyz/id6757661383"

var IosCommand *cli.Command = &cli.Command{
Name: "ios",
Usage: "iOS app related commands",
Subcommands: []*cli.Command{
{
Name: "dl",
Usage: "open the ShellTime iOS app download page on the App Store",
Action: commandIosDl,
OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error {
color.Red.Println(err.Error())
return nil
},
},
},
OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error {
color.Red.Println(err.Error())
return nil
},
}
Comment on lines +13 to +31
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The OnUsageError handler function is duplicated for the main command and its subcommand. To improve maintainability and adhere to the Don't Repeat Yourself (DRY) principle, you can extract this logic into a shared function.

func onUsageError(cCtx *cli.Context, err error, isSubcommand bool) error {
	color.Red.Println(err.Error())
	return nil
}

var IosCommand *cli.Command = &cli.Command{
	Name:  "ios",
	Usage: "iOS app related commands",
	Subcommands: []*cli.Command{
		{
			Name:         "dl",
			Usage:        "open the ShellTime iOS app download page on the App Store",
			Action:       commandIosDl,
			OnUsageError: onUsageError,
		},
	},
	OnUsageError: onUsageError,
}


func commandIosDl(c *cli.Context) error {
color.Green.Printf("ShellTime iOS App Store URL:\n%s\n", appStoreURL)

err := browser.OpenURL(appStoreURL)
if err != nil {
fmt.Printf("Could not open browser automatically. Please visit the URL above to download.\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better command-line-tool citizenship, error or informational messages should be printed to standard error instead of standard output. This prevents polluting stdout, which might be piped to other commands. You can use c.App.ErrWriter, which is the idiomatic way to write to stderr in urfave/cli.

		fmt.Fprintf(c.App.ErrWriter, "Could not open browser automatically. Please visit the URL above to download.\n")

}

return nil
}
Loading