Skip to content

Commit

Permalink
Merge pull request #7 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.0.0
  • Loading branch information
andyone authored Oct 23, 2020
2 parents a3831ff + bd1b53c commit 6ff4094
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 47 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: go

go:
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x
- tip

os:
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {} \;
Expand All @@ -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'

################################################################################
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions common/uc.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}

Expand All @@ -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}

Expand All @@ -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 <[email protected]> - 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 <[email protected]> - 0.0.2-0
- Added option -m/--max for defining maximum unique lines to process
- Added option -d/--dist for printing data distribution
Expand Down
134 changes: 94 additions & 40 deletions uc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@ 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"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// Application basic info
const (
APP = "uc"
VER = "0.0.2"
VER = "1.0.0"
DESC = "Tool for counting unique lines"
)

Expand All @@ -48,7 +51,8 @@ const (
OPT_HELP = "h:help"
OPT_VER = "v:version"

OPT_COMPLETION = "completion"
OPT_COMPLETION = "completion"
OPT_GENERATE_MAN = "generate-man"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand All @@ -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",
Expand All @@ -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 <https://www.apache.org/licenses/LICENSE-2.0>",
BugTracker: "https://github.com/essentialkaos/uc",
UpdateChecker: usage.UpdateChecker{"essentialkaos/uc", update.GitHubChecker},
}

about.Render()
}

0 comments on commit 6ff4094

Please sign in to comment.