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 @@
@@ -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