next, last : glob and regexp
This commit is contained in:
parent
c19b11e115
commit
af2f2032f1
2 changed files with 72 additions and 3 deletions
33
next/last.go
33
next/last.go
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue