Skip to content

Commit bd1b53c

Browse files
committed
Improvements
1 parent 75cfed5 commit bd1b53c

File tree

5 files changed

+120
-47
lines changed

5 files changed

+120
-47
lines changed

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
language: go
22

33
go:
4-
- 1.12.x
5-
- 1.13.x
64
- 1.14.x
5+
- 1.15.x
76
- tip
87

98
os:

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
################################################################################
22

3-
# This Makefile generated by GoMakeGen 1.3.0 using next command:
3+
# This Makefile generated by GoMakeGen 1.3.1 using next command:
44
# gomakegen .
55
#
66
# More info: https://kaos.sh/gomakegen
@@ -27,7 +27,7 @@ git-config: ## Configure git redirects for stable import path services
2727
git config --global http.https://pkg.re.followRedirects true
2828

2929
deps: git-config ## Download dependencies
30-
go get -d -v pkg.re/essentialkaos/ek.v11
30+
go get -d -v pkg.re/essentialkaos/ek.v12
3131

3232
fmt: ## Format source code with gofmt
3333
find . -name "*.go" -exec gofmt -s -w {} \;
@@ -43,6 +43,6 @@ help: ## Show this info
4343
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
4444
| awk 'BEGIN {FS = ":.*?## "}; {printf " \033[33m%-12s\033[0m %s\n", $$1, $$2}'
4545
@echo -e ''
46-
@echo -e '\033[90mGenerated by GoMakeGen 1.3.0\033[0m\n'
46+
@echo -e '\033[90mGenerated by GoMakeGen 1.3.1\033[0m\n'
4747

4848
################################################################################

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ Fish:
7878
sudo uc --completion=fish 1> /usr/share/fish/vendor_completions.d/uc.fish
7979
```
8080

81+
### Man documentation
82+
83+
You can generate man page for `uc` using next command:
84+
85+
```bash
86+
uc --generate-man | sudo gzip > /usr/share/man/man1/uc.1.gz
87+
```
88+
8189
### Usage
8290

8391
```
@@ -101,6 +109,9 @@ Examples
101109
uc -d file.txt
102110
Show distribution for file.txt
103111
112+
uc -d -m 5k file.txt
113+
Show distribution for file.txt for 5,000 uniq lines max
114+
104115
cat file.txt | uc -
105116
Count unique lines in stdin data
106117

common/uc.spec

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
Summary: Simple utility for counting unique lines
1212
Name: uc
13-
Version: 0.0.2
13+
Version: 1.0.0
1414
Release: 0%{?dist}
1515
Group: Applications/System
1616
License: Apache License, Version 2.0
@@ -20,7 +20,7 @@ Source0: https://source.kaos.st/%{name}/%{name}-%{version}.tar.bz2
2020

2121
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
2222

23-
BuildRequires: golang >= 1.11
23+
BuildRequires: golang >= 1.14
2424

2525
Provides: %{name} = %{version}-%{release}
2626

@@ -42,8 +42,12 @@ go build src/github.com/essentialkaos/%{name}/%{name}.go
4242
rm -rf %{buildroot}
4343

4444
install -dm 755 %{buildroot}%{_bindir}
45+
install -dm 755 %{buildroot}%{_mandir}/man1
46+
4547
install -pm 755 %{name} %{buildroot}%{_bindir}/
4648

49+
./%{name} --generate-man > %{buildroot}%{_mandir}/man1/%{name}.1
50+
4751
%clean
4852
rm -rf %{buildroot}
4953

@@ -52,11 +56,16 @@ rm -rf %{buildroot}
5256
%files
5357
%defattr(-,root,root,-)
5458
%doc LICENSE
59+
%{_mandir}/man1/%{name}.1.*
5560
%{_bindir}/%{name}
5661

5762
################################################################################
5863

5964
%changelog
65+
* Thu Oct 22 2020 Anton Novojilov <[email protected]> - 1.0.0-0
66+
- Added possibility to define -m/--max option as number with K and M
67+
- Added man page generation
68+
6069
* Fri Jan 31 2020 Anton Novojilov <[email protected]> - 0.0.2-0
6170
- Added option -m/--max for defining maximum unique lines to process
6271
- Added option -d/--dist for printing data distribution

uc.go

