From 32bf4947c3f4f937d5faa95c96d5d84252f000b3 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 16 May 2023 12:31:22 +0300 Subject: [PATCH 1/2] Add more commands --- .bibop/path.recipe | 30 +++++++++ README.md | 31 +++++---- cli/cli.go | 28 +++++++- cli/commands.go | 164 +++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 227 insertions(+), 26 deletions(-) diff --git a/.bibop/path.recipe b/.bibop/path.recipe index ce78677..32e80ac 100644 --- a/.bibop/path.recipe +++ b/.bibop/path.recipe @@ -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 diff --git a/README.md b/README.md index a66a785..518749c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cli/cli.go b/cli/cli.go index 25ab568..b5a5d50 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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" ) @@ -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" @@ -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: @@ -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") diff --git a/cli/commands.go b/cli/commands.go index e544f6a..637c2c3 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -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) @@ -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 { From f4f6db7e88b99539961a5a6a85b37f22dc8153f6 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 16 May 2023 13:22:00 +0300 Subject: [PATCH 2/2] Update spec --- common/path.spec | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/common/path.spec b/common/path.spec index 13f87dc..845fd75 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.2 +Version: 0.0.3 Release: 0%{?dist} Group: Applications/System License: Apache License, Version 2.0 @@ -101,6 +101,13 @@ fi ################################################################################ %changelog +* Tue May 16 2023 Anton Novojilov - 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 - 0.0.2-0 - Added join command - Custom version info formats support