-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
55 lines (50 loc) · 1.68 KB
/
parser.go
File metadata and controls
55 lines (50 loc) · 1.68 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"errors"
"flag"
"fmt"
"os"
)
func isValidevent(event string) bool {
switch event {
case
"create",
"write",
"remove",
"rename",
"chmod":
return true
}
return false
}
func cliParser() (CliInput, error) {
if len(os.Args) < 2 {
return CliInput{}, errors.New("command line argument is required to do something")
}
localPath, err := os.Getwd()
if err != nil {
return CliInput{}, err
}
directory := flag.String("d", localPath, "Directory to watch, set to current working directory as default")
pattern := flag.String("p", "*", "File pattern to notify")
event := flag.String("e", "create", "Event to notify, select between create, write, remove, rename, chmod")
nonBlocking := flag.Bool("n", false, "Set execution mode to non-blocking")
flag.Parse()
cmdArguments := flag.Args()
eventValidation := isValidevent(*event)
if !eventValidation {
return CliInput{}, errors.New("select only between create, write, remove, rename, chmod")
}
return CliInput{directory: *directory, pattern: *pattern, eventType: *event, commandArgs: cmdArguments, nonBlocking: *nonBlocking}, nil
}
func displayHelp() {
flag.Usage = func() {
fmt.Printf("#### goracle ####\nA CLI app to watch a directory and doing something, powered by golang and fsnotify\n")
fmt.Printf("Usage: %s [options] COMMAND ARGS\nOptions:\n", os.Args[0])
flag.PrintDefaults()
fmt.Printf("NOTE: Separate multiple ARGS by space\n")
fmt.Printf("Example:\nrun a python program over a write event of .csv file\n")
fmt.Printf("goracle -e write -d /home/users/folder -n -p *.csv python example.py input_argument\n")
fmt.Printf("The event file name will always be passed to the last ARGS by default\n")
}
}