diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8cbb662..ae800f8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,10 +35,10 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} @@ -56,10 +56,10 @@ jobs: steps: - name: Code checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Login to GitHub Container Registry - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} @@ -78,7 +78,8 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Check spelling + continue-on-error: true uses: crate-ci/typos@master diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index bf5ecc9..bfc4df5 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -20,14 +20,14 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 2 - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: go - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 diff --git a/Makefile b/Makefile index 8b219e3..17726e0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ ################################################################################ -# This Makefile generated by GoMakeGen 2.2.0 using next command: +# This Makefile generated by GoMakeGen 2.3.0 using next command: # gomakegen --mod . # # More info: https://kaos.sh/gomakegen @@ -13,6 +13,7 @@ ifdef VERBOSE ## Print verbose information (Flag) VERBOSE_FLAG = -v endif +COMPAT ?= 1.18 MAKEDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) GITREV ?= $(shell test -s $(MAKEDIR)/.git && git rev-parse --short HEAD) @@ -50,7 +51,7 @@ else endif ifdef COMPAT ## Compatible Go version (String) - go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) + go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) -go=$(COMPAT) else go mod tidy $(VERBOSE_FLAG) endif @@ -94,6 +95,6 @@ help: ## Show this info | sed 's/ifdef //' \ | awk 'BEGIN {FS = " .*?## "}; {printf " \033[32m%-14s\033[0m %s\n", $$1, $$2}' @echo -e '' - @echo -e '\033[90mGenerated by GoMakeGen 2.2.0\033[0m\n' + @echo -e '\033[90mGenerated by GoMakeGen 2.3.0\033[0m\n' ################################################################################ diff --git a/README.md b/README.md index 0f85dec..f4b10da 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,10 @@ To build the `uc` from scratch, make sure you have a working Go 1.19+ workspace go install github.com/essentialkaos/uc@latest ``` -#### From [ESSENTIAL KAOS Public Repository](https://yum.kaos.st) +#### From [ESSENTIAL KAOS Public Repository](https://pkgs.kaos.st) ```bash -sudo yum install -y https://yum.kaos.st/kaos-repo-latest.el$(grep 'CPE_NAME' /etc/os-release | tr -d '"' | cut -d':' -f5).noarch.rpm +sudo yum install -y https://pkgs.kaos.st/kaos-repo-latest.el$(grep 'CPE_NAME' /etc/os-release | tr -d '"' | cut -d':' -f5).noarch.rpm sudo yum install uc ``` @@ -103,6 +103,9 @@ Examples cat file.txt | uc Count unique lines in stdin data + + uc -m 100 < file.txt + Count unique lines in stdin data with 100 uniq lines max ``` ### Build Status diff --git a/cli/cli.go b/cli/cli.go index c768c60..f0319c3 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -25,6 +25,7 @@ import ( "github.com/essentialkaos/ek/v12/options" "github.com/essentialkaos/ek/v12/signal" "github.com/essentialkaos/ek/v12/strutil" + "github.com/essentialkaos/ek/v12/terminal/tty" "github.com/essentialkaos/ek/v12/usage" "github.com/essentialkaos/ek/v12/usage/completion/bash" "github.com/essentialkaos/ek/v12/usage/completion/fish" @@ -40,7 +41,7 @@ import ( // Application basic info const ( APP = "uc" - VER = "2.0.0" + VER = "2.0.1" DESC = "Tool for counting unique lines" ) @@ -117,6 +118,12 @@ var stats *Stats // rawMode is raw mode flag var rawMode bool +// colorTagApp contains color tag for app name +var colorTagApp string + +// colorTagVer contains color tag for app version +var colorTagVer string + // ////////////////////////////////////////////////////////////////////////////////// // // Run is main application function @@ -162,27 +169,10 @@ func Run(gitRev string, gomod []byte) { // preConfigureUI preconfigures UI based on information about user terminal func preConfigureUI() { - term := os.Getenv("TERM") - - fmtc.DisableColors = true - - if term != "" { - switch { - case strings.Contains(term, "xterm"), - strings.Contains(term, "color"), - term == "screen": - fmtc.DisableColors = false - } - } - - if !fsutil.IsCharacterDevice("/dev/stdout") && os.Getenv("FAKETTY") == "" { + if !tty.IsTTY() { fmtc.DisableColors = true rawMode = true } - - if os.Getenv("NO_COLOR") != "" { - fmtc.DisableColors = true - } } // configureUI configures user interface @@ -194,6 +184,13 @@ func configureUI() { if options.GetB(OPT_NO_PROGRESS) { rawMode = true } + + switch { + case fmtc.Is256ColorsSupported(): + colorTagApp, colorTagVer = "{*}{#168}", "{#168}" + default: + colorTagApp, colorTagVer = "{*}{m}", "{m}" + } } // processData starts data processing @@ -398,11 +395,11 @@ func printError(f string, a ...interface{}) { func printCompletion() int { switch options.GetS(OPT_COMPLETION) { case "bash": - fmt.Printf(bash.Generate(genUsage(), APP)) + fmt.Print(bash.Generate(genUsage(), APP)) case "fish": - fmt.Printf(fish.Generate(genUsage(), APP)) + fmt.Print(fish.Generate(genUsage(), APP)) case "zsh": - fmt.Printf(zsh.Generate(genUsage(), optMap, APP)) + fmt.Print(zsh.Generate(genUsage(), optMap, APP)) default: return 1 } @@ -424,6 +421,8 @@ func printMan() { func genUsage() *usage.Info { info := usage.NewInfo(APP, "file") + info.AppNameColorTag = colorTagApp + info.AddOption(OPT_DISTRIBUTION, "Show number of occurrences for every line") info.AddOption(OPT_MAX_LINES, "Max number of unique lines", "num") info.AddOption(OPT_NO_PROGRESS, "Disable progress output") @@ -436,7 +435,11 @@ func genUsage() *usage.Info { 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(APP+" -m 100 < file.txt", "Count unique lines in stdin data with 100 uniq lines max") + + info.AddRawExample( + APP+" -m 100 < file.txt", + "Count unique lines in stdin data with 100 uniq lines max", + ) return info } @@ -444,11 +447,16 @@ func genUsage() *usage.Info { // genAbout generates info about version func genAbout(gitRev string) *usage.About { about := &usage.About{ - App: APP, - Version: VER, - Desc: DESC, - Year: 2009, - Owner: "ESSENTIAL KAOS", + App: APP, + Version: VER, + Desc: DESC, + Year: 2009, + Owner: "ESSENTIAL KAOS", + + AppNameColorTag: colorTagApp, + VersionColorTag: colorTagVer, + DescSeparator: "{s}—{!}", + License: "Apache License, Version 2.0 ", BugTracker: "https://github.com/essentialkaos/uc", UpdateChecker: usage.UpdateChecker{"essentialkaos/uc", update.GitHubChecker}, diff --git a/cli/support/support.go b/cli/support/support.go index c5d80fd..74070a5 100644 --- a/cli/support/support.go +++ b/cli/support/support.go @@ -11,14 +11,15 @@ import ( "fmt" "os" "runtime" + "runtime/debug" "strings" "github.com/essentialkaos/ek/v12/fmtc" "github.com/essentialkaos/ek/v12/fmtutil" - "github.com/essentialkaos/ek/v12/fsutil" "github.com/essentialkaos/ek/v12/hash" "github.com/essentialkaos/ek/v12/strutil" "github.com/essentialkaos/ek/v12/system" + "github.com/essentialkaos/ek/v12/system/container" "github.com/essentialkaos/depsy" ) @@ -55,6 +56,10 @@ func showApplicationInfo(app, ver, gitRev string) { runtime.GOOS, runtime.GOARCH, )) + if gitRev == "" { + gitRev = extractGitRevFromBuildInfo() + } + if gitRev != "" { if !fmtc.DisableColors && fmtc.IsTrueColorSupported() { printInfo(7, "Git SHA", gitRev+getHashColorBullet(gitRev)) @@ -83,13 +88,14 @@ func showOSInfo() { if err == nil { fmtutil.Separator(false, "OS INFO") - printInfo(12, "Name", osInfo.Name) - printInfo(12, "Pretty Name", osInfo.PrettyName) - printInfo(12, "Version", osInfo.VersionID) + printInfo(12, "Name", osInfo.ColoredName()) + printInfo(12, "Pretty Name", osInfo.ColoredPrettyName()) + printInfo(12, "Version", osInfo.Version) printInfo(12, "ID", osInfo.ID) printInfo(12, "ID Like", osInfo.IDLike) printInfo(12, "Version ID", osInfo.VersionID) printInfo(12, "Version Code", osInfo.VersionCodename) + printInfo(12, "Platform ID", osInfo.PlatformID) printInfo(12, "CPE", osInfo.CPEName) } @@ -97,11 +103,9 @@ func showOSInfo() { if err != nil { return - } else { - if osInfo == nil { - fmtutil.Separator(false, "SYSTEM INFO") - printInfo(12, "Name", systemInfo.OS) - } + } else if osInfo == nil { + fmtutil.Separator(false, "SYSTEM INFO") + printInfo(12, "Name", systemInfo.OS) } printInfo(12, "Arch", systemInfo.Arch) @@ -109,11 +113,13 @@ func showOSInfo() { containerEngine := "No" - switch { - case fsutil.IsExist("/.dockerenv"): + switch container.GetEngine() { + case container.DOCKER: containerEngine = "Yes (Docker)" - case fsutil.IsExist("/run/.containerenv"): + case container.PODMAN: containerEngine = "Yes (Podman)" + case container.LXC: + containerEngine = "Yes (LXC)" } fmtc.NewLine() @@ -140,6 +146,23 @@ func showDepsInfo(gomod []byte) { } } +// extractGitRevFromBuildInfo extracts git SHA from embedded build info +func extractGitRevFromBuildInfo() string { + info, ok := debug.ReadBuildInfo() + + if !ok { + return "" + } + + for _, s := range info.Settings { + if s.Key == "vcs.revision" && len(s.Value) > 7 { + return s.Value[:7] + } + } + + return "" +} + // getHashColorBullet return bullet with color from hash func getHashColorBullet(v string) string { if len(v) > 6 { @@ -151,7 +174,7 @@ func getHashColorBullet(v string) string { // printInfo formats and prints info record func printInfo(size int, name, value string) { - name = name + ":" + name += ":" size++ if value == "" { diff --git a/common/uc.spec b/common/uc.spec index 88209b3..8d2ec89 100644 --- a/common/uc.spec +++ b/common/uc.spec @@ -6,7 +6,7 @@ Summary: Simple utility for counting unique lines Name: uc -Version: 2.0.0 +Version: 2.0.1 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -65,6 +65,10 @@ rm -rf %{buildroot} ################################################################################ %changelog +* Tue Dec 19 2023 Anton Novojilov - 2.0.1-0 +- Dependencies update +- Code refactoring + * Wed May 03 2023 Anton Novojilov - 2.0.0-0 - Better standard input handling diff --git a/go.mod b/go.mod index 3ce7606..a6c425a 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/essentialkaos/depsy v1.1.0 - github.com/essentialkaos/ek/v12 v12.67.0 + github.com/essentialkaos/ek/v12 v12.92.0 ) -require golang.org/x/sys v0.8.0 // indirect +require golang.org/x/sys v0.15.0 // indirect diff --git a/go.sum b/go.sum index 93bd658..70b6add 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.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/essentialkaos/ek/v12 v12.92.0 h1:3JIkHWNA6MNkJOfqzMWJ8jN9sRM7nRi7URoFRVFHZzI= +github.com/essentialkaos/ek/v12 v12.92.0/go.mod h1:9efMqo1S8EtYhmeelOSTmMQDGC2vRgPkjkKKfvUD2eU= 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.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= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=