+94-40
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,31 @@ import (
1414
"os"
1515
"runtime"
1616
"sort"
17+
"strconv"
18+
"strings"
1719
"sync"
1820
"time"
1921

20-
"pkg.re/essentialkaos/ek.v11/fmtc"
21-
"pkg.re/essentialkaos/ek.v11/fmtutil"
22-
"pkg.re/essentialkaos/ek.v11/fsutil"
23-
"pkg.re/essentialkaos/ek.v11/options"
24-
"pkg.re/essentialkaos/ek.v11/signal"
25-
"pkg.re/essentialkaos/ek.v11/strutil"
26-
"pkg.re/essentialkaos/ek.v11/usage"
27-
"pkg.re/essentialkaos/ek.v11/usage/completion/bash"
28-
"pkg.re/essentialkaos/ek.v11/usage/completion/fish"
29-
"pkg.re/essentialkaos/ek.v11/usage/completion/zsh"
30-
"pkg.re/essentialkaos/ek.v11/usage/update"
22+
"pkg.re/essentialkaos/ek.v12/fmtc"
23+
"pkg.re/essentialkaos/ek.v12/fmtutil"
24+
"pkg.re/essentialkaos/ek.v12/fsutil"
25+
"pkg.re/essentialkaos/ek.v12/options"
26+
"pkg.re/essentialkaos/ek.v12/signal"
27+
"pkg.re/essentialkaos/ek.v12/strutil"
28+
"pkg.re/essentialkaos/ek.v12/usage"
29+
"pkg.re/essentialkaos/ek.v12/usage/completion/bash"
30+
"pkg.re/essentialkaos/ek.v12/usage/completion/fish"
31+
"pkg.re/essentialkaos/ek.v12/usage/completion/zsh"
32+
"pkg.re/essentialkaos/ek.v12/usage/man"
33+
"pkg.re/essentialkaos/ek.v12/usage/update"
3134
)
3235

3336
// ////////////////////////////////////////////////////////////////////////////////// //
3437

3538
// Application basic info
3639
const (
3740
APP = "uc"
38-
VER = "0.0.2"
41+
VER = "1.0.0"
3942
DESC = "Tool for counting unique lines"
4043
)
4144

@@ -48,7 +51,8 @@ const (
4851
OPT_HELP = "h:help"
4952
OPT_VER = "v:version"
5053

51-
OPT_COMPLETION = "completion"
54+
OPT_COMPLETION = "completion"
55+
OPT_GENERATE_MAN = "generate-man"
5256
)
5357

5458
// ////////////////////////////////////////////////////////////////////////////////// //
@@ -92,14 +96,15 @@ func (s linesSlice) Less(i, j int) bool {
9296

9397
// optMap is map with options
9498
var optMap = options.Map{
95-
OPT_MAX_LINES: {Type: options.INT},
99+
OPT_MAX_LINES: {Type: options.MIXED},
96100
OPT_DISTRIBUTION: {Type: options.BOOL},
97101
OPT_NO_PROGRESS: {Type: options.BOOL},
98102
OPT_NO_COLOR: {Type: options.BOOL},
99103
OPT_HELP: {Type: options.BOOL, Alias: "u:usage"},
100104
OPT_VER: {Type: options.BOOL, Alias: "ver"},
101105

102-
OPT_COMPLETION: {},
106+
OPT_COMPLETION: {},
107+
OPT_GENERATE_MAN: {Type: options.BOOL},
103108
}
104109

105110
// stats contains info about data
@@ -127,7 +132,12 @@ func main() {
127132
}
128133

129134
if options.Has(OPT_COMPLETION) {
130-
genCompletion()
135+
os.Exit(genCompletion())
136+
}
137+
138+
if options.Has(OPT_GENERATE_MAN) {
139+
genMan()
140+
os.Exit(0)
131141
}
132142

133143
configureUI()
@@ -195,7 +205,12 @@ func processData(input string) {
195205
func readData(s *bufio.Scanner) {
196206
ct := crc64.MakeTable(crc64.ECMA)
197207
dist := options.GetB(OPT_DISTRIBUTION)
198-
maxLines := options.GetI(OPT_MAX_LINES)
208+
maxLines, err := parseMaxLines(options.GetS(OPT_MAX_LINES))
209+
210+
if err != nil {
211+
printError(err.Error())
212+
os.Exit(1)
213+
}
199214

200215
if dist {
201216
stats.Samples = make(map[uint64]string)
@@ -299,6 +314,30 @@ func printDistribution() {
299314
}
300315
}
301316

317+
// parseMaxLines parses max line option
318+
func parseMaxLines(maxLines string) (int, error) {
319+
maxLines = strings.ToUpper(maxLines)
320+
321+
mp := 1
322+
323+
switch {
324+
case strings.HasSuffix(maxLines, "K"):
325+
maxLines = strutil.Exclude(maxLines, "K")
326+
mp = 1000
327+
case strings.HasSuffix(maxLines, "M"):
328+
mp = 1000 * 1000
329+
maxLines = strutil.Exclude(maxLines, "M")
330+
}
331+
332+
num, err := strconv.Atoi(maxLines)
333+
334+
if err != nil {
335+
return 0, err
336+
}
337+
338+
return num * mp, nil
339+
}
340+
302341
// signalHandler is signal handler
303342
func signalHandler() {
304343
printResults()
@@ -312,11 +351,42 @@ func printError(f string, a ...interface{}) {
312351

313352
// ////////////////////////////////////////////////////////////////////////////////// //
314353

315-
// showUsage print usage info
354+
// showUsage prints usage info
316355
func showUsage() {
317356
genUsage().Render()
318357
}
319358

359+
// showAbout prints info about version
360+
func showAbout() {
361+
genAbout().Render()
362+
}
363+
364+
// genCompletion generates completion for different shells
365+
func genCompletion() int {
366+
switch options.GetS(OPT_COMPLETION) {
367+
case "bash":
368+
fmt.Printf(bash.Generate(genUsage(), APP))
369+
case "fish":
370+
fmt.Printf(fish.Generate(genUsage(), APP))
371+
case "zsh":
372+
fmt.Printf(zsh.Generate(genUsage(), optMap, APP))
373+
default:
374+
return 1
375+
}
376+
377+
return 0
378+
}
379+
380+
// genMan generates man page
381+
func genMan() {
382+
fmt.Println(
383+
man.Generate(
384+
genUsage(),
385+
genAbout(),
386+
),
387+
)
388+
}
389+
320390
// genUsage generates usage info
321391
func genUsage() *usage.Info {
322392
info := usage.NewInfo(APP, "file")
@@ -331,6 +401,7 @@ func genUsage() *usage.Info {
331401

332402
info.AddExample("file.txt", "Count unique lines in file.txt")
333403
info.AddExample("-d file.txt", "Show distribution for file.txt")
404+
info.AddExample("-d -m 5k file.txt", "Show distribution for file.txt for 5,000 uniq lines max")
334405
info.AddRawExample(
335406
"cat file.txt | "+APP+" -",
336407
"Count unique lines in stdin data",
@@ -339,33 +410,16 @@ func genUsage() *usage.Info {
339410
return info
340411
}
341412

342-
// genCompletion generates completion for different shells
343-
func genCompletion() {
344-
switch options.GetS(OPT_COMPLETION) {
345-
case "bash":
346-
fmt.Printf(bash.Generate(genUsage(), APP))
347-
case "fish":
348-
fmt.Printf(fish.Generate(genUsage(), APP))
349-
case "zsh":
350-
fmt.Printf(zsh.Generate(genUsage(), optMap, APP))
351-
default:
352-
os.Exit(1)
353-
}
354-
355-
os.Exit(0)
356-
}
357-
358-
// showAbout print info about version
359-
func showAbout() {
360-
about := &usage.About{
413+
// genAbout generates info about version
414+
func genAbout() *usage.About {
415+
return &usage.About{
361416
App: APP,
362417
Version: VER,
363418
Desc: DESC,
364419
Year: 2009,
365-
Owner: "Essential Kaos",
420+
Owner: "ESSENTIAL KAOS",
366421
License: "Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>",
422+
BugTracker: "https://github.com/essentialkaos/uc",
367423
UpdateChecker: usage.UpdateChecker{"essentialkaos/uc", update.GitHubChecker},
368424
}
369-
370-
about.Render()
371425
}

0 commit comments

Comments
 (0)