1- package main
1+ package cmd
22
33import (
44 "context"
55 "fmt"
66 "net/url"
77 "os"
88 "os/signal"
9+ "os/user"
910 "path/filepath"
1011 "syscall"
1112
@@ -20,46 +21,60 @@ import (
2021///////////////////////////////////////////////////////////////////////////////
2122// TYPES
2223
23- type Globals struct {
24+ type app struct {
2425 Endpoint string `env:"ENDPOINT" default:"http://localhost/" help:"Service endpoint"`
2526 Debug bool `help:"Enable debug output"`
2627 Trace bool `help:"Enable trace output"`
2728
28- vars kong.Vars `kong:"-"` // Variables for kong
29+ kong * kong.Context `kong:"-"` // Kong parser
30+ vars kong.Vars `kong:"-"` // Variables for kong
2931 ctx context.Context
3032 cancel context.CancelFunc
3133}
3234
33- var _ server.Cmd = (* Globals )(nil )
35+ var _ server.Cmd = (* app )(nil )
3436
3537///////////////////////////////////////////////////////////////////////////////
3638// LIFECYCLE
3739
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+ )
4154
4255 // Create the context
4356 // This context is cancelled when the process receives a SIGINT or SIGTERM
4457 app .ctx , app .cancel = signal .NotifyContext (context .Background (), os .Interrupt , syscall .SIGTERM )
4558
4659 // Return the app
47- return & app , nil
60+ return app , nil
4861}
4962
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 ()
5166 app .cancel ()
52- return nil
67+ return err
5368}
5469
5570///////////////////////////////////////////////////////////////////////////////
5671// PUBLIC METHODS
5772
58- func (app * Globals ) Context () context.Context {
73+ func (app * app ) Context () context.Context {
5974 return app .ctx
6075}
6176
62- func (app * Globals ) GetDebug () server.DebugLevel {
77+ func (app * app ) GetDebug () server.DebugLevel {
6378 if app .Debug {
6479 return server .Debug
6580 }
@@ -69,7 +84,7 @@ func (app *Globals) GetDebug() server.DebugLevel {
6984 return server .None
7085}
7186
72- func (app * Globals ) GetEndpoint (paths ... string ) * url.URL {
87+ func (app * app ) GetEndpoint (paths ... string ) * url.URL {
7388 url , err := url .Parse (app .Endpoint )
7489 if err != nil {
7590 return nil
@@ -82,7 +97,7 @@ func (app *Globals) GetEndpoint(paths ...string) *url.URL {
8297 return url
8398}
8499
85- func (app * Globals ) GetClientOpts () []client.ClientOpt {
100+ func (app * app ) GetClientOpts () []client.ClientOpt {
86101 opts := []client.ClientOpt {}
87102
88103 // Trace mode
@@ -104,3 +119,31 @@ func (app *Globals) GetClientOpts() []client.ClientOpt {
104119 // Return options
105120 return opts
106121}
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