blob: a768bacd3384f199b26dd762f74de3bb888600dc (
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
|
package main
import (
"os"
"fmt"
"log"
"flag"
// "strings"
// "bufio"
db "sanine.net/git/phlox/db"
)
var Params struct {
InitDb string
Db string
}
var p *db.Phlox
type Command struct {
Name string
Help string
subcommands map[string]*Command
Branch bool
Execute func([]string)
}
func (c *Command) errNoSubCommand() {
fmt.Println("error: a subcommand was required but not provided.")
fmt.Println("available options are:\n")
for name := range c.subcommands {
fmt.Printf("\t* %v\n", name)
}
fmt.Println()
}
func (c *Command) Parse(args []string) {
if !c.Branch {
c.Execute(args)
} else {
if len(args) > 0 {
cn, ok := c.subcommands[args[0]]
if ok {
cn.Parse(args[1:])
} else {
c.errNoSubCommand()
}
} else {
c.errNoSubCommand()
}
}
}
func (c *Command) AddCommand(cn *Command) {
if c.subcommands == nil {
c.subcommands = make(map[string]*Command)
}
c.subcommands[cn.Name] = cn
}
var parser *Command
func main() {
p = &db.Phlox{}
flag.StringVar(&Params.InitDb, "init", "", "initialize a new database at the given path")
flag.StringVar(&Params.Db, "db", "/etc/phlox/phlox.conf", "open the database at the given path for processing")
flag.Parse()
if Params.InitDb != "" {
// initialize a new db
err := p.Create(Params.InitDb)
if err != nil { log.Fatal(err) }
fmt.Printf("created new database at %v\n", Params.InitDb)
p.Close()
os.Exit(0)
}
err := p.Open(Params.Db)
if err != nil {
log.Fatal(err)
}
defer p.Close()
parser = &Command{Branch: true}
ListInit(parser)
CreateInit(parser)
parser.Parse(flag.Args())
}
|