diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 068b454..51c4777 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,14 +29,14 @@ jobs: strategy: matrix: - go: [ '1.20.x' ] + go: [ '1.20.x', '1.21.x' ] 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 }} @@ -46,14 +46,6 @@ jobs: - name: Run tests run: make all - - name: Upload built artifacts - uses: actions/upload-artifact@v3 - with: - name: path - path: ${{github.workspace}}/path - if-no-files-found: error - retention-days: 1 - Bibop: name: Bibop runs-on: ubuntu-latest @@ -62,16 +54,18 @@ jobs: steps: - name: Code checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - - name: Download pre-built artifacts - uses: actions/download-artifact@v3 + - name: Set up Go + uses: actions/setup-go@v5 with: - name: path - path: ${{github.workspace}} + go-version: '1.21.x' + + - name: Download dependencies + run: make deps - - name: Restore permissions - run: chmod +x path + - name: Run tests + run: make all - name: Run bibop tests uses: essentialkaos/bibop-action@v1 @@ -87,10 +81,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 }} @@ -109,7 +103,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 8f59233..54b975e 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 da20922..42b2a33 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,10 @@ To build the `path` from scratch, make sure you have a working Go 1.20+ workspac go install github.com/essentialkaos/path@latest ``` -#### From [ESSENTIAL KAOS Public Repository](https://yum.kaos.st) for EL 7/8/9 +#### From [ESSENTIAL KAOS Public Repository](https://kaos.sh/kaos-repo) for EL 7/8/9 ```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 path ``` diff --git a/cli/cli.go b/cli/cli.go index 2ab618e..7e58303 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -14,8 +14,8 @@ import ( "strings" "github.com/essentialkaos/ek/v12/fmtc" - "github.com/essentialkaos/ek/v12/fsutil" "github.com/essentialkaos/ek/v12/options" + "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" @@ -30,7 +30,7 @@ import ( // Basic utility info const ( APP = "path" - VER = "0.0.5" + VER = "0.0.6" DESC = "Dead simple tool for working with paths" ) @@ -143,24 +143,7 @@ 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") == "" { - fmtc.DisableColors = true - } - - if os.Getenv("NO_COLOR") != "" { + if !tty.IsTTY() { fmtc.DisableColors = true } } @@ -244,11 +227,11 @@ func printCompletion() int { switch options.GetS(OPT_COMPLETION) { case "bash": - fmt.Printf(bash.Generate(info, "path")) + fmt.Print(bash.Generate(info, "path")) case "fish": - fmt.Printf(fish.Generate(info, "path")) + fmt.Print(fish.Generate(info, "path")) case "zsh": - fmt.Printf(zsh.Generate(info, optMap, "path")) + fmt.Print(zsh.Generate(info, optMap, "path")) default: return 1 } @@ -270,26 +253,26 @@ func printMan() { func genUsage() *usage.Info { info := usage.NewInfo() - info.AddCommand(CMD_BASENAME, "Strip directory and suffix from filenames") - info.AddCommand(CMD_DIRNAME, "Strip last component from file name") - info.AddCommand(CMD_READLINK, "Print resolved symbolic links or canonical file names") - info.AddCommand(CMD_CLEAN, "Print shortest path name equivalent to path by purely lexical processing") - info.AddCommand(CMD_COMPACT, "Converts path to compact representation") - info.AddCommand(CMD_ABS, "Print absolute representation of path") - info.AddCommand(CMD_EXT, "Print file extension") - info.AddCommand(CMD_MATCH, "Filter given path using pattern", "pattern") - info.AddCommand(CMD_JOIN, "Join path elements", "root") - - info.AddCommand(CMD_ADD_PREFIX, "Add the substring at the beginning", "prefix") - info.AddCommand(CMD_DEL_PREFIX, "Remove the substring at the beginning", "prefix") - info.AddCommand(CMD_ADD_SUFFIX, "Add the substring at the end", "suffix") - info.AddCommand(CMD_DEL_SUFFIX, "Remove the substring at the end", "suffix") - info.AddCommand(CMD_EXCLUDE, "Exclude part of the string", "substr") - - info.AddCommand(CMD_IS_ABS, "Check if given path is absolute") - info.AddCommand(CMD_IS_LOCAL, "Check if given path is local") - info.AddCommand(CMD_IS_SAFE, "Check if given path is safe") - info.AddCommand(CMD_IS_MATCH, "Check if given path is match to pattern", "pattern") + info.AddCommand(CMD_BASENAME, "Strip directory and suffix from filenames", "?path") + info.AddCommand(CMD_DIRNAME, "Strip last component from file name", "?path") + info.AddCommand(CMD_READLINK, "Print resolved symbolic links or canonical file names", "?path") + info.AddCommand(CMD_CLEAN, "Print shortest path name equivalent to path by purely lexical processing", "?path") + info.AddCommand(CMD_COMPACT, "Converts path to compact representation", "?path") + info.AddCommand(CMD_ABS, "Print absolute representation of path", "?path") + info.AddCommand(CMD_EXT, "Print file extension", "?path") + info.AddCommand(CMD_MATCH, "Filter given path using pattern", "pattern", "?path") + info.AddCommand(CMD_JOIN, "Join path elements", "root", "?path") + + info.AddCommand(CMD_ADD_PREFIX, "Add the substring at the beginning", "prefix", "?path") + info.AddCommand(CMD_DEL_PREFIX, "Remove the substring at the beginning", "prefix", "?path") + info.AddCommand(CMD_ADD_SUFFIX, "Add the substring at the end", "suffix", "?path") + info.AddCommand(CMD_DEL_SUFFIX, "Remove the substring at the end", "suffix", "?path") + info.AddCommand(CMD_EXCLUDE, "Exclude part of the string", "substr", "?path") + + info.AddCommand(CMD_IS_ABS, "Check if given path is absolute", "?path") + info.AddCommand(CMD_IS_LOCAL, "Check if given path is local", "?path") + info.AddCommand(CMD_IS_SAFE, "Check if given path is safe", "?path") + info.AddCommand(CMD_IS_MATCH, "Check if given path is match to pattern", "pattern", "?path") info.AddOption(OPT_ZERO, "End each output line with NUL, not newline") info.AddOption(OPT_SPACE, "End each output line with space, not newline") diff --git a/cli/support/support.go b/cli/support/support.go index 9374af0..74070a5 100644 --- a/cli/support/support.go +++ b/cli/support/support.go @@ -11,34 +11,24 @@ import ( "fmt" "os" "runtime" + "runtime/debug" "strings" "github.com/essentialkaos/ek/v12/fmtc" "github.com/essentialkaos/ek/v12/fmtutil" "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" ) // ////////////////////////////////////////////////////////////////////////////////// // -// Pkg contains basic package info -type Pkg struct { - Name string - Version string -} - -// Pkgs is slice with packages -type Pkgs []Pkg - -// ////////////////////////////////////////////////////////////////////////////////// // - // Print prints verbose info about application, system, dependencies and // important environment func Print(app, ver, gitRev string, gomod []byte) { - pkgs := collectEnvInfo() - fmtutil.SeparatorTitleColorTag = "{s-}" fmtutil.SeparatorFullscreen = false fmtutil.SeparatorColorTag = "{s-}" @@ -46,7 +36,6 @@ func Print(app, ver, gitRev string, gomod []byte) { showApplicationInfo(app, ver, gitRev) showOSInfo() - showEnvInfo(pkgs) showDepsInfo(gomod) fmtutil.Separator(false) @@ -67,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)) @@ -88,6 +81,52 @@ func showApplicationInfo(app, ver, gitRev string) { } } +// showOSInfo shows verbose information about system +func showOSInfo() { + osInfo, err := system.GetOSInfo() + + if err == nil { + fmtutil.Separator(false, "OS INFO") + + 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) + } + + systemInfo, err := system.GetSystemInfo() + + if err != nil { + return + } else if osInfo == nil { + fmtutil.Separator(false, "SYSTEM INFO") + printInfo(12, "Name", systemInfo.OS) + } + + printInfo(12, "Arch", systemInfo.Arch) + printInfo(12, "Kernel", systemInfo.Kernel) + + containerEngine := "No" + + switch container.GetEngine() { + case container.DOCKER: + containerEngine = "Yes (Docker)" + case container.PODMAN: + containerEngine = "Yes (Podman)" + case container.LXC: + containerEngine = "Yes (LXC)" + } + + fmtc.NewLine() + + printInfo(12, "Container", containerEngine) +} + // showDepsInfo shows information about all dependencies func showDepsInfo(gomod []byte) { deps := depsy.Extract(gomod, false) @@ -107,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 { @@ -118,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 == "" { @@ -131,16 +187,3 @@ func printInfo(size int, name, value string) { } // ////////////////////////////////////////////////////////////////////////////////// // - -// getMaxSize returns max package name size -func (p Pkgs) getMaxSize() int { - size := 0 - - for _, pkg := range p { - if len(pkg.Name) > size { - size = len(pkg.Name) - } - } - - return size -} diff --git a/cli/support/support_darwin.go b/cli/support/support_darwin.go deleted file mode 100644 index 8d1c69b..0000000 --- a/cli/support/support_darwin.go +++ /dev/null @@ -1,50 +0,0 @@ -package support - -// ////////////////////////////////////////////////////////////////////////////////// // -// // -// Copyright (c) 2023 ESSENTIAL KAOS // -// Apache License, Version 2.0 // -// // -// ////////////////////////////////////////////////////////////////////////////////// // - -import ( - "github.com/essentialkaos/ek/v12/fmtutil" - "github.com/essentialkaos/ek/v12/system" -) - -// ////////////////////////////////////////////////////////////////////////////////// // - -// showOSInfo shows verbose information about system -func showOSInfo() { - systemInfo, err := system.GetSystemInfo() - - if err != nil { - return - } - - osInfo, err := system.GetOSInfo() - - if err == nil { - fmtutil.Separator(false, "OS INFO") - - printInfo(12, "Name", osInfo.Name) - printInfo(12, "Version", osInfo.VersionID) - printInfo(12, "Build", osInfo.Build) - } - - fmtutil.Separator(false, "SYSTEM INFO") - - printInfo(7, "Name", systemInfo.OS) - printInfo(7, "Arch", systemInfo.Arch) - printInfo(7, "Kernel", systemInfo.Kernel) -} - -// collectEnvInfo collects info about environment -func collectEnvInfo() Pkgs { - return nil -} - -// showEnvInfo shows info about environment -func showEnvInfo(pkgs Pkgs) { - return -} diff --git a/cli/support/support_linux.go b/cli/support/support_linux.go deleted file mode 100644 index a3172a7..0000000 --- a/cli/support/support_linux.go +++ /dev/null @@ -1,144 +0,0 @@ -package support - -// ////////////////////////////////////////////////////////////////////////////////// // -// // -// Copyright (c) 2023 ESSENTIAL KAOS // -// Apache License, Version 2.0 // -// // -// ////////////////////////////////////////////////////////////////////////////////// // - -import ( - "os/exec" - "strings" - - "github.com/essentialkaos/ek/v12/fmtc" - "github.com/essentialkaos/ek/v12/fmtutil" - "github.com/essentialkaos/ek/v12/fsutil" - "github.com/essentialkaos/ek/v12/system" - "github.com/essentialkaos/ek/v12/system/container" -) - -// ////////////////////////////////////////////////////////////////////////////////// // - -// showOSInfo shows verbose information about system -func showOSInfo() { - osInfo, err := system.GetOSInfo() - - 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, "ID", osInfo.ID) - printInfo(12, "ID Like", osInfo.IDLike) - printInfo(12, "Version ID", osInfo.VersionID) - printInfo(12, "Version Code", osInfo.VersionCodename) - printInfo(12, "CPE", osInfo.CPEName) - } - - systemInfo, err := system.GetSystemInfo() - - if err != nil { - return - } else { - if osInfo == nil { - fmtutil.Separator(false, "SYSTEM INFO") - printInfo(12, "Name", systemInfo.OS) - } - } - - printInfo(12, "Arch", systemInfo.Arch) - printInfo(12, "Kernel", systemInfo.Kernel) - - containerEngine := "No" - - switch container.GetEngine() { - case container.DOCKER: - containerEngine = "Yes (Docker)" - case container.PODMAN: - containerEngine = "Yes (Podman)" - case container.LXC: - containerEngine = "Yes (LXC)" - } - - fmtc.NewLine() - - printInfo(12, "Container", containerEngine) -} - -// showEnvInfo shows info about environment -func showEnvInfo(pkgs Pkgs) { - fmtutil.Separator(false, "ENVIRONMENT") - - size := pkgs.getMaxSize() - - for _, pkg := range pkgs { - printInfo(size, pkg.Name, pkg.Version) - } -} - -// collectEnvInfo collects info about packages -func collectEnvInfo() Pkgs { - return Pkgs{ - getPackageInfo("rpm"), - } -} - -// getPackageVersion returns package name and version from package manager database -func getPackageInfo(names ...string) Pkg { - var info Pkg - - if len(names) == 0 { - return Pkg{} - } - - for _, name := range names { - switch { - case isDEBBased(): - info = getDEBPackageInfo(name) - case isRPMBased(): - info = getRPMPackageInfo(name) - } - - if info.Version != "" { - return info - } - } - - return Pkg{names[0], ""} -} - -// isDEBBased returns true if is DEB-based distro -func isRPMBased() bool { - return fsutil.IsExist("/usr/bin/rpm") -} - -// isDEBBased returns true if is DEB-based distro -func isDEBBased() bool { - return fsutil.IsExist("/usr/bin/dpkg-query") -} - -// getRPMPackageInfo returns info about RPM package -func getRPMPackageInfo(name string) Pkg { - cmd := exec.Command("rpm", "-q", name) - out, err := cmd.Output() - - if err != nil || len(out) == 0 { - return Pkg{name, ""} - } - - return Pkg{name, strings.TrimRight(string(out), "\n\r")} -} - -// getDEBPackageInfo returns info about DEB package -func getDEBPackageInfo(name string) Pkg { - cmd := exec.Command("dpkg-query", "--showformat=${Version}", "--show", name) - out, err := cmd.Output() - - if err != nil || len(out) == 0 { - return Pkg{name, ""} - } - - return Pkg{name, string(out)} -} diff --git a/common/path.spec b/common/path.spec index 271f8a4..fbab525 100644 --- a/common/path.spec +++ b/common/path.spec @@ -10,7 +10,7 @@ Summary: Dead simple tool for working with paths Name: path -Version: 0.0.5 +Version: 0.0.6 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -101,6 +101,11 @@ fi ################################################################################ %changelog +* Sun Dec 17 2023 Anton Novojilov - 0.0.6-0 +- Improved verbose version info output +- Code refactoring +- Dependencies update + * Tue May 23 2023 Anton Novojilov - 0.0.5-0 - Add support of enabling quiet mode using environment variable (PATH_QUIET) diff --git a/go.mod b/go.mod index 8d2481d..8c07e99 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/essentialkaos/depsy v1.1.0 - github.com/essentialkaos/ek/v12 v12.67.1 + 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 43ec542..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.1 h1:RSQxq0TaALADLVYhILky5nFQw8G1HeryUy8C6td35PA= -github.com/essentialkaos/ek/v12 v12.67.1/go.mod h1:SDR3JJJa8FySwRusH/h1gg5c6kpdF18aCkeK7eQSlic= +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=