Skip to content

Commit

Permalink
fixed version and help flags
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Sep 9, 2018
1 parent febd662 commit f605a4e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
10 changes: 5 additions & 5 deletions cmd/tipreport/tipreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/urfave/cli"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"

_ "github.com/mattn/go-sqlite3"
"github.com/michael1011/lightningtip/version"
"github.com/urfave/cli"
)

const (
version = "1.0.0"

defaultDataDir = "LightningTip"
defaultDatabaseFile = "tips.db"
)
Expand All @@ -26,7 +26,7 @@ func main() {
app.Name = "tipreport"
app.Usage = "display received tips"

app.Version = version
app.Version = version.Version

app.Flags = []cli.Flag{
cli.StringFlag{
Expand Down
47 changes: 34 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/michael1011/lightningtip/backends"
"github.com/michael1011/lightningtip/database"
"github.com/michael1011/lightningtip/notifications"
"github.com/michael1011/lightningtip/version"
"github.com/op/go-logging"
)

Expand Down Expand Up @@ -51,30 +52,37 @@ const (
defaultSTMPPassword = ""
)

type helpOptions struct {
ShowHelp bool `long:"help" short:"h" description:"Display this help message"`
ShowVersion bool `long:"version" short:"v" description:"Display version and exit"`
}

type config struct {
ConfigFile string `long:"config" Description:"Location of the config file"`
ConfigFile string `long:"config" description:"Location of the config file"`

DataDir string `long:"datadir" Description:"Location of the data stored by LightningTip"`
DataDir string `long:"datadir" description:"Location of the data stored by LightningTip"`

LogFile string `long:"logfile" Description:"Location of the log file"`
LogLevel string `long:"loglevel" Description:"Log level: debug, info, warning, error"`
LogFile string `long:"logfile" description:"Location of the log file"`
LogLevel string `long:"loglevel" description:"Log level: debug, info, warning, error"`

DatabaseFile string `long:"databasefile" Description:"Location of the database file to store settled invoices"`
DatabaseFile string `long:"databasefile" description:"Location of the database file to store settled invoices"`

RESTHost string `long:"resthost" Description:"Host for the REST interface of LightningTip"`
TLSCertFile string `long:"tlscertfile" Description:"Certificate for using LightningTip via HTTPS"`
TLSKeyFile string `long:"tlskeyfile" Description:"Certificate for using LightningTip via HTTPS"`
RESTHost string `long:"resthost" description:"Host for the REST interface of LightningTip"`
TLSCertFile string `long:"tlscertfile" description:"Certificate for using LightningTip via HTTPS"`
TLSKeyFile string `long:"tlskeyfile" description:"Certificate for using LightningTip via HTTPS"`

AccessDomain string `long:"accessdomain" Description:"The domain you are using LightningTip from"`
AccessDomain string `long:"accessdomain" description:"The domain you are using LightningTip from"`

TipExpiry int64 `long:"tipexpiry" Description:"Invoice expiry time in seconds"`
TipExpiry int64 `long:"tipexpiry" description:"Invoice expiry time in seconds"`

ReconnectInterval int64 `long:"reconnectinterval" Description:"Reconnect interval to LND in seconds"`
KeepAliveInterval int64 `long:"keepaliveinterval" Description:"Send a dummy request to LND to prevent timeouts "`
ReconnectInterval int64 `long:"reconnectinterval" description:"Reconnect interval to LND in seconds"`
KeepAliveInterval int64 `long:"keepaliveinterval" description:"Send a dummy request to LND to prevent timeouts "`

LND *backends.LND `group:"LND" namespace:"lnd"`

Mail *notifications.Mail `group:"Mail" namespace:"mail"`

Help *helpOptions `group:"Help Options"`
}

var cfg config
Expand Down Expand Up @@ -121,10 +129,23 @@ func initConfig() {
}

// Ignore unknown flags the first time parsing command line flags to prevent showing the unknown flag error twice
flags.NewParser(&cfg, flags.IgnoreUnknown).Parse()
parser := flags.NewParser(&cfg, flags.IgnoreUnknown)
parser.Parse()

errFile := flags.IniParse(cfg.ConfigFile, &cfg)

// If the user just wants to see the version initializing everything else is irrelevant
if cfg.Help.ShowVersion {
version.PrintVersion()
os.Exit(0)
}

// If the user just wants to see the help message
if cfg.Help.ShowHelp {
parser.WriteHelp(os.Stdout)
os.Exit(0)
}

// Parse flags again to override config file
_, err := flags.Parse(&cfg)

Expand Down
5 changes: 3 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"github.com/op/go-logging"
"os"

"github.com/op/go-logging"
)

var log = logging.MustGetLogger("")
var logFormat = logging.MustStringFormatter("%{time:2006-01-02 15:04:05.000} [%{level}] %{message}")
var logFormat = logging.MustStringFormatter("%{time:2006-01-02 15:04:05} [%{level}] %{message}")

var backendConsole = logging.NewLogBackend(os.Stdout, "", 0)

Expand Down
11 changes: 11 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package version

import "fmt"

// Version is the version of LightningTip
const Version = "1.1.0-dev"

// PrintVersion prints the version of LightningTip to the console
func PrintVersion() {
fmt.Println("LightningTip version " + Version)
}

0 comments on commit f605a4e

Please sign in to comment.