log.Fatal and more consistent error strings
This commit is contained in:
parent
9ae9c8e5fd
commit
c00904a004
6 changed files with 31 additions and 28 deletions
13
next/last.go
13
next/last.go
|
|
@ -3,8 +3,7 @@ package next
|
|||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"log"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
|
@ -14,7 +13,8 @@ const (
|
|||
// NOTE: the -r flag:
|
||||
LS_CMD = `ls -r [0-9]*.md`
|
||||
// we only care about the number group:
|
||||
FILE_REGEXP = `(?m)^([0-9]+)\.[[:alpha:]]*\.*md$`
|
||||
FILE_REGEXP = `(?m)^([0-9]+)\.[[:alpha:]]*\.*md$`
|
||||
NOT_FOUND_MSG = "um files not found"
|
||||
)
|
||||
|
||||
var fileRegexp *regexp.Regexp = regexp.MustCompile(FILE_REGEXP)
|
||||
|
|
@ -35,7 +35,7 @@ func last() (string, error) {
|
|||
|
||||
lines := strings.Split(stdout.String(), "\n")
|
||||
if len(lines) == 0 {
|
||||
return "", errors.New("um files not found")
|
||||
return "", errors.New(NOT_FOUND_MSG)
|
||||
}
|
||||
return lines[0], nil
|
||||
}
|
||||
|
|
@ -43,8 +43,7 @@ func last() (string, error) {
|
|||
func Last() {
|
||||
s, err := last()
|
||||
if err != nil {
|
||||
fmt.Printf("um last: %v", err)
|
||||
os.Exit(1)
|
||||
log.Fatalf("um last: %v", err)
|
||||
}
|
||||
fmt.Println(s)
|
||||
log.Println(s)
|
||||
}
|
||||
|
|
|
|||
15
next/next.go
15
next/next.go
|
|
@ -3,6 +3,7 @@ package next
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
|
||||
|
|
@ -32,7 +33,7 @@ func parseArgs(args []string) options {
|
|||
case i == 1 && !flags.HasDashPrefix(arg):
|
||||
opts.Tags.Val = arg
|
||||
case i >= 2:
|
||||
fmt.Println("um next : too many args")
|
||||
log.Println("um next: too many args")
|
||||
flags.Help("next", opts)
|
||||
case arg == opts.Help.Long || arg == opts.Help.Short:
|
||||
flags.Help("next", opts)
|
||||
|
|
@ -47,7 +48,7 @@ func numFromLast(last string) (string, error) {
|
|||
res := fileRegexp.FindStringSubmatch(last)
|
||||
num := ""
|
||||
if len(res) < 2 {
|
||||
return num, errors.New("no um files in current dir")
|
||||
return num, errors.New(NOT_FOUND_MSG)
|
||||
}
|
||||
num = res[1]
|
||||
return num, nil
|
||||
|
|
@ -92,11 +93,11 @@ func emacsNext(f string, tags string) error {
|
|||
quotedargs = fmt.Sprintf(`%s "%s"`, quotedargs, tags)
|
||||
}
|
||||
fn := fmt.Sprintf(`(um-next %s)`, quotedargs)
|
||||
out, err := exec.Command("emacsclient", "--eval", fn).Output()
|
||||
out, err := exec.Command("emacsclient", "--eval", fn).CombinedOutput()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(out))
|
||||
log.Print(string(out))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -104,13 +105,13 @@ func Next(args []string) {
|
|||
opts := parseArgs(args)
|
||||
l, err := last()
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
log.Fatalf("um next: %v", err)
|
||||
}
|
||||
filename, err := next(l, opts.Descriptor.Val)
|
||||
if err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
log.Fatalf("um next: %v", err)
|
||||
}
|
||||
if err := emacsNext(filename, opts.Tags.Val); err != nil {
|
||||
fmt.Printf("err: %v\n", err)
|
||||
log.Fatalf("um next: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue