diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..16f4324 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,39 @@ +version: "2" + +checks: + argument-count: + enabled: true + config: + threshold: 6 + complex-logic: + enabled: true + config: + threshold: 6 + file-lines: + enabled: true + config: + threshold: 1000 + method-complexity: + enabled: true + config: + threshold: 8 + method-count: + enabled: true + config: + threshold: 20 + method-lines: + enabled: true + config: + threshold: 100 + nested-control-flow: + enabled: true + config: + threshold: 6 + return-statements: + enabled: true + config: + threshold: 6 + similar-code: + enabled: false + identical-code: + enabled: false diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b92ce12..8cbb662 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,9 +24,6 @@ concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true -env: - SRC_DIR: src/github.com/${{ github.repository }} - jobs: Go: name: Go @@ -37,22 +34,18 @@ jobs: go: [ '1.19.x', '1.20.x' ] steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Go uses: actions/setup-go@v4 with: go-version: ${{ matrix.go }} - - name: Checkout - uses: actions/checkout@v3 - with: - path: ${{env.SRC_DIR}} - - name: Download dependencies - working-directory: ${{env.SRC_DIR}} run: make deps - name: Build binary - working-directory: ${{env.SRC_DIR}} run: make all Perfecto: @@ -76,3 +69,16 @@ jobs: uses: essentialkaos/perfecto-action@v2 with: files: common/uc.spec + + Typos: + name: Typos + runs-on: ubuntu-latest + + needs: Go + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Check spelling + uses: crate-ci/typos@master diff --git a/.typos.toml b/.typos.toml new file mode 100644 index 0000000..55aead8 --- /dev/null +++ b/.typos.toml @@ -0,0 +1,2 @@ +[files] +extend-exclude = ["go.sum"] diff --git a/README.md b/README.md index 3e27843..0f85dec 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@

- GitHub Actions CI Status GoReportCard + Code Climate Maintainability codebeat badge + GitHub Actions CI Status GitHub Actions CodeQL Status

