From bd1b53cdcdf2ddf2718c173f02e3349e033578d3 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 23 Oct 2020 13:56:57 +0300 Subject: [PATCH] Improvements --- .travis.yml | 3 +- Makefile | 6 +-- README.md | 11 ++++ common/uc.spec | 13 ++++- uc.go | 134 ++++++++++++++++++++++++++++++++++--------------- 5 files changed, 120 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6f88316..34f6017 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ language: go go: - - 1.12.x - - 1.13.x - 1.14.x + - 1.15.x - tip os: diff --git a/Makefile b/Makefile index 8689fc9..8afc579 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ ################################################################################ -# This Makefile generated by GoMakeGen 1.3.0 using next command: +# This Makefile generated by GoMakeGen 1.3.1 using next command: # gomakegen . # # More info: https://kaos.sh/gomakegen @@ -27,7 +27,7 @@ git-config: ## Configure git redirects for stable import path services git config --global http.https://pkg.re.followRedirects true deps: git-config ## Download dependencies - go get -d -v pkg.re/essentialkaos/ek.v11 + go get -d -v pkg.re/essentialkaos/ek.v12 fmt: ## Format source code with gofmt find . -name "*.go" -exec gofmt -s -w {} \; @@ -43,6 +43,6 @@ help: ## Show this info @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[33m%-12s\033[0m %s\n", $$1, $$2}' @echo -e '' - @echo -e '\033[90mGenerated by GoMakeGen 1.3.0\033[0m\n' + @echo -e '\033[90mGenerated by GoMakeGen 1.3.1\033[0m\n' ################################################################################ diff --git a/README.md b/README.md index 0544c14..88d2b9f 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,14 @@ Fish: sudo uc --completion=fish 1> /usr/share/fish/vendor_completions.d/uc.fish ``` +### Man documentation + +You can generate man page for `uc` using next command: + +```bash +uc --generate-man | sudo gzip > /usr/share/man/man1/uc.1.gz +``` + ### Usage ``` @@ -101,6 +109,9 @@ Examples uc -d file.txt Show distribution for file.txt + uc -d -m 5k file.txt + Show distribution for file.txt for 5,000 uniq lines max + cat file.txt | uc - Count unique lines in stdin data diff --git a/common/uc.spec b/common/uc.spec index 60dcb4b..da64adf 100644 --- a/common/uc.spec +++ b/common/uc.spec @@ -10,7 +10,7 @@ Summary: Simple utility for counting unique lines Name: uc -Version: 0.0.2 +Version: 1.0.0 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -20,7 +20,7 @@ Source0: https://source.kaos.st/%{name}/%{name}-%{version}.tar.bz2 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) -BuildRequires: golang >= 1.11 +BuildRequires: golang >= 1.14 Provides: %{name} = %{version}-%{release} @@ -42,8 +42,12 @@ go build src/github.com/essentialkaos/%{name}/%{name}.go rm -rf %{buildroot} install -dm 755 %{buildroot}%{_bindir} +install -dm 755 %{buildroot}%{_mandir}/man1 + install -pm 755 %{name} %{buildroot}%{_bindir}/ +./%{name} --generate-man > %{buildroot}%{_mandir}/man1/%{name}.1 + %clean rm -rf %{buildroot} @@ -52,11 +56,16 @@ rm -rf %{buildroot} %files %defattr(-,root,root,-) %doc LICENSE +%{_mandir}/man1/%{name}.1.* %{_bindir}/%{name} ################################################################################ %changelog +* Thu Oct 22 2020 Anton Novojilov - 1.0.0-0 +- Added possibility to define -m/--max option as number with K and M +- Added man page generation + * Fri Jan 31 2020 Anton Novojilov - 0.0.2-0 - Added option -m/--max for defining maximum unique lines to process - Added option -d/--dist for printing data distribution diff --git a/uc.go b/uc.go index e1d5838..d3a2438 100644 --- a/uc.go +++ b/uc.go @@ -14,20 +14,23 @@ import ( "os" "runtime" "sort" + "strconv" + "strings" "sync" "time" - "pkg.re/essentialkaos/ek.v11/fmtc" - "pkg.re/essentialkaos/ek.v11/fmtutil" - "pkg.re/essentialkaos/ek.v11/fsutil" - "pkg.re/essentialkaos/ek.v11/options" - "pkg.re/essentialkaos/ek.v11/signal" - "pkg.re/essentialkaos/ek.v11/strutil" - "pkg.re/essentialkaos/ek.v11/usage" - "pkg.re/essentialkaos/ek.v11/usage/completion/bash" - "pkg.re/essentialkaos/ek.v11/usage/completion/fish" - "pkg.re/essentialkaos/ek.v11/usage/completion/zsh" - "pkg.re/essentialkaos/ek.v11/usage/update" + "pkg.re/essentialkaos/ek.v12/fmtc" + "pkg.re/essentialkaos/ek.v12/fmtutil" + "pkg.re/essentialkaos/ek.v12/fsutil" + "pkg.re/essentialkaos/ek.v12/options" + "pkg.re/essentialkaos/ek.v12/signal" + "pkg.re/essentialkaos/ek.v12/strutil" + "pkg.re/essentialkaos/ek.v12/usage" + "pkg.re/essentialkaos/ek.v12/usage/completion/bash" + "pkg.re/essentialkaos/ek.v12/usage/completion/fish" + "pkg.re/essentialkaos/ek.v12/usage/completion/zsh" + "pkg.re/essentialkaos/ek.v12/usage/man" + "pkg.re/essentialkaos/ek.v12/usage/update" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -35,7 +38,7 @@ import ( // Application basic info const ( APP = "uc" - VER = "0.0.2" + VER = "1.0.0" DESC = "Tool for counting unique lines" ) @@ -48,7 +51,8 @@ const ( OPT_HELP = "h:help" OPT_VER = "v:version" - OPT_COMPLETION = "completion" + OPT_COMPLETION = "completion" + OPT_GENERATE_MAN = "generate-man" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -92,14 +96,15 @@ func (s linesSlice) Less(i, j int) bool { // optMap is map with options var optMap = options.Map{ - OPT_MAX_LINES: {Type: options.INT}, + OPT_MAX_LINES: {Type: options.MIXED}, OPT_DISTRIBUTION: {Type: options.BOOL}, OPT_NO_PROGRESS: {Type: options.BOOL}, OPT_NO_COLOR: {Type: options.BOOL}, OPT_HELP: {Type: options.BOOL, Alias: "u:usage"}, OPT_VER: {Type: options.BOOL, Alias: "ver"}, - OPT_COMPLETION: {}, + OPT_COMPLETION: {}, + OPT_GENERATE_MAN: {Type: options.BOOL}, } // stats contains info about data @@ -127,7 +132,12 @@ func main() { } if options.Has(OPT_COMPLETION) { - genCompletion() + os.Exit(genCompletion()) + } + + if options.Has(OPT_GENERATE_MAN) { + genMan() + os.Exit(0) } configureUI() @@ -195,7 +205,12 @@ func processData(input string) { func readData(s *bufio.Scanner) { ct := crc64.MakeTable(crc64.ECMA) dist := options.GetB(OPT_DISTRIBUTION) - maxLines := options.GetI(OPT_MAX_LINES) + maxLines, err := parseMaxLines(options.GetS(OPT_MAX_LINES)) + + if err != nil { + printError(err.Error()) + os.Exit(1) + } if dist { stats.Samples = make(map[uint64]string) @@ -299,6 +314,30 @@ func printDistribution() { } } +// parseMaxLines parses max line option +func parseMaxLines(maxLines string) (int, error) { + maxLines = strings.ToUpper(maxLines) + + mp := 1 + + switch { + case strings.HasSuffix(maxLines, "K"): + maxLines = strutil.Exclude(maxLines, "K") + mp = 1000 + case strings.HasSuffix(maxLines, "M"): + mp = 1000 * 1000 + maxLines = strutil.Exclude(maxLines, "M") + } + + num, err := strconv.Atoi(maxLines) + + if err != nil { + return 0, err + } + + return num * mp, nil +} + // signalHandler is signal handler func signalHandler() { printResults() @@ -312,11 +351,42 @@ func printError(f string, a ...interface{}) { // ////////////////////////////////////////////////////////////////////////////////// // -// showUsage print usage info +// showUsage prints usage info func showUsage() { genUsage().Render() } +// showAbout prints info about version +func showAbout() { + genAbout().Render() +} + +// genCompletion generates completion for different shells +func genCompletion() int { + switch options.GetS(OPT_COMPLETION) { + case "bash": + fmt.Printf(bash.Generate(genUsage(), APP)) + case "fish": + fmt.Printf(fish.Generate(genUsage(), APP)) + case "zsh": + fmt.Printf(zsh.Generate(genUsage(), optMap, APP)) + default: + return 1 + } + + return 0 +} + +// genMan generates man page +func genMan() { + fmt.Println( + man.Generate( + genUsage(), + genAbout(), + ), + ) +} + // genUsage generates usage info func genUsage() *usage.Info { info := usage.NewInfo(APP, "file") @@ -331,6 +401,7 @@ func genUsage() *usage.Info { info.AddExample("file.txt", "Count unique lines in file.txt") info.AddExample("-d file.txt", "Show distribution for file.txt") + info.AddExample("-d -m 5k file.txt", "Show distribution for file.txt for 5,000 uniq lines max") info.AddRawExample( "cat file.txt | "+APP+" -", "Count unique lines in stdin data", @@ -339,33 +410,16 @@ func genUsage() *usage.Info { return info } -// genCompletion generates completion for different shells -func genCompletion() { - switch options.GetS(OPT_COMPLETION) { - case "bash": - fmt.Printf(bash.Generate(genUsage(), APP)) - case "fish": - fmt.Printf(fish.Generate(genUsage(), APP)) - case "zsh": - fmt.Printf(zsh.Generate(genUsage(), optMap, APP)) - default: - os.Exit(1) - } - - os.Exit(0) -} - -// showAbout print info about version -func showAbout() { - about := &usage.About{ +// genAbout generates info about version +func genAbout() *usage.About { + return &usage.About{ App: APP, Version: VER, Desc: DESC, Year: 2009, - Owner: "Essential Kaos", + Owner: "ESSENTIAL KAOS", License: "Apache License, Version 2.0 ", + BugTracker: "https://github.com/essentialkaos/uc", UpdateChecker: usage.UpdateChecker{"essentialkaos/uc", update.GitHubChecker}, } - - about.Render() }