-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
43 lines (33 loc) · 791 Bytes
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"errors"
"fmt"
"regexp"
"github.com/PMcca/phmod/chmodparse"
"github.com/urfave/cli"
)
var regexNum = regexp.MustCompile("^[1-7]?[0-7]{3}$") // 3 or 4 digits
var regexChar = regexp.MustCompile("^(?:r|-)(?:w|-)(?:x|-).{6}$")
var parser chmodparse.Parser
func parse(ctx *cli.Context) error {
if len(ctx.Args()) != 1 {
return errors.New("Invalid number of arguments")
}
arg := ctx.Args().First()
switch {
case regexNum.MatchString(arg):
parser = chmodparse.NewNumParser(arg)
case regexChar.MatchString(arg):
parser = chmodparse.CharParser{}
default:
return errors.New("Invalid argument")
}
var parsed string
if ctx.Bool("l") {
parsed = parser.ParseVerbose(arg)
} else {
parsed = parser.Parse(arg)
}
fmt.Println(parsed)
return nil
}