@@ -22,7 +23,7 @@ #### From sources -To build the `uc` from scratch, make sure you have a working Go 1.19+ workspace (_[instructions](https://golang.org/doc/install)_), then: +To build the `uc` from scratch, make sure you have a working Go 1.19+ workspace (_[instructions](https://go.dev/doc/install)_), then: ``` go install github.com/essentialkaos/uc@latest @@ -100,7 +101,7 @@ Examples uc -d -m 5k file.txt Show distribution for file.txt with 5,000 uniq lines max - cat file.txt | uc - + cat file.txt | uc Count unique lines in stdin data ``` diff --git a/cli/cli.go b/cli/cli.go index 20e137e..c768c60 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -40,7 +40,7 @@ import ( // Application basic info const ( APP = "uc" - VER = "1.1.1" + VER = "2.0.0" DESC = "Tool for counting unique lines" ) @@ -104,7 +104,7 @@ var optMap = options.Map{ OPT_NO_PROGRESS: {Type: options.BOOL}, OPT_NO_COLOR: {Type: options.BOOL}, OPT_HELP: {Type: options.BOOL}, - OPT_VER: {Type: options.BOOL}, + OPT_VER: {Type: options.MIXED}, OPT_VERB_VER: {Type: options.BOOL}, OPT_COMPLETION: {}, @@ -141,12 +141,12 @@ func Run(gitRev string, gomod []byte) { printMan() os.Exit(0) case options.GetB(OPT_VER): - genAbout(gitRev).Print() + genAbout(gitRev).Print(options.GetS(OPT_VER)) os.Exit(0) case options.GetB(OPT_VERB_VER): support.Print(APP, VER, gitRev, gomod) os.Exit(0) - case options.GetB(OPT_HELP) || len(args) == 0: + case options.GetB(OPT_HELP): genUsage().Print() os.Exit(0) } @@ -157,7 +157,7 @@ func Run(gitRev string, gomod []byte) { signal.QUIT: signalHandler, }.TrackAsync() - processData(args.Get(0).String()) + processData(args) } // preConfigureUI preconfigures UI based on information about user terminal @@ -197,7 +197,7 @@ func configureUI() { } // processData starts data processing -func processData(input string) { +func processData(args options.Arguments) { var r *bufio.Reader stats = &Stats{ @@ -205,6 +205,8 @@ func processData(input string) { mx: &sync.Mutex{}, } + input := getInput(args) + if input == "-" { r = bufio.NewReader(os.Stdin) } else { @@ -221,6 +223,23 @@ func processData(input string) { readData(bufio.NewScanner(r)) } +// getInput returns input for reading data +func getInput(args options.Arguments) string { + if args.Get(0).String() == "-" || !fsutil.IsCharacterDevice("/dev/stdin") { + return "-" + } + + input := args.Get(0).Clean().String() + err := fsutil.ValidatePerms("FRS", input) + + if err != nil { + printError(err.Error()) + os.Exit(1) + } + + return input +} + // readData reads data func readData(s *bufio.Scanner) { ct := crc64.MakeTable(crc64.ECMA) @@ -319,7 +338,7 @@ func printResults() { stats.mx.Unlock() } -// printDistribution prints distrubution info +// printDistribution prints distribution info func printDistribution() { var distData linesSlice @@ -416,10 +435,8 @@ 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 with 5,000 uniq lines max") - info.AddRawExample( - "cat file.txt | "+APP+" -", - "Count unique lines in stdin data", - ) + info.AddRawExample("cat file.txt | "+APP, "Count unique lines in stdin data") + info.AddRawExample(APP+" -m 100 < file.txt", "Count unique lines in stdin data with 100 uniq lines max") return info } diff --git a/common/uc.spec b/common/uc.spec index 36e55cf..88209b3 100644 --- a/common/uc.spec +++ b/common/uc.spec @@ -6,7 +6,7 @@ Summary: Simple utility for counting unique lines Name: uc -Version: 1.1.1 +Version: 2.0.0 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -65,6 +65,9 @@ rm -rf %{buildroot} ################################################################################ %changelog +* Wed May 03 2023 Anton Novojilov - 2.0.0-0 +- Better standard input handling + * Mon Mar 06 2023 Anton Novojilov - 1.1.1-0 - Added verbose info output - Dependencies update diff --git a/go.mod b/go.mod index 8cc5b90..3ce7606 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/essentialkaos/uc go 1.18 require ( - github.com/essentialkaos/depsy v1.0.0 - github.com/essentialkaos/ek/v12 v12.63.0 + github.com/essentialkaos/depsy v1.1.0 + github.com/essentialkaos/ek/v12 v12.67.0 ) -require golang.org/x/sys v0.6.0 // indirect +require golang.org/x/sys v0.8.0 // indirect diff --git a/go.sum b/go.sum index a0cb15a..93bd658 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk= -github.com/essentialkaos/depsy v1.0.0 h1:FikBtTnNhk+xFO/hFr+CfiKs6QnA3wMD6tGL0XTEUkc= -github.com/essentialkaos/depsy v1.0.0/go.mod h1:XVsB2eVUonEzmLKQP3ig2P6v2+WcHVgJ10zm0JLqFMM= -github.com/essentialkaos/ek/v12 v12.63.0 h1:9yaEu5W3bx//9y52ShqYCoFDKOcwEdrnvgSkUYyatgI= -github.com/essentialkaos/ek/v12 v12.63.0/go.mod h1:9MlSuHpewu7OZ9tM9dLFHvoA8dflBIUPCA0Ctt97wRs= +github.com/essentialkaos/depsy v1.1.0 h1:U6dp687UkQwXlZU17Hg2KMxbp3nfZAoZ8duaeUFYvJI= +github.com/essentialkaos/depsy v1.1.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8= +github.com/essentialkaos/ek/v12 v12.67.0 h1:ii15t0O+3Mu7uI3Te5X5BIqYXi1V7ovtSIYi5LyiltU= +github.com/essentialkaos/ek/v12 v12.67.0/go.mod h1:sRgw+F7PeeoNsmLLVMKy5ccugwGSljk8/rTXC9dyT+M= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=