-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
61 lines (55 loc) · 1.23 KB
/
setup.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
)
func init() {
log.SetFlags(log.Lshortfile)
}
func setup() (configuration Configuration, command string) {
file := fmt.Sprintf("%s.json", PROGNAME)
flag.StringVar(&file, "f", file, "Configuration `file`")
flag.Parse()
configuration = parseConfiguration(file)
if len(flag.Args()) == 0 {
usage()
}
command = flag.Args()[0]
if _, err := os.Stat(configuration.Directory); os.IsNotExist(err) {
fmt.Printf("Directory %q not found!\n", configuration.Directory)
usage()
}
fmt.Println(configuration)
return
}
func parseConfiguration(file string) (configuration Configuration) {
content, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(content, &configuration)
if err != nil {
fmt.Println("Invalid JSON!")
log.Fatal(err)
}
return
}
func usage() {
fmt.Printf("%s v.%s - (c) Lyderic Landry, London 2018\n", PROGNAME, VERSION)
fmt.Println("Usage: collix <option> command")
fmt.Println("Commands:")
fmt.Println(" init initialize new database")
fmt.Println(" info show information on database")
fmt.Println("Options:")
flag.PrintDefaults()
os.Exit(1)
}
func c(err error) {
if err != nil {
panic(err)
}
}