|
7 | 7 | "fmt" |
8 | 8 | "os" |
9 | 9 | "path" |
| 10 | + "runtime" |
10 | 11 | "strconv" |
11 | 12 | "strings" |
12 | 13 |
|
@@ -48,21 +49,48 @@ func New(updateType update.ApplicationType) *Application { |
48 | 49 | func (a *Application) Run() { |
49 | 50 | if len(os.Args) == 1 { |
50 | 51 | return |
51 | | - } else if os.Args[1] == WithEnvCommandName { |
| 52 | + } |
| 53 | + |
| 54 | + switch arg := os.Args[1]; arg { |
| 55 | + case WithEnvCommandName: |
52 | 56 | // is handled in linux_root_hacks.go |
53 | 57 | 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) |
58 | 73 | } |
| 74 | +} |
59 | 75 |
|
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> |
64 | 78 |
|
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) |
66 | 94 | } |
67 | 95 |
|
68 | 96 | func (a *Application) init() { |
@@ -437,34 +465,36 @@ func (a *Application) init() { |
437 | 465 | } |
438 | 466 | } |
439 | 467 |
|
440 | | -func (a *Application) initApiConnection(c *cli.Context) (err error) { |
| 468 | +func (a *Application) initApiConnection(c *cli.Context) error { |
441 | 469 | 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 | | - }() |
453 | 470 | if apiAddr != "" { |
454 | | - addr = apiAddr |
455 | | - return nil |
| 471 | + return a.initApiFromAddr(apiAddr) |
456 | 472 | } |
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 { |
461 | 481 | return nil |
462 | 482 | } |
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) |
466 | 495 | } |
467 | 496 |
|
| 497 | + a.api = api |
468 | 498 | return nil |
469 | 499 | } |
470 | 500 |
|
|
0 commit comments