-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: command help improvement (#118)
- Loading branch information
1 parent
283ee62
commit 551de68
Showing
3 changed files
with
92 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,140 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/jessevdk/go-flags" | ||
|
||
"github.com/guyfedwards/nom/v2/internal/commands" | ||
"github.com/guyfedwards/nom/v2/internal/config" | ||
"github.com/guyfedwards/nom/v2/internal/store" | ||
"github.com/jessevdk/go-flags" | ||
) | ||
|
||
type Options struct { | ||
Verbose bool `short:"v" long:"verbose" description:"Show verbose logging"` | ||
Number int `short:"n" long:"number" description:"Number of results to show"` | ||
Pager string `short:"p" long:"pager" description:"Pager to use for longer output. Set to false for no pager"` | ||
ConfigPath string `long:"config-path" description:"Location of config.yml"` | ||
PreviewFeeds []string `short:"f" long:"feed" description:"Feed(s) URL(s) for preview"` | ||
Version bool `long:"version" description:"Display version information"` | ||
} | ||
|
||
var ( | ||
version = "dev" | ||
ErrNotEnoughArgs = errors.New("not enough args") | ||
options Options | ||
version = "dev" | ||
) | ||
|
||
func run(args []string, opts Options) error { | ||
cfg, err := config.New(opts.ConfigPath, opts.Pager, opts.PreviewFeeds, version) | ||
// Setup subcommands | ||
|
||
type Add struct { | ||
Positional struct { | ||
Url string `positional-arg-name:"URL" required:"yes"` | ||
} `positional-args:"yes"` | ||
} | ||
|
||
func (r *Add) Execute(args []string) error { | ||
cmds, err := getCmds() | ||
if err != nil { | ||
return err | ||
} | ||
return cmds.Add(r.Positional.Url) | ||
} | ||
|
||
if err = cfg.Load(); err != nil { | ||
type Config struct{} | ||
|
||
func (r *Config) Execute(args []string) error { | ||
cmds, err := getCmds() | ||
if err != nil { | ||
return err | ||
} | ||
return cmds.ShowConfig() | ||
} | ||
|
||
s, err := store.NewSQLiteStore(cfg.ConfigDir) | ||
type List struct{} | ||
|
||
func (r *List) Execute(args []string) error { | ||
cmds, err := getCmds() | ||
if err != nil { | ||
return fmt.Errorf("main.go: %w", err) | ||
return err | ||
} | ||
cmds := commands.New(cfg, s) | ||
|
||
if opts.Version || (len(args) > 0 && args[0] == "version") { | ||
fmt.Printf("%s\n", version) | ||
return nil | ||
return cmds.List() | ||
} | ||
|
||
type Version struct{} | ||
|
||
func (r *Version) Execute(args []string) error { | ||
_, err := getCmds() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// no subcommand, run the TUI | ||
if len(args) == 0 { | ||
return cmds.TUI() | ||
fmt.Printf("%s\n", version) | ||
return nil | ||
} | ||
|
||
type Refresh struct{} | ||
|
||
func (r *Refresh) Execute(args []string) error { | ||
cmds, err := getCmds() | ||
if err != nil { | ||
return err | ||
} | ||
return cmds.Refresh() | ||
} | ||
|
||
switch args[0] { | ||
case "list": | ||
return cmds.List(opts.Number) | ||
case "refresh": | ||
return cmds.Refresh() | ||
case "config": | ||
return cmds.ShowConfig() | ||
case "add": | ||
if len(args) != 2 { | ||
return ErrNotEnoughArgs | ||
} | ||
func getCmds() (*commands.Commands, error) { | ||
cfg, err := config.New(options.ConfigPath, options.Pager, options.PreviewFeeds, version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cmds.Add(args[1]) | ||
if err = cfg.Load(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return nil | ||
s, err := store.NewSQLiteStore(cfg.ConfigDir) | ||
if err != nil { | ||
return nil, fmt.Errorf("main.go: %w", err) | ||
} | ||
cmds := commands.New(cfg, s) | ||
return &cmds, nil | ||
} | ||
|
||
func main() { | ||
var opts Options | ||
parser := flags.NewParser(&options, flags.Default) | ||
// allow nom to be run without any subcommands | ||
parser.SubcommandsOptional = true | ||
|
||
// add commands | ||
parser.AddCommand("add", "Add feed", "Add a new feed", &Add{}) | ||
parser.AddCommand("config", "Show config", "Show configuration", &Config{}) | ||
parser.AddCommand("list", "List feeds", "List all feeds", &List{}) | ||
parser.AddCommand("version", "Show Vesion", "Display version information", &Version{}) | ||
parser.AddCommand("refresh", "Refresh feeds", "refresh feed(s) without opening TUI", &Refresh{}) | ||
|
||
parser := flags.NewParser(&opts, flags.Default) | ||
// parse the command line arguments | ||
_, err := parser.Parse() | ||
|
||
args, err := parser.Parse() | ||
// check for help flag | ||
if err != nil { | ||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { | ||
os.Exit(0) | ||
} else { | ||
fmt.Printf("%v\n", err) | ||
os.Exit(1) | ||
if flagErr, ok := err.(*flags.Error); ok && flagErr.Type != flags.ErrHelp { | ||
parser.WriteHelp(os.Stdout) | ||
} | ||
return | ||
|
||
os.Exit(0) | ||
} | ||
|
||
if err := run(args, opts); err != nil { | ||
if opts.Verbose || errors.Is(err, config.ErrFeedAlreadyExists) { | ||
fmt.Printf("%v\n", err) | ||
// no subcommand or help flag, run the TUI | ||
if parser.Active == nil { | ||
cmds, err := getCmds() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
parser.WriteHelp(os.Stderr) | ||
os.Exit(1) | ||
err = cmds.TUI() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters