diff --git a/last/last.go b/last/last.go new file mode 100644 index 0000000..6abfc01 --- /dev/null +++ b/last/last.go @@ -0,0 +1,66 @@ +package last + +import ( + "errors" + "fmt" + "log" + "path/filepath" + + cmdpkg "github.com/brtholomy/um/go/cmd" + "github.com/brtholomy/um/go/flags" +) + +const cmd = cmdpkg.Last + +const ( + GLOB = "[0-9]*.md" + // we only care about the number group: + NOT_FOUND_MSG = "um files not found: " + GLOB +) + +type options struct { + Help flags.Flag[bool] +} + +func initOpts() options { + return options{ + flags.Flag[bool]{"--help", "-h", false, "show help"}, + } +} + +func parseArgs(args []string) options { + opts := initOpts() + for _, arg := range args { + switch { + case arg == opts.Help.Long || arg == opts.Help.Short: + flags.Help(cmd, opts) + default: + flags.HelpInvalidArg(cmd, arg) + flags.Help(cmd, opts) + } + } + return opts +} + +// get the lexical last file from GLOB +func GlobLast(glob string) (string, error) { + // NOTE: filepath.Glob is more reliable than a manual ls call: + filelist, err := filepath.Glob(glob) + if err != nil { + return "", err + } + if len(filelist) == 0 { + return "", errors.New(NOT_FOUND_MSG) + } + return filelist[len(filelist)-1], nil +} + +func Last(args []string) { + _ = parseArgs(args) + s, err := GlobLast(GLOB) + if err != nil { + 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/last.go b/next/last.go deleted file mode 100644 index 7363c56..0000000 --- a/next/last.go +++ /dev/null @@ -1,42 +0,0 @@ -package next - -import ( - "errors" - "fmt" - "log" - "path/filepath" - "regexp" - - cmdpkg "github.com/brtholomy/um/go/cmd" -) - -const ( - GLOB = "[0-9]*.md" - // we only care about the number group: - FILE_REGEXP = `(?m)^([0-9]+)\.[[:alpha:]]*\.*md$` - NOT_FOUND_MSG = "um files not found: " + GLOB -) - -var fileRegexp *regexp.Regexp = regexp.MustCompile(FILE_REGEXP) - -// get the lexical last file from GLOB -func last() (string, error) { - // NOTE: filepath.Glob is more reliable than a manual ls call: - filelist, err := filepath.Glob(GLOB) - if err != nil { - return "", err - } - if len(filelist) == 0 { - return "", errors.New(NOT_FOUND_MSG) - } - return filelist[len(filelist)-1], nil -} - -func Last() { - s, err := last() - if err != nil { - 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 25cb422..e83abe2 100644 --- a/next/next.go +++ b/next/next.go @@ -5,14 +5,20 @@ import ( "fmt" "log" "os/exec" + "regexp" "strconv" cmdpkg "github.com/brtholomy/um/go/cmd" "github.com/brtholomy/um/go/flags" + "github.com/brtholomy/um/go/last" ) const cmd = cmdpkg.Next +const FILE_REGEXP = `(?m)^([0-9]+)\.[[:alpha:]]*\.*md$` + +var fileRegexp *regexp.Regexp = regexp.MustCompile(FILE_REGEXP) + type options struct { Descriptor flags.Arg Tags flags.Arg @@ -47,11 +53,11 @@ func parseArgs(args []string) options { // takes the complete last file string // returns the number as string -func numFromLast(last string) (string, error) { - res := fileRegexp.FindStringSubmatch(last) +func numFromLast(l string) (string, error) { + res := fileRegexp.FindStringSubmatch(l) num := "" if len(res) < 2 { - return num, errors.New(NOT_FOUND_MSG) + return num, errors.New(last.NOT_FOUND_MSG) } num = res[1] return num, nil @@ -108,7 +114,7 @@ func emacsNext(f string, tags string) error { func Next(args []string) { opts := parseArgs(args) - l, err := last() + l, err := last.GlobLast(last.GLOB) if err != nil { log.Fatalf("um %s: %v", cmd, err) } diff --git a/tag/parse.go b/tag/parse.go index 0d309ce..897f774 100644 --- a/tag/parse.go +++ b/tag/parse.go @@ -14,9 +14,6 @@ import ( ) const ( - // TODO: source in common with last.go - GLOB = "[0-9]*.md" - // The layout string must be a representation of: // Jan 2 15:04:05 2006 MST // 1 2 3 4 5 6 -7 diff --git a/tag/tag.go b/tag/tag.go index 2ddc313..abd74a0 100644 --- a/tag/tag.go +++ b/tag/tag.go @@ -5,6 +5,7 @@ import ( cmdpkg "github.com/brtholomy/um/go/cmd" "github.com/brtholomy/um/go/flags" + "github.com/brtholomy/um/go/last" ) // just so that copied calls into flags.Help don't have to be adjusted between files: @@ -54,7 +55,7 @@ func parseArgs(args []string) options { func Tag(args []string) { opts := parseArgs(args) queries := parseQuery(opts.Query.Val) - entries := entriesGlobOrStdin(GLOB) + entries := entriesGlobOrStdin(last.GLOB) // we shrink the entries list immediately if we want a date range: if opts.Date.IsSet() { diff --git a/um.go b/um.go index 7ee58fd..6f57d20 100644 --- a/um.go +++ b/um.go @@ -6,6 +6,7 @@ import ( "os" "github.com/brtholomy/um/go/cmd" + "github.com/brtholomy/um/go/last" "github.com/brtholomy/um/go/next" "github.com/brtholomy/um/go/sort" "github.com/brtholomy/um/go/tag" @@ -38,7 +39,7 @@ func main() { case cmd.Next: next.Next(args) case cmd.Last: - next.Last() + last.Last(args) case cmd.Tag: tag.Tag(args) case cmd.Sort: