mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-12-09 20:33:45 +01:00
cmd/lncli: move commands and export
We want to export some of our CLI code to re-use in other projects. But in Golang you cannot import code from a `main` package. So we need to move the actual code into its own package and only have the `func main()` in the `main` package.
This commit is contained in:
48
cmd/commands/arg_parse.go
Normal file
48
cmd/commands/arg_parse.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// reTimeRange matches systemd.time-like short negative timeranges, e.g. "-200s".
|
||||
var reTimeRange = regexp.MustCompile(`^-\d{1,18}[s|m|h|d|w|M|y]$`)
|
||||
|
||||
// secondsPer allows translating s(seconds), m(minutes), h(ours), d(ays),
|
||||
// w(eeks), M(onths) and y(ears) into corresponding seconds.
|
||||
var secondsPer = map[string]int64{
|
||||
"s": 1,
|
||||
"m": 60,
|
||||
"h": 3600,
|
||||
"d": 86400,
|
||||
"w": 604800,
|
||||
"M": 2630016, // 30.44 days
|
||||
"y": 31557600, // 365.25 days
|
||||
}
|
||||
|
||||
// parseTime parses UNIX timestamps or short timeranges inspired by systemd
|
||||
// (when starting with "-"), e.g. "-1M" for one month (30.44 days) ago.
|
||||
func parseTime(s string, base time.Time) (uint64, error) {
|
||||
if reTimeRange.MatchString(s) {
|
||||
last := len(s) - 1
|
||||
|
||||
d, err := strconv.ParseInt(s[1:last], 10, 64)
|
||||
if err != nil {
|
||||
return uint64(0), err
|
||||
}
|
||||
|
||||
mul := secondsPer[string(s[last])]
|
||||
return uint64(base.Unix() - d*mul), nil
|
||||
}
|
||||
|
||||
return strconv.ParseUint(s, 10, 64)
|
||||
}
|
||||
|
||||
var lightningPrefix = "lightning:"
|
||||
|
||||
// stripPrefix removes accidentally copied 'lightning:' prefix.
|
||||
func stripPrefix(s string) string {
|
||||
return strings.TrimSpace(strings.TrimPrefix(s, lightningPrefix))
|
||||
}
|
||||
Reference in New Issue
Block a user