Skip to content

Commit c22c0db

Browse files
committed
aperture: parse command line flags
1 parent 83a657d commit c22c0db

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

aperture.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package aperture
22

33
import (
44
"crypto/tls"
5+
"errors"
56
"fmt"
67
"io"
78
"io/ioutil"
@@ -11,6 +12,7 @@ import (
1112
"sync"
1213
"time"
1314

15+
flags "github.com/jessevdk/go-flags"
1416
"github.com/lightninglabs/aperture/auth"
1517
"github.com/lightninglabs/aperture/mint"
1618
"github.com/lightninglabs/aperture/proxy"
@@ -71,10 +73,21 @@ var (
7173
func Main() {
7274
// TODO: Prevent from running twice.
7375
err := run()
74-
if err != nil {
75-
_, _ = fmt.Fprintln(os.Stderr, err)
76-
os.Exit(1)
76+
77+
// Unwrap our error and check whether help was requested from our flag
78+
// library. If the error is not wrapped, Unwrap returns nil. It is
79+
// still safe to check the type of this nil error.
80+
flagErr, isFlagErr := errors.Unwrap(err).(*flags.Error)
81+
isHelpErr := isFlagErr && flagErr.Type == flags.ErrHelp
82+
83+
// If we got a nil error, or help was requested, just exit.
84+
if err == nil || isHelpErr {
85+
os.Exit(0)
7786
}
87+
88+
// Print any other non-help related errors.
89+
_, _ = fmt.Fprintln(os.Stderr, err)
90+
os.Exit(1)
7891
}
7992

8093
// run sets up the proxy server and runs it. This function blocks until a
@@ -91,7 +104,7 @@ func run() error {
91104
configFile := filepath.Join(apertureDataDir, defaultConfigFilename)
92105
cfg, err := getConfig(configFile)
93106
if err != nil {
94-
return fmt.Errorf("unable to parse config file: %v", err)
107+
return fmt.Errorf("unable to parse config file: %w", err)
95108
}
96109
err = setupLogging(cfg, interceptor)
97110
if err != nil {
@@ -316,6 +329,12 @@ func getConfig(configFile string) (*Config, error) {
316329
return nil, err
317330
}
318331

332+
// Finally, parse the remaining command line options again to ensure
333+
// they take precedence.
334+
if _, err := flags.Parse(cfg); err != nil {
335+
return nil, err
336+
}
337+
319338
// Then check the configuration that we got from the config file, all
320339
// required values need to be set at this point.
321340
if cfg.ListenAddr == "" {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/btcsuite/btcwallet/wtxmgr v1.3.1-0.20210706234807-aaf03fee735a
1010
github.com/fortytw2/leaktest v1.3.0
1111
github.com/golang/protobuf v1.5.2
12+
github.com/jessevdk/go-flags v1.4.0
1213
github.com/lightninglabs/lndclient v0.12.0-9
1314
github.com/lightningnetwork/lnd v0.13.0-beta.rc5.0.20210728112744-ebabda671786
1415
github.com/lightningnetwork/lnd/cert v1.0.3

0 commit comments

Comments
 (0)