Skip to content

Commit 512059a

Browse files
authored
Merge pull request #212 from anywherelan/cli-ux
cli: various UX improvements
2 parents a9da09f + 1e11aa0 commit 512059a

2 files changed

Lines changed: 62 additions & 32 deletions

File tree

cli/cli.go

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path"
10+
"runtime"
1011
"strconv"
1112
"strings"
1213

@@ -48,21 +49,48 @@ func New(updateType update.ApplicationType) *Application {
4849
func (a *Application) Run() {
4950
if len(os.Args) == 1 {
5051
return
51-
} else if os.Args[1] == WithEnvCommandName {
52+
}
53+
54+
switch arg := os.Args[1]; arg {
55+
case WithEnvCommandName:
5256
// is handled in linux_root_hacks.go
5357
return
54-
} else if os.Args[1] == CliCommandName {
55-
// ok, handle here below
56-
} else {
57-
a.logger.Fatalf("Unknown command '%s', try '%s cli -h' for info on cli commands or '%s' to start awl server", os.Args[1], binaryName, binaryName)
58+
case CliCommandName:
59+
err := a.cliapp.Run(os.Args[1:])
60+
if err != nil {
61+
a.logger.Fatalf("Error occurred: %v", err)
62+
}
63+
os.Exit(0)
64+
case "-h", "--help":
65+
a.printGlobalHelp()
66+
os.Exit(0)
67+
case "-v", "--version":
68+
a.printVersion()
69+
os.Exit(0)
70+
default:
71+
fmt.Printf("Unknown command '%s'. Use '%s -h' for help, or run '%s' to start the server\n", arg, binaryName, binaryName)
72+
os.Exit(-1)
5873
}
74+
}
5975

60-
err := a.cliapp.Run(os.Args[1:])
61-
if err != nil {
62-
a.logger.Fatalf("Error occurred: %v", err)
63-
}
76+
func (a *Application) printGlobalHelp() {
77+
fmt.Printf(`Usage: %s <command>
6478
65-
os.Exit(0)
79+
Run without a command to start the server.
80+
81+
Flags:
82+
-h, --help Show context-sensitive help.
83+
-v, --version Show version.
84+
85+
Commands:
86+
cli Command line interface for Anywherelan
87+
88+
Run "%s <command> --help" for more information on a command.
89+
`, binaryName, binaryName)
90+
}
91+
92+
func (a *Application) printVersion() {
93+
fmt.Printf("Anywherelan version %s (%s %s-%s)\n", config.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
6694
}
6795

6896
func (a *Application) init() {
@@ -437,34 +465,36 @@ func (a *Application) init() {
437465
}
438466
}
439467

440-
func (a *Application) initApiConnection(c *cli.Context) (err error) {
468+
func (a *Application) initApiConnection(c *cli.Context) error {
441469
apiAddr := c.String("api_addr")
442-
var addr string
443-
defer func() {
444-
if err != nil {
445-
return
446-
}
447-
a.api = apiclient.New(addr)
448-
_, err2 := a.api.PeerInfo()
449-
if err2 != nil {
450-
err = fmt.Errorf("could not access api on address %s: %v", addr, err2)
451-
}
452-
}()
453470
if apiAddr != "" {
454-
addr = apiAddr
455-
return nil
471+
return a.initApiFromAddr(apiAddr)
456472
}
457-
conf, err := config.LoadConfig(eventbus.NewBus())
458-
if err != nil {
459-
a.logger.Errorf("could not load config, use default api_addr (%s), error: %v", defaultApiAddr, err)
460-
addr = defaultApiAddr
473+
474+
conf, errConfig := config.LoadConfig(eventbus.NewBus())
475+
if errConfig == nil {
476+
return a.initApiFromAddr(conf.HttpListenAddress)
477+
}
478+
479+
errDefault := a.initApiFromAddr(defaultApiAddr)
480+
if errDefault == nil {
461481
return nil
462482
}
463-
addr = conf.HttpListenAddress
464-
if addr == "" {
465-
return errors.New("httpListenAddress from config is empty")
483+
484+
a.logger.Errorf("could not load config file, error: %v", errConfig)
485+
a.logger.Errorf("could not connect to default api_addr (%s), error: %v", defaultApiAddr, errDefault)
486+
487+
return errors.New("no connection to api server")
488+
}
489+
490+
func (a *Application) initApiFromAddr(addr string) error {
491+
api := apiclient.New(addr)
492+
_, err := api.PeerInfo()
493+
if err != nil {
494+
return fmt.Errorf("could not access api on address %s: %v", addr, err)
466495
}
467496

497+
a.api = api
468498
return nil
469499
}
470500

cli/me.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func printStatus(api *apiclient.Client) error {
2222
table.AppendBulk([][]string{
2323
{"Download rate", fmt.Sprintf("%s (%s)", stats.NetworkStatsInIECUnits.RateIn, stats.NetworkStatsInIECUnits.TotalIn)},
2424
{"Upload rate", fmt.Sprintf("%s (%s)", stats.NetworkStatsInIECUnits.RateOut, stats.NetworkStatsInIECUnits.TotalOut)},
25-
{"Bootstrap peers", fmt.Sprintf("%d/%d", stats.TotalBootstrapPeers, stats.ConnectedBootstrapPeers)},
25+
{"Bootstrap peers", fmt.Sprintf("%d/%d", stats.ConnectedBootstrapPeers, stats.TotalBootstrapPeers)},
2626
{"DNS", formatWorkingStatus(stats.IsAwlDNSSetAsSystem)},
2727
{"SOCKS5 Proxy", formatWorkingStatus(stats.SOCKS5.ListenerEnabled)},
2828
{"SOCKS5 Proxy address", stats.SOCKS5.ListenAddress},

0 commit comments

Comments
 (0)