last : handle ls stderr

This commit is contained in:
bth 2026-01-19 08:42:27 -08:00
commit 9ae9c8e5fd

View file

@ -1,8 +1,10 @@
package next package next
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"regexp" "regexp"
"strings" "strings"
@ -21,13 +23,19 @@ var fileRegexp *regexp.Regexp = regexp.MustCompile(FILE_REGEXP)
func last() (string, error) { func last() (string, error) {
// NOTE: globbing requires invoking the shell as cmd: // NOTE: globbing requires invoking the shell as cmd:
cmd := exec.Command("sh", "-c", LS_CMD) cmd := exec.Command("sh", "-c", LS_CMD)
ls, err := cmd.Output()
if err != nil { // fine grained control:
return "", err var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// the returned err is ExitStatus, which has stderr embedded, but i find this clearer:
if err := cmd.Run(); err != nil {
return "", errors.New(stderr.String())
} }
lines := strings.Split(string(ls), "\n")
lines := strings.Split(stdout.String(), "\n")
if len(lines) == 0 { if len(lines) == 0 {
return "", errors.New("no um files in current dir") return "", errors.New("um files not found")
} }
return lines[0], nil return lines[0], nil
} }
@ -35,7 +43,8 @@ func last() (string, error) {
func Last() { func Last() {
s, err := last() s, err := last()
if err != nil { if err != nil {
fmt.Printf("err: %v\n", err) fmt.Printf("um last: %v", err)
os.Exit(1)
} }
fmt.Println(s) fmt.Println(s)
} }