-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (85 loc) · 2.04 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
101
102
103
104
105
//nolint:exhaustivestruct,exhaustruct,gochecknoglobals,gci
package main
import (
"fmt"
"os"
"sheepla/whois-cli/printer"
"sheepla/whois-cli/resolver"
cli "github.com/urfave/cli/v2"
)
var (
appName = "whois"
appVersion = "unknown"
appRevision = "unknown"
appUsage = "whois CLI"
appDescription = "A whois command line client, to query domain owner information and retrieve results."
)
type exitCode int
const (
exitCodeOK exitCode = iota
exitCodeErrArgs
exitCodeErrWhois
exitCodeErrJSON
)
func (e exitCode) Int() int { return int(e) }
func main() {
if err := initApp().Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func initApp() *cli.App {
app := &cli.App{
Name: appName,
Usage: appUsage,
Description: appDescription,
Action: run,
ArgsUsage: "DOMAIN SERVERS...",
Version: fmt.Sprintf("%s-%s", appVersion, appRevision),
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "json",
Aliases: []string{"j"},
Usage: "Output in JSON format",
},
// &cli.BoolFlag{
// Name: "shell",
// Aliases: []string{"s"},
// Usage: "Start interactive mode",
// },
}
return app
}
func run(ctx *cli.Context) error {
domain, servers, err := parsePositionalArgs(ctx)
if err != nil {
return cli.Exit(err, exitCodeErrArgs.Int())
}
result, err := resolver.Resolve(domain, servers)
if err != nil {
return cli.Exit(err, exitCodeErrWhois.Int())
}
if ctx.Bool("json") {
if err := printer.FprintResultAsJSON(ctx.App.Writer, result); err != nil {
return cli.Exit(
err,
exitCodeErrJSON.Int(),
)
}
return cli.Exit("", exitCodeOK.Int())
}
printer.FprintResult(ctx.App.Writer, result)
return cli.Exit("", exitCodeOK.Int())
}
//nolint:nonamedreturns
func parsePositionalArgs(ctx *cli.Context) (domain string, servers []string, err error) {
if ctx.NArg() < 1 {
return "", []string{}, cli.Exit(
"must requires augument(s)",
exitCodeErrArgs.Int(),
)
}
domain = ctx.Args().First()
servers = ctx.Args().Slice()[1:]
return domain, servers, nil
}