-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
100 lines (86 loc) · 2.54 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
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
package main
import (
"flag"
"fmt"
"io"
"os"
// "strings"
"github.com/mitchellh/go-homedir"
"github.com/ake-persson/peekaboo/query"
"github.com/ake-persson/peekaboo/serve"
)
var version = "undefined"
func usage(flags *flag.FlagSet, stdout io.Writer) func() {
return func() {
fmt.Fprintf(stdout, `Usage: %s [OPTIONS] COMMAND
Micro-service for exposing system and hardware information
Options:
`, os.Args[0])
flags.PrintDefaults()
fmt.Fprintf(stdout, `
Commands:
serve Serve API.
query Query API from one or more servers.
`)
}
}
// Abstract main so we can test it as a regular function.
func main() {
if err := run(os.Args, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(args []string, stdout io.Writer) error {
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
flags.SetOutput(stdout)
flags.Usage = usage(flags, stdout)
var (
noTLS = flags.Bool("no-tls", false, "No TLS (testing)")
noMTLS = flags.Bool("no-mtls", true, "No Mutual TLS, client and server certificate needs to be signed by the same CA authority to establish trust")
noVerify = flags.Bool("no-verify", false, "No TLS verify, for self-signed certificates (testing)")
certFile = flags.String("cert-file", "~/certs/srv.crt", "Server TLS certificate file")
keyFile = flags.String("key-file", "~/certs/srv.key", "Server TLS key file")
caFile = flags.String("ca-file", "~/certs/root_ca.crt", "CA certificate file, required for Mutual TLS")
printVersion = flags.Bool("version", false, "Version")
)
if err := flags.Parse(args[1:]); err != nil {
return err
}
// Print version.
if *printVersion {
fmt.Fprintf(stdout, "%s\n", version)
return nil
}
// Replace tilde with home directory.
*certFile, _ = homedir.Expand(*certFile)
*keyFile, _ = homedir.Expand(*keyFile)
*caFile, _ = homedir.Expand(*caFile)
// Check command.
if len(flags.Args()) < 1 {
usage(flags, stdout)()
return fmt.Errorf("no command specified")
}
command := flags.Args()[0]
// Run command.
args = append([]string{os.Args[0]}, flags.Args()[1:]...)
switch command {
case "serve":
return serve.Run(args, stdout, &serve.Options{
NoTLS: *noTLS,
NoMTLS: *noMTLS,
NoVerify: *noVerify,
CertFile: *certFile,
KeyFile: *keyFile,
CAFile: *caFile})
case "query":
return query.Run(args, stdout, &query.Options{
NoTLS: *noTLS,
NoMTLS: *noMTLS,
NoVerify: *noVerify,
CertFile: *certFile,
KeyFile: *keyFile,
CAFile: *caFile})
}
return fmt.Errorf("unknown command: %s", command)
}