aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: bc4ef307f59bc197ad1e14e3f37d6b821690d3db (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main

import (
	"code.laria.me/petrific/logging"
	"flag"
	"fmt"
	"math/rand"
	"os"
	"time"
)

type subcmd func(env *Env, args []string) int

var subcmds = map[string]subcmd{
	"write-dir":        WriteDir,
	"restore-dir":      RestoreDir,
	"take-snapshot":    TakeSnapshot,
	"create-snapshot":  CreateSnapshot,
	"list-snapshots":   ListSnapshots,
	"restore-snapshot": RestoreSnapshot,
	"fsck":             Fsck,
	"storagecmd":       StorageCmd,
}

func subcmdUsage(name string, usage string, flags *flag.FlagSet) func() {
	return func() {
		fmt.Fprintf(os.Stderr, "Usage: %s %s %s\n", os.Args[0], name, usage)
		if flags != nil {
			fmt.Fprintln(os.Stderr, "\nFlags:")
			flags.PrintDefaults()
		}
	}
}

func subcmdErrout(log *logging.Log, name string) func(error) {
	return func(err error) {
		log.Error().Printf("%s: %s\n", name, err)
	}
}

// Global flags
var (
	flagConfPath  = flag.String("config", "", "Use this config file instead of the default")
	flagStorage   = flag.String("storage", "", "Operate on this storage instead of the default one")
	flagVerbosity = flag.Int("verbosity", int(logging.LWarn), "Verbosity level (0: quiet, 4: everything)")
)

func main() {
	os.Exit(Main())
}

func Main() int {
	rand.Seed(time.Now().UnixNano())

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [global flags] command\n\nAvailable commands:\n", os.Args[0])
		for cmd := range subcmds {
			fmt.Fprintf(os.Stderr, "  %s\n", cmd)
		}
		fmt.Fprintln(os.Stderr, "\nGlobal flags:")
		flag.PrintDefaults()
	}
	flag.Parse()

	log, err := createLogger(logging.Level(*flagVerbosity))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}

	env, err := NewEnv(log, *flagConfPath, *flagStorage)
	if err != nil {
		log.Error().Println(err)
		return 1
	}
	defer env.Close()

	remaining := make([]string, 0)
	for _, arg := range flag.Args() {
		if arg != "" {
			remaining = append(remaining, arg)
		}
	}

	var cmd subcmd
	if len(remaining) > 0 {
		cmd = subcmds[remaining[0]]
	}

	if cmd == nil {
		flag.Usage()
		return 1
	}

	return cmd(env, remaining[1:])
}

func createLogger(level logging.Level) (log *logging.Log, err error) {
	if level < logging.LQuiet || level > logging.LDebug {
		level = logging.LDebug
		return nil, fmt.Errorf("verbosity %d is out of range", level)
	}

	return logging.NewLog(os.Stderr, level), nil
}