Skip to content

Commit

Permalink
Merge pull request #49 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.0.0
  • Loading branch information
andyone authored Mar 17, 2024
2 parents 118de7e + 4b434b7 commit 969ba39
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 49 deletions.
6 changes: 6 additions & 0 deletions .bibop/path.recipe
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

strategy:
matrix:
go: [ '1.20.x', '1.21.x' ]
go: [ '1.21.x', '1.22.x' ]

steps:
- name: Checkout
Expand Down
61 changes: 36 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 15 additions & 11 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -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"
)

Expand All @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
32 changes: 31 additions & 1 deletion cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package app

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -12,6 +12,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/essentialkaos/ek/v12/fsutil"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cli/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package support

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
9 changes: 7 additions & 2 deletions common/path.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}

Expand Down Expand Up @@ -101,6 +101,11 @@ fi
################################################################################

%changelog
* Sun Feb 18 2024 Anton Novojilov <[email protected]> - 1.0.0-0
- Added dirn command
- Code refactoring
- Dependencies update

* Sun Dec 17 2023 Anton Novojilov <[email protected]> - 0.0.6-0
- Improved verbose version info output
- Code refactoring
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2023 ESSENTIAL KAOS //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down

0 comments on commit 969ba39

Please sign in to comment.