Skip to content

Commit a65825b

Browse files
author
Brent Beardsley
committed
add format option
1 parent d533311 commit a65825b

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@
1212
*.out
1313

1414
debug
15-
histkeep
1615
histkeep-cli
17-
dist/histkeep.rb
18-
dist/histkeep*.zip
16+
dist/*

cmd/histkeep/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/bbeardsley/histkeep-cli
2+
3+
go 1.14
4+
5+
require (
6+
github.com/bbeardsley/histkeep v0.0.3
7+
)

cmd/histkeep/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/bbeardsley/histkeep v0.0.1 h1:Io27oE3jAG2FT/xhhspqzYFm/STvgPD1+d8Q1uweEEw=
2+
github.com/bbeardsley/histkeep v0.0.1/go.mod h1:bYUZoqU122IpvVr8h1H19KkV3MgQ10C9bMNKK6ZP05k=
3+
github.com/bbeardsley/histkeep v0.0.3 h1:clPq3s1ZuJZFqbiqivklJfnMzpAM7ndahgZjAa7ZEko=
4+
github.com/bbeardsley/histkeep v0.0.3/go.mod h1:bYUZoqU122IpvVr8h1H19KkV3MgQ10C9bMNKK6ZP05k=

cmd/histkeep/main.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"regexp"
89
"strings"
910

1011
"github.com/bbeardsley/histkeep"
1112
)
1213

13-
const version = "0.0.2"
14+
const version = "0.0.3"
1415

1516
func printUsage() {
1617
fmt.Fprintln(os.Stderr, "Usage")
@@ -31,6 +32,7 @@ func printUsage() {
3132

3233
func main() {
3334
lastNPtr := flag.Int("last", 15, "keep the last specified number of values")
35+
formatPtr := flag.String("format", "", "regex format for the values. Accepts NUMBER and UUID as shortcuts.")
3436
versionPtr := flag.Bool("version", false, "print version number and exit")
3537

3638
flag.Parse()
@@ -43,8 +45,7 @@ func main() {
4345
command := strings.TrimSpace(flag.Arg(0))
4446
file := strings.TrimSpace(flag.Arg(1))
4547
value := strings.TrimSpace(flag.Arg(2))
46-
47-
hist := histkeep.NewHistKeep(file, *lastNPtr)
48+
hist := histkeep.NewHistKeep(file, *lastNPtr, buildFormat(*formatPtr))
4849

4950
switch command {
5051
case "", "h", "-h", "--h", "/h", "/?", "help", "-help", "--help", "/help":
@@ -93,3 +94,24 @@ func main() {
9394

9495
return
9596
}
97+
98+
func buildFormat(formatStr string) *regexp.Regexp {
99+
format := processedNamedFormats(formatStr)
100+
regex, _ := regexp.Compile("^" + format + "$")
101+
return regex
102+
}
103+
104+
func processedNamedFormats(formatStr string) string {
105+
var matched bool
106+
matched, _ = regexp.MatchString("^NUMBER$", formatStr)
107+
if matched {
108+
return "\\d+"
109+
}
110+
111+
matched, _ = regexp.MatchString("^UUID$", formatStr)
112+
if matched {
113+
return "([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}"
114+
}
115+
116+
return formatStr
117+
}

0 commit comments

Comments
 (0)