diff --git a/.bibop/path.recipe b/.bibop/path.recipe index 32e80ac..b40d498 100644 --- a/.bibop/path.recipe +++ b/.bibop/path.recipe @@ -37,6 +37,12 @@ command "path dirname /home/user/john/file.txt" "Check dirname command" ################################################################################ +command "path dirn 2 /home/user/john/file.txt" "Check dir command" + exit 0 + output-contains "/home/user" + +################################################################################ + command "ln -sf path.recipe recipe.link" "Create link for the test" exit 0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 51c4777..967b030 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: strategy: matrix: - go: [ '1.20.x', '1.21.x' ] + go: [ '1.21.x', '1.22.x' ] steps: - name: Checkout diff --git a/README.md b/README.md index 42b2a33..87e1f73 100644 --- a/README.md +++ b/README.md @@ -36,14 +36,24 @@ find . -mindepth 1 -maxdepth 1 -type d | path basename Also, it works MUCH faster: -```bash -time find . -iname '*.go' -print0 | xargs -0 -n1 -- basename -# find . -iname '*.go' -print0 0.07s user 0.12s system 4% cpu 4.255 total -# xargs -0 -n1 -- basename 2.59s user 2.74s system 102% cpu 5.195 total +``` +$ git clone https://github.com/kubernetes/kubernetes.git --depth=1 + +$ cd kubernetes + +$ hyperfine 'find . -iname *.go -print0 | xargs -0 -n1 -- basename' 'find . -iname *.go | path basename' + +Benchmark 1: find . -iname *.go -print0 | xargs -0 -n1 -- basename + Time (mean ± σ): 12.621 s ± 0.077 s [User: 5.871 s, System: 7.043 s] + Range (min … max): 12.512 s … 12.745 s 10 runs + +Benchmark 2: find . -iname *.go | path basename + Time (mean ± σ): 106.5 ms ± 1.5 ms [User: 59.8 ms, System: 60.4 ms] + Range (min … max): 104.1 ms … 111.1 ms 28 runs -time find . -iname '*.go' | path basename -# find . -iname '*.go' 0.08s user 0.09s system 99% cpu 0.174 total -# path basename 0.02s user 0.02s system 21% cpu 0.207 total +Summary + find . -iname *.go | path basename ran + 118.45 ± 1.80 times faster than find . -iname *.go -print0 | xargs -0 -n1 -- basename ``` ### Installation @@ -105,24 +115,25 @@ Usage: path {options} {command} Commands - base Strip directory and suffix from filenames - dir Strip last component from file name - link Print resolved symbolic links or canonical file names - clean Print shortest path name equivalent to path by purely lexical processing - compact Converts path to compact representation - abs Print absolute representation of path - ext Print file extension - match pattern Filter given path using pattern - join root Join path elements - add-prefix prefix Add the substring at the beginning - del-prefix prefix Remove the substring at the beginning - add-suffix suffix Add the substring at the end - del-suffix suffix Remove the substring at the end - exclude substr Exclude part of the string - is-abs Check if given path is absolute - is-local Check if given path is local - is-safe Check if given path is safe - is-match pattern Check if given path is match to pattern + base path Strip directory and suffix from filenames + dir path Strip last component from file name + dirn num path Return N elements from path + link path Print resolved symbolic links or canonical file names + clean path Print shortest path name equivalent to path by purely lexical processing + compact path Converts path to compact representation + abs path Print absolute representation of path + ext path Print file extension + match pattern path Filter given path using pattern + join root path Join path elements + add-prefix prefix path Add the substring at the beginning + del-prefix prefix path Remove the substring at the beginning + add-suffix suffix path Add the substring at the end + del-suffix suffix path Remove the substring at the end + exclude substr path Exclude part of the string + is-abs path Check if given path is absolute + is-local path Check if given path is local + is-safe path Check if given path is safe + is-match pattern path Check if given path is match to pattern Options diff --git a/cli/cli.go b/cli/cli.go index 7e58303..d9773a0 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -2,7 +2,7 @@ package app // ////////////////////////////////////////////////////////////////////////////////// // // // -// Copyright (c) 2023 ESSENTIAL KAOS // +// Copyright (c) 2024 ESSENTIAL KAOS // // Apache License, Version 2.0 // // // // ////////////////////////////////////////////////////////////////////////////////// // @@ -30,7 +30,7 @@ import ( // Basic utility info const ( APP = "path" - VER = "0.0.6" + VER = "1.0.0" DESC = "Dead simple tool for working with paths" ) @@ -53,15 +53,16 @@ const ( // ////////////////////////////////////////////////////////////////////////////////// // const ( - CMD_BASENAME = "base" - CMD_DIRNAME = "dir" - CMD_READLINK = "link" - CMD_CLEAN = "clean" - CMD_COMPACT = "compact" - CMD_EXT = "ext" - CMD_ABS = "abs" - CMD_MATCH = "match" - CMD_JOIN = "join" + CMD_BASENAME = "base" + CMD_DIRNAME = "dir" + CMD_DIRNAME_NUM = "dirn" + CMD_READLINK = "link" + CMD_CLEAN = "clean" + CMD_COMPACT = "compact" + CMD_EXT = "ext" + CMD_ABS = "abs" + CMD_MATCH = "match" + CMD_JOIN = "join" CMD_ADD_PREFIX = "add-prefix" CMD_DEL_PREFIX = "del-prefix" @@ -167,6 +168,8 @@ func process(args options.Arguments) (error, bool) { return cmdBasename(cmdArgs) case CMD_DIRNAME, "dirname": return cmdDirname(cmdArgs) + case CMD_DIRNAME_NUM: + return cmdDirnameNum(cmdArgs) case CMD_READLINK, "readlink": return cmdReadlink(cmdArgs) case CMD_CLEAN: @@ -255,6 +258,7 @@ func genUsage() *usage.Info { 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_DIRNAME_NUM, "Return N elements from path", "num", "?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") diff --git a/cli/commands.go b/cli/commands.go index 03fbf4c..d17aa7c 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -2,7 +2,7 @@ package app // ////////////////////////////////////////////////////////////////////////////////// // // // -// Copyright (c) 2023 ESSENTIAL KAOS // +// Copyright (c) 2024 ESSENTIAL KAOS // // Apache License, Version 2.0 // // // // ////////////////////////////////////////////////////////////////////////////////// // @@ -12,6 +12,7 @@ import ( "io" "os" "path/filepath" + "strconv" "strings" "github.com/essentialkaos/ek/v12/fsutil" @@ -68,6 +69,35 @@ func cmdDirname(args options.Arguments) (error, bool) { return nil, true } +// cmdDirnameNum is handler for "dirn" command +func cmdDirnameNum(args options.Arguments) (error, bool) { + var result []string + + input, err := getInputData(args) + + if err != nil { + return err, false + } + + num, err := strconv.Atoi(input[0]) + + if err != nil { + return err, false + } + + for _, item := range input[1:] { + result = append(result, path.DirN(item, num)) + } + + if len(result) == 0 { + return err, false + } + + fmt.Println(strings.Join(result, getSeparator())) + + return nil, true +} + // cmdReadlink is handler for "link" command func cmdReadlink(args options.Arguments) (error, bool) { var result []string diff --git a/cli/support/support.go b/cli/support/support.go index 74070a5..588ea54 100644 --- a/cli/support/support.go +++ b/cli/support/support.go @@ -2,7 +2,7 @@ package support // ////////////////////////////////////////////////////////////////////////////////// // // // -// Copyright (c) 2023 ESSENTIAL KAOS // +// Copyright (c) 2024 ESSENTIAL KAOS // // Apache License, Version 2.0 // // // // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/common/path.spec b/common/path.spec index fbab525..359d558 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.6 +Version: 1.0.0 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -22,7 +22,7 @@ Source100: checksum.sha512 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) -BuildRequires: golang >= 1.20 +BuildRequires: golang >= 1.21 Provides: %{name} = %{version}-%{release} @@ -101,6 +101,11 @@ fi ################################################################################ %changelog +* Sun Feb 18 2024 Anton Novojilov - 1.0.0-0 +- Added dirn command +- Code refactoring +- Dependencies update + * Sun Dec 17 2023 Anton Novojilov - 0.0.6-0 - Improved verbose version info output - Code refactoring diff --git a/go.mod b/go.mod index 8c07e99..16bd938 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/essentialkaos/path -go 1.19 +go 1.18 require ( github.com/essentialkaos/depsy v1.1.0 - github.com/essentialkaos/ek/v12 v12.92.0 + github.com/essentialkaos/ek/v12 v12.104.0 ) -require golang.org/x/sys v0.15.0 // indirect +require golang.org/x/sys v0.18.0 // indirect diff --git a/go.sum b/go.sum index 70b6add..39a4f76 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.92.0 h1:3JIkHWNA6MNkJOfqzMWJ8jN9sRM7nRi7URoFRVFHZzI= -github.com/essentialkaos/ek/v12 v12.92.0/go.mod h1:9efMqo1S8EtYhmeelOSTmMQDGC2vRgPkjkKKfvUD2eU= +github.com/essentialkaos/ek/v12 v12.104.0 h1:srNIOipfcfqwWoiZKjIuRhbvEBt0UyP5ni13bimU1Iw= +github.com/essentialkaos/ek/v12 v12.104.0/go.mod h1:exBTL3OE3dm4vjHihE4ZhQ3onJq7C8q2r+OTZmpCO6s= 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.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= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/path.go b/path.go index 1d7c9c3..ee85ebf 100644 --- a/path.go +++ b/path.go @@ -2,7 +2,7 @@ package main // ////////////////////////////////////////////////////////////////////////////////// // // // -// Copyright (c) 2023 ESSENTIAL KAOS // +// Copyright (c) 2024 ESSENTIAL KAOS // // Apache License, Version 2.0 // // // // ////////////////////////////////////////////////////////////////////////////////// //