-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
61 lines (51 loc) · 1.37 KB
/
cli.go
File metadata and controls
61 lines (51 loc) · 1.37 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
56
57
58
59
60
61
package main
import (
"flag"
"fmt"
"io"
"github.com/megane42/cvlin/cvlin"
)
// Exit codes are int values that represent an exit code for a particular error.
const (
ExitCodeOK int = 0
ExitCodeError int = 1 + iota
ExitCodeInvalid
)
// CLI is the command line object
type CLI struct {
// outStream and errStream are the stdout and stderr
// to write message from the CLI.
outStream, errStream io.Writer
}
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
var (
rulePath string
version bool
)
// Define option flag parse
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
flags.SetOutput(cli.errStream)
flags.StringVar(&rulePath, "rule", "", "Path to rule file")
flags.StringVar(&rulePath, "r", "", "Path to rule file")
flags.BoolVar(&version, "version", false, "Print version information and quit.")
flags.BoolVar(&version, "v", false, "Print version information and quit.")
// Parse commandline flag
if err := flags.Parse(args[1:]); err != nil {
return ExitCodeError
}
// Show version
if version {
fmt.Fprintf(cli.outStream, "v%s\n", Version)
return ExitCodeOK
}
// Validate CSV
subjectPath := flags.Arg(0)
_, err := cvlin.Run(rulePath, subjectPath)
if err != nil {
fmt.Fprintf(cli.errStream, "%v\n", err)
return ExitCodeError
}
fmt.Fprintf(cli.outStream, "Valid.\n")
return ExitCodeOK
}