-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (69 loc) · 1.98 KB
/
Copy pathmain.go
File metadata and controls
82 lines (69 loc) · 1.98 KB
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
// Package main initializes and starts ipman
package main
import (
"fmt"
"log/slog"
"os"
"strings"
)
// setupLogger configures the application logger.
func setupLogger() *slog.Logger {
lvl := &slog.LevelVar{} // create new level logger
lvl.Set(slog.LevelInfo) // default to Info
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: lvl}))
if l := os.Getenv("LOG"); l != "" {
switch {
case strings.HasPrefix(l, "D"):
lvl.Set(slog.LevelDebug)
case strings.HasPrefix(l, "W"):
lvl.Set(slog.LevelWarn)
case strings.HasPrefix(l, "E"):
lvl.Set(slog.LevelError)
}
}
return logger
}
// usage generates the command-line usage message.
func usage(self string) string {
return fmt.Sprintf(`Usage: %s <command> [options]
Commands:
check Return current external IP address of local machine.
update Update DNS registry with external IP address of local machine.
help Show this help message.
`, self)
}
func usageError(self, message string) string {
return fmt.Sprintf("%s: %s\n\n%s", self, message, usage(self))
}
// main is a thin wrapper around the real application entry point.
func main() {
os.Exit(realMain())
}
// realMain executes the application and returns a process exit code.
func realMain() int {
logger := setupLogger()
commands := initCommands(logger)
if len(os.Args) < 2 {
_, _ = os.Stderr.WriteString(usage(os.Args[0]))
return 1
}
switch arg := os.Args[1]; arg {
case "help", "-h", "--help":
_, _ = os.Stdout.WriteString(usage(os.Args[0]))
return 0
default:
if strings.HasPrefix(arg, "-") {
message := fmt.Sprintf("unknown top-level flag %q", arg)
if arg == "-" || arg == "--" {
message = fmt.Sprintf("expected a subcommand before %q", arg)
}
_, _ = os.Stderr.WriteString(usageError(os.Args[0], message))
return 1
}
if cmd, ok := commands[arg]; ok {
return runCommand(os.Args[2:], cmd)
}
_, _ = os.Stderr.WriteString(usageError(os.Args[0], fmt.Sprintf("unknown command %q", arg)))
return 1
}
}