Skip to content

Commit

Permalink
Add 'strip-ext' command
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 8, 2024
1 parent 4f08e0c commit 148bb2f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// Basic utility info
const (
APP = "path"
VER = "1.0.3"
VER = "1.1.0"
DESC = "Dead simple tool for working with paths"
)

Expand Down Expand Up @@ -70,6 +70,7 @@ const (
CMD_ADD_SUFFIX = "add-suffix"
CMD_DEL_SUFFIX = "del-suffix"
CMD_EXCLUDE = "exclude"
CMD_STRIP_EXT = "strip-ext"

CMD_IS_ABS = "is-abs"
CMD_IS_LOCAL = "is-local"
Expand Down Expand Up @@ -200,6 +201,8 @@ func process(args options.Arguments) (error, bool) {
return cmdDelSuffix(cmdArgs)
case CMD_EXCLUDE:
return cmdExclude(cmdArgs)
case CMD_STRIP_EXT:
return cmdStripExt(cmdArgs)

case CMD_IS_ABS:
return cmdIsAbs(cmdArgs)
Expand Down Expand Up @@ -268,6 +271,7 @@ func genUsage() *usage.Info {
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_STRIP_EXT, "Remove file extension", "?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")
Expand Down
29 changes: 29 additions & 0 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,35 @@ func cmdExclude(args options.Arguments) (error, bool) {
return nil, true
}

// cmdStripExt is handler for "strip-ext" command
func cmdStripExt(args options.Arguments) (error, bool) {
var result []string

input, err := getInputData(args)

if err != nil {
return err, false
}

for _, item := range input {
ext := path.Ext(item)

if ext != "" {
result = append(result, strings.TrimSuffix(item, ext))
} else {
result = append(result, item)
}
}

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

0 comments on commit 148bb2f

Please sign in to comment.