-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
62 lines (52 loc) · 1.33 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/BytecodeAgency/import-boundary-checker/logging"
"github.com/BytecodeAgency/import-boundary-checker/runner"
)
func main() {
// CLI flags
configPath := flag.String("config", ".importrules", "Configuration path to be used when building import rule set")
printHelp := flag.Bool("help", false, "Print CLI usage information")
verbose := flag.Bool("verbose", false, "Enable verbose output")
flag.Parse()
if *printHelp {
flag.PrintDefaults()
os.Exit(0)
}
// Create logger
logger := logging.New(*verbose)
// Parse config file
c, err := getConfigString(*configPath)
if err != nil {
logger.FailWithError("error loading config", err)
fmt.Print(logger.Logs.String())
os.Exit(1)
}
// Create runner and run application
r := runner.New(c, logger)
failed := r.Run()
// Get and print the logs
fmt.Print(logger.Logs.String())
// Fail with correct exit code
if failed {
os.Exit(1)
} else {
os.Exit(0)
}
}
func getConfigString(path string) (string, error) {
fullPath, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("error parsing full config path: %+v", err)
}
config, err := ioutil.ReadFile(fullPath)
if err != nil {
return "", fmt.Errorf("error reading config file %s: %+v", path, err)
}
return string(config), nil
}