next, last : glob and regexp

This commit is contained in:
bth 2026-01-18 21:38:18 -08:00
commit af2f2032f1
2 changed files with 72 additions and 3 deletions

View file

@ -1,14 +1,39 @@
package next
import (
"errors"
"fmt"
"os/exec"
"regexp"
"strings"
)
func Last() {
ls, err := exec.Command("ls").Output()
const (
LS_CMD = `ls -r [0-9]*.md`
LS_GREP_PIPE = `ls -r | grep -E '^[[:digit:]].*\.md'`
FILE_REGEXP = `(?m)^([0-9]+)\.([[:alpha:]]*)\.*md$`
)
var fileRegexp *regexp.Regexp = regexp.MustCompile(FILE_REGEXP)
func last() (string, error) {
// NOTE: globbing requires invoking the shell as cmd:
cmd := exec.Command("sh", "-c", LS_CMD)
ls, err := cmd.Output()
if err != nil {
fmt.Print(err)
return "", err
}
fmt.Printf("ls\n%s\n", string(ls))
lines := strings.Split(string(ls), "\n")
if len(lines) == 0 {
return "", errors.New("no um files in current dir")
}
return lines[0], nil
}
func Last() {
s, err := last()
if err != nil {
fmt.Printf("err: %v\n", err)
}
fmt.Println(s)
}