summaryrefslogtreecommitdiff
path: root/conf/main.go
blob: d0785f83bcffcb869230f2e1febd40ae844fa653 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main

import (
	"os"
	"fmt"
	"log"
	"flag"
	"strings"
	"bufio"
	db "sanine.net/git/phlox/db"
)


var p *db.Phlox


type Params struct {
	DbFilename string
}
var params Params


type Command struct {
	Set bool
	Value int
	Flag string
	Usage string
	Invoke func()
}


func RegisterCommand(val int, cmd *Command) {
	cmd.Value = val
	flag.BoolVar(&(*cmd).Set, cmd.Flag, false, cmd.Usage)
}


func OpenDb() {
	err := p.Open(params.DbFilename)
	if err != nil { log.Fatal(err) }
}


func ReadLine(prompt string) string {
	reader := bufio.NewReader(os.Stdin)

	fmt.Print(prompt)
	line, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	return strings.TrimSpace(line)
}


func main() {
	p = &db.Phlox{}

	cmds := map[int]*Command{
		1: Create(),
		2: SchemaVersion(),

		3: CreateUser(),
		4: DeleteUser(),
		5: UpdatePassword(),
		6: ListUsers(),

		7: CreateEndpoint(),
		8: DeleteEndpoint(),
		9: SetPath(),
		10: SetAddress(),
		11: ListEndpoints(),

		12: SetHost(),
		13: GetHost(),
		14: SetLogin(),
		15: GetLogin(),
	}

	for val, cmd := range cmds {
		RegisterCommand(val, cmd)
	}

	flag.StringVar(&params.DbFilename, "db", "phlox.conf", "select the db file to operate on")
	flag.Parse()

	for _, cmd := range cmds {
		if cmd.Set { cmd.Invoke(); os.Exit(0); }
	}

	log.Fatal("no command specified!")
}


func Create() *Command {
	return &Command{
		Flag: "create",
		Usage: "create a new database",
		Invoke: func() {
			err := p.Create(params.DbFilename)
			if err != nil {
				log.Fatal(err)
			}
			fmt.Printf("created database %v\n", params.DbFilename)
		},
	}
}


func SchemaVersion() *Command {
	return &Command{
		Flag: "schema-version",
		Usage: "get the schema version",
		Invoke: func() {
			err := p.Open(params.DbFilename)
			if err != nil { log.Fatal(err) }
			version, err := p.GetSchemaVersion()
			if err != nil { log.Fatal(err) }
			fmt.Printf("schema version: %v\n", version)
		},
	}
}


func SetHost() *Command {
	return &Command{
		Flag: "set-host-address",
		Usage: "set the address to serve the reverse proxy on",
		Invoke: func() {
			OpenDb()
			defer p.Close()
			address := ReadLine("Address: ")
			err := p.SetHostAddress(address)
			if err != nil { log.Fatal(err) }
			fmt.Printf("set host address to %v\n", address)
		},
	}
}


func GetHost() *Command {
	return &Command{
		Flag: "get-host-address",
		Usage: "get the address to serve the reverse proxy on",
		Invoke: func() {
			OpenDb()
			defer p.Close()
			address, err := p.GetHostAddress()
			if err != nil { log.Fatal(err) }
			fmt.Printf("host address is %v\n", address)
		},
	}
}


func SetLogin() *Command {
	return &Command{
		Flag: "set-login-page",
		Usage: "set the file to serve as the login page",
		Invoke: func() {
			OpenDb()
			defer p.Close()
			path := ReadLine("File Path (blank for internal default): ")
			err := p.SetLoginPage(path)
			if err != nil { log.Fatal(err) }
			fmt.Printf("set login page to %v\n", path)
		},
	}
}


func GetLogin() *Command {
	return &Command{
		Flag: "get-login-page",
		Usage: "get the file to serve the reverse proxy on",
		Invoke: func() {
			OpenDb()
			defer p.Close()
			path, err := p.GetLoginPage()
			if err != nil { log.Fatal(err) }
			fmt.Printf("login page is at %v\n", path)
		},
	}
}