Skip to content

Commit

Permalink
Merge pull request #5 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 0.0.3
  • Loading branch information
andyone authored May 16, 2023
2 parents 3d428ef + f4f6db7 commit 8c878ca
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 27 deletions.
30 changes: 30 additions & 0 deletions .bibop/path.recipe
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ command "path match /home/user/*/file.txt /home/user/bob/file.txt /home/user/bob

################################################################################

command "path -s add-prefix /home/user/ john bob jack"
exit 0
output-contains "/home/user/john /home/user/bob /home/user/jack"

################################################################################

command "path -s del-prefix /home/user/ /home/user/john /home/user/bob /home/user/jack"
exit 0
output-contains "john bob jack"

################################################################################

command "path -s add-suffix .jpg john bob jack"
exit 0
output-contains "john.jpg bob.jpg jack.jpg"

################################################################################

command "path -s del-suffix .jpg john.jpg bob.jpg jack.jpg"
exit 0
output-contains "john bob jack"

################################################################################

command "path -s exclude _small john_small.jpg bob_small.jpg jack_small.jpg"
exit 0
output-contains "john.jpg bob.jpg jack.jpg"

################################################################################

command "path is-abs /home/user/bob/file.txt" "Check is-abs command"
exit 0

Expand Down
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,24 @@ 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
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 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
Options
Expand Down
28 changes: 27 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// Basic utility info
const (
APP = "path"
VER = "0.0.2"
VER = "0.0.3"
DESC = "Dead simple tool for working with paths"
)

Expand Down Expand Up @@ -62,6 +62,13 @@ const (
CMD_ABS = "abs"
CMD_MATCH = "match"
CMD_JOIN = "join"

CMD_ADD_PREFIX = "add-prefix"
CMD_DEL_PREFIX = "del-prefix"
CMD_ADD_SUFFIX = "add-suffix"
CMD_DEL_SUFFIX = "del-suffix"
CMD_EXCLUDE = "exclude"

CMD_IS_ABS = "is-abs"
CMD_IS_LOCAL = "is-local"
CMD_IS_SAFE = "is-safe"
Expand Down Expand Up @@ -186,6 +193,18 @@ func process(args options.Arguments) (error, bool) {
return cmdMatch(cmdArgs)
case CMD_JOIN:
return cmdJoin(cmdArgs)

case CMD_ADD_PREFIX:
return cmdAddPrefix(cmdArgs)
case CMD_DEL_PREFIX:
return cmdDelPrefix(cmdArgs)
case CMD_ADD_SUFFIX:
return cmdAddSuffix(cmdArgs)
case CMD_DEL_SUFFIX:
return cmdDelSuffix(cmdArgs)
case CMD_EXCLUDE:
return cmdExclude(cmdArgs)

case CMD_IS_ABS:
return cmdIsAbs(cmdArgs)
case CMD_IS_LOCAL:
Expand Down Expand Up @@ -255,6 +274,13 @@ func genUsage() *usage.Info {
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")
Expand Down
164 changes: 152 additions & 12 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,158 @@ func cmdJoin(args options.Arguments) (error, bool) {
return nil, true
}

// cmdAddPrefix is handler for "add-prefix" command
func cmdAddPrefix(args options.Arguments) (error, bool) {
var result []string

input, err := getInputData(args)

if err != nil {
return err, false
}

if len(input) < 2 {
printError("Not enough arguments")
return nil, false
}

prefix := input[0]

for _, item := range input[1:] {
result = append(result, prefix+item)
}

if len(result) == 0 {
return err, false
}

fmt.Println(strings.Join(result, getSeparator()))

return nil, true
}

// cmdDelPrefix is handler for "del-prefix" command
func cmdDelPrefix(args options.Arguments) (error, bool) {
var result []string

input, err := getInputData(args)

if err != nil {
return err, false
}

if len(input) < 2 {
printError("Not enough arguments")
return nil, false
}

prefix := input[0]

for _, item := range input[1:] {
data, _ := strings.CutPrefix(item, prefix)
result = append(result, data)
}

if len(result) == 0 {
return err, false
}

fmt.Println(strings.Join(result, getSeparator()))

return nil, true
}

// cmdAddSuffix is handler for "add-suffix" command
func cmdAddSuffix(args options.Arguments) (error, bool) {
var result []string

input, err := getInputData(args)

if err != nil {
return err, false
}

if len(input) < 2 {
printError("Not enough arguments")
return nil, false
}

suffix := input[0]

for _, item := range input[1:] {
result = append(result, item+suffix)
}

if len(result) == 0 {
return err, false
}

fmt.Println(strings.Join(result, getSeparator()))

return nil, true
}

// cmdDelSuffix is handler for "del-suffix" command
func cmdDelSuffix(args options.Arguments) (error, bool) {
var result []string

input, err := getInputData(args)

if err != nil {
return err, false
}

if len(input) < 2 {
printError("Not enough arguments")
return nil, false
}

prefix := input[0]

for _, item := range input[1:] {
data, _ := strings.CutSuffix(item, prefix)
result = append(result, data)
}

if len(result) == 0 {
return err, false
}

fmt.Println(strings.Join(result, getSeparator()))

return nil, true
}

// cmdExclude is handler for "exclude" command
func cmdExclude(args options.Arguments) (error, bool) {
var result []string

input, err := getInputData(args)

if err != nil {
return err, false
}

if len(input) < 2 {
printError("Not enough arguments")
return nil, false
}

substr := input[0]

for _, item := range input[1:] {
result = append(result, strutil.Exclude(item, substr))
}

if len(result) == 0 {
return err, false
}

fmt.Println(strings.Join(result, getSeparator()))

return nil, true
}

// cmdIsAbs is handler for "is-abs" command
func cmdIsAbs(args options.Arguments) (error, bool) {
input, err := getInputData(args)
Expand Down Expand Up @@ -353,18 +505,6 @@ func getInputData(args options.Arguments) ([]string, error) {
return result, nil
}

// printSeparator prints data separator
func printSeparator() {
switch {
case options.GetB(OPT_SPACE):
fmt.Printf(" ")
case options.GetB(OPT_ZERO):
fmt.Printf("\x00")
default:
fmt.Println()
}
}

// getSeparator returns data separator
func getSeparator() string {
switch {
Expand Down
9 changes: 8 additions & 1 deletion 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.2
Version: 0.0.3
Release: 0%{?dist}
Group: Applications/System
License: Apache License, Version 2.0
Expand Down Expand Up @@ -101,6 +101,13 @@ fi
################################################################################

%changelog
* Tue May 16 2023 Anton Novojilov <[email protected]> - 0.0.3-0
- Added add-prefix command
- Added remove-prefix command
- Added add-suffix command
- Added remove-suffix command
- Added exclude command

* Mon May 15 2023 Anton Novojilov <[email protected]> - 0.0.2-0
- Added join command
- Custom version info formats support
Expand Down

0 comments on commit 8c878ca

Please sign in to comment.