From c434005d39ff20f87d96220337f21bffdbe9a495 Mon Sep 17 00:00:00 2001 From: bth Date: Tue, 20 Jan 2026 13:30:53 -0800 Subject: [PATCH] cmd pkg for Subcommand type, use everywhere --- cmd/cmd.go | 11 +++++++++++ flags/flags.go | 14 ++++++++------ next/last.go | 7 +++---- next/next.go | 15 ++++++++------- sort/sort.go | 5 +++-- tag/tag.go | 4 +++- um.go | 41 ++++++++++++++++------------------------- 7 files changed, 52 insertions(+), 45 deletions(-) create mode 100644 cmd/cmd.go diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..f38ae64 --- /dev/null +++ b/cmd/cmd.go @@ -0,0 +1,11 @@ +package cmd + +type Subcommand string + +const ( + Tag Subcommand = "tag" + Next Subcommand = "next" + Last Subcommand = "last" + Sort Subcommand = "sort" + Help Subcommand = "help" +) diff --git a/flags/flags.go b/flags/flags.go index cbf6a0b..559b20f 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -7,6 +7,8 @@ import ( "reflect" "strings" "text/tabwriter" + + "github.com/brtholomy/um/go/cmd" ) type Arg struct { @@ -74,17 +76,17 @@ func ValidateIncrementFetchOrExit(args []string, i int) (int, string) { return i + 1, args[i+1] } -func HelpRequired(cmd string, long string) { - log.Printf("um %s: %s is required", cmd, long) +func HelpRequired(sub cmd.Subcommand, long string) { + log.Printf("um %s: %s is required", sub, long) } -func HelpInvalidArg(cmd string, arg string) { - log.Printf("um %s: invalid argument: %s", cmd, arg) +func HelpInvalidArg(sub cmd.Subcommand, arg string) { + log.Printf("um %s: invalid argument: %s", sub, arg) } // print out help string by reflecting over fields of provided opts struct // and os.Exit(0) -func Help(subcmd string, opts any) { +func Help(sub cmd.Subcommand, opts any) { v := reflect.ValueOf(opts) t := v.Type() @@ -110,7 +112,7 @@ func Help(subcmd string, opts any) { ) } } - log.Printf("um %s%s\n", subcmd, positional) + log.Printf("um %s%s\n", sub, positional) w.Flush() os.Exit(0) } diff --git a/next/last.go b/next/last.go index c8e5ad7..280a45d 100644 --- a/next/last.go +++ b/next/last.go @@ -8,10 +8,9 @@ import ( "os/exec" "regexp" "strings" -) -// but this stinks: -const lcmd = "last" + cmdpkg "github.com/brtholomy/um/go/cmd" +) const ( // NOTE: the -r flag: @@ -47,7 +46,7 @@ func last() (string, error) { func Last() { s, err := last() if err != nil { - log.Fatalf("um %s: %v", lcmd, err) + log.Fatalf("um %s: %v", cmdpkg.Last, err) } // send to stdout, not stderr as is default for log.Print: fmt.Println(s) diff --git a/next/next.go b/next/next.go index 03fb680..3d40568 100644 --- a/next/next.go +++ b/next/next.go @@ -7,10 +7,11 @@ import ( "os/exec" "strconv" + cmdpkg "github.com/brtholomy/um/go/cmd" "github.com/brtholomy/um/go/flags" ) -const cmd = "next" +const this = cmdpkg.Next type options struct { Descriptor flags.Arg @@ -35,10 +36,10 @@ func parseArgs(args []string) options { case i == 1 && !flags.HasDashPrefix(arg): opts.Tags.Val = arg case arg == opts.Help.Long || arg == opts.Help.Short: - flags.Help(cmd, opts) + flags.Help(this, opts) default: - flags.HelpInvalidArg(cmd, arg) - flags.Help(cmd, opts) + flags.HelpInvalidArg(this, arg) + flags.Help(this, opts) } } return opts @@ -109,13 +110,13 @@ func Next(args []string) { opts := parseArgs(args) l, err := last() if err != nil { - log.Fatalf("um %s: %v", cmd, err) + log.Fatalf("um %s: %v", this, err) } filename, err := next(l, opts.Descriptor.Val) if err != nil { - log.Fatalf("um %s: %v", cmd, err) + log.Fatalf("um %s: %v", this, err) } if err := emacsNext(filename, opts.Tags.Val); err != nil { - log.Fatalf("um %s: %v", cmd, err) + log.Fatalf("um %s: %v", this, err) } } diff --git a/sort/sort.go b/sort/sort.go index 5f3f0f4..5bd96ba 100644 --- a/sort/sort.go +++ b/sort/sort.go @@ -7,11 +7,12 @@ import ( "slices" "strings" + cmdpkg "github.com/brtholomy/um/go/cmd" "github.com/brtholomy/um/go/flags" "github.com/brtholomy/um/go/pipe" ) -const cmd = "sort" +const cmd = cmdpkg.Sort type options struct { Source flags.Flag[string] @@ -43,7 +44,7 @@ func parseArgs(args []string) options { case arg == opts.Help.Long || arg == opts.Help.Short: flags.Help(cmd, opts) default: - log.Printf("um sort: invalid argument: %s", arg) + flags.HelpInvalidArg(cmd, arg) flags.Help(cmd, opts) } } diff --git a/tag/tag.go b/tag/tag.go index 1a9b25d..280a7b8 100644 --- a/tag/tag.go +++ b/tag/tag.go @@ -3,10 +3,12 @@ package tag import ( "log" + cmdpkg "github.com/brtholomy/um/go/cmd" "github.com/brtholomy/um/go/flags" ) -const cmd = "tag" +// just so that copied calls into flags.Help don't have to be adjusted between files: +const cmd = cmdpkg.Tag type options struct { Query flags.Arg diff --git a/um.go b/um.go index cbc5e85..7ee58fd 100644 --- a/um.go +++ b/um.go @@ -5,22 +5,13 @@ import ( "log" "os" - nextpkg "github.com/brtholomy/um/go/next" - sortpkg "github.com/brtholomy/um/go/sort" - tagpkg "github.com/brtholomy/um/go/tag" + "github.com/brtholomy/um/go/cmd" + "github.com/brtholomy/um/go/next" + "github.com/brtholomy/um/go/sort" + "github.com/brtholomy/um/go/tag" ) -type Subcommand string - -const ( - tag Subcommand = "tag" - next Subcommand = "next" - last Subcommand = "last" - sort Subcommand = "sort" - help Subcommand = "help" -) - -var helpShort string = fmt.Sprintf("um [%s | %s | %s | %s | %s]", tag, next, last, sort, help) +var helpShort string = fmt.Sprintf("um [%s | %s | %s | %s | %s]", cmd.Tag, cmd.Next, cmd.Last, cmd.Sort, cmd.Help) var helpLong string = fmt.Sprintf(`%s An (U)ltralight database for (M)arkdown composition. @@ -40,19 +31,19 @@ func main() { log.Fatalln(helpShort) } - cmd := Subcommand(os.Args[1]) + arg := cmd.Subcommand(os.Args[1]) args := os.Args[2:] - switch cmd { - case next: - nextpkg.Next(args) - case last: - nextpkg.Last() - case tag: - tagpkg.Tag(args) - case sort: - sortpkg.Sort(args) - case help: + switch arg { + case cmd.Next: + next.Next(args) + case cmd.Last: + next.Last() + case cmd.Tag: + tag.Tag(args) + case cmd.Sort: + sort.Sort(args) + case cmd.Help: log.Fatalln(helpLong) default: log.Println("um: command not found")