1
- package main
1
+ package cmd
2
2
3
3
import (
4
4
"context"
5
5
"fmt"
6
6
"net/url"
7
7
"os"
8
8
"os/signal"
9
+ "os/user"
9
10
"path/filepath"
10
11
"syscall"
11
12
@@ -20,46 +21,60 @@ import (
20
21
///////////////////////////////////////////////////////////////////////////////
21
22
// TYPES
22
23
23
- type Globals struct {
24
+ type app struct {
24
25
Endpoint string `env:"ENDPOINT" default:"http://localhost/" help:"Service endpoint"`
25
26
Debug bool `help:"Enable debug output"`
26
27
Trace bool `help:"Enable trace output"`
27
28
28
- vars kong.Vars `kong:"-"` // Variables for kong
29
+ kong * kong.Context `kong:"-"` // Kong parser
30
+ vars kong.Vars `kong:"-"` // Variables for kong
29
31
ctx context.Context
30
32
cancel context.CancelFunc
31
33
}
32
34
33
- var _ server.Cmd = (* Globals )(nil )
35
+ var _ server.Cmd = (* app )(nil )
34
36
35
37
///////////////////////////////////////////////////////////////////////////////
36
38
// LIFECYCLE
37
39
38
- func NewApp (app Globals , vars kong.Vars ) (* Globals , error ) {
39
- // Set the vars
40
- app .vars = vars
40
+ func New (commands any , description string ) (server.Cmd , error ) {
41
+ app := new (app )
42
+
43
+ app .kong = kong .Parse (commands ,
44
+ kong .Name (execName ()),
45
+ kong .Description (description ),
46
+ kong .UsageOnError (),
47
+ kong .ConfigureHelp (kong.HelpOptions {Compact : true }),
48
+ kong .Embed (app ),
49
+ kong.Vars {
50
+ "HOST" : hostName (),
51
+ "USER" : userName (),
52
+ },
53
+ )
41
54
42
55
// Create the context
43
56
// This context is cancelled when the process receives a SIGINT or SIGTERM
44
57
app .ctx , app .cancel = signal .NotifyContext (context .Background (), os .Interrupt , syscall .SIGTERM )
45
58
46
59
// Return the app
47
- return & app , nil
60
+ return app , nil
48
61
}
49
62
50
- func (app * Globals ) Close () error {
63
+ func (app * app ) Run () error {
64
+ app .kong .BindTo (app , (* server .Cmd )(nil ))
65
+ err := app .kong .Run ()
51
66
app .cancel ()
52
- return nil
67
+ return err
53
68
}
54
69
55
70
///////////////////////////////////////////////////////////////////////////////
56
71
// PUBLIC METHODS
57
72
58
- func (app * Globals ) Context () context.Context {
73
+ func (app * app ) Context () context.Context {
59
74
return app .ctx
60
75
}
61
76
62
- func (app * Globals ) GetDebug () server.DebugLevel {
77
+ func (app * app ) GetDebug () server.DebugLevel {
63
78
if app .Debug {
64
79
return server .Debug
65
80
}
@@ -69,7 +84,7 @@ func (app *Globals) GetDebug() server.DebugLevel {
69
84
return server .None
70
85
}
71
86
72
- func (app * Globals ) GetEndpoint (paths ... string ) * url.URL {
87
+ func (app * app ) GetEndpoint (paths ... string ) * url.URL {
73
88
url , err := url .Parse (app .Endpoint )
74
89
if err != nil {
75
90
return nil
@@ -82,7 +97,7 @@ func (app *Globals) GetEndpoint(paths ...string) *url.URL {
82
97
return url
83
98
}
84
99
85
- func (app * Globals ) GetClientOpts () []client.ClientOpt {
100
+ func (app * app ) GetClientOpts () []client.ClientOpt {
86
101
opts := []client.ClientOpt {}
87
102
88
103
// Trace mode
@@ -104,3 +119,31 @@ func (app *Globals) GetClientOpts() []client.ClientOpt {
104
119
// Return options
105
120
return opts
106
121
}
122
+
123
+ ///////////////////////////////////////////////////////////////////////////////
124
+ // PRIVATE METHODS
125
+
126
+ func hostName () string {
127
+ name , err := os .Hostname ()
128
+ if err != nil {
129
+ panic (err )
130
+ }
131
+ return name
132
+ }
133
+
134
+ func userName () string {
135
+ user , err := user .Current ()
136
+ if err != nil {
137
+ panic (err )
138
+ }
139
+ return user .Username
140
+ }
141
+
142
+ func execName () string {
143
+ // The name of the executable
144
+ name , err := os .Executable ()
145
+ if err != nil {
146
+ panic (err )
147
+ }
148
+ return filepath .Base (name )
149
+ }
0 commit comments