package last import ( "errors" "fmt" "log" "path/filepath" "github.com/brtholomy/um/go/cmd" "github.com/brtholomy/um/go/flags" ) const ( CMD = cmd.Last SUMMARY = "print the lexically last um file" ) 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.Bool } func initOpts() options { return options{ flags.Bool{"--help", "-h", false, "show help"}, } } // 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) { opts := initOpts() help := flags.NewHelpError(CMD, SUMMARY) if err := flags.ParseArgs(help, args, &opts); err != nil { if errors.As(err, &help) { fmt.Println(help) return } log.Fatalf("um %s: %s", CMD, err) } s, err := GlobLast(GLOB) if err != nil { log.Fatalf("um %s: %v", cmd.Last, err) } // send to stdout, not stderr as is default for log.Print: fmt.Println(s) }