last : move to its own pkg for sharing of GLOB pattern and --help, slightly overkill but consistent
This commit is contained in:
parent
b69f3aabd0
commit
c2412caba6
6 changed files with 80 additions and 51 deletions
66
last/last.go
Normal file
66
last/last.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
42
next/last.go
42
next/last.go
|
|
@ -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)
|
||||
}
|
||||
14
next/next.go
14
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
3
um.go
3
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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue