Skip to content

Commit

Permalink
#12
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Mar 20, 2019
1 parent 52b6d64 commit 6cc7f90
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions brename.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

var log *logging.Logger

var version = "2.7.1"
var version = "2.7.2"
var app = "brename"

// for detecting one case where two or more files are renamed to same new path
Expand Down Expand Up @@ -175,6 +175,8 @@ func getOptions(cmd *cobra.Command) *Options {

var replaceWithKV bool
var kvs map[string]string
keepKey := getFlagBool(cmd, "keep-key")
keyMissRepl := getFlagString(cmd, "key-miss-repl")
if reKV.MatchString(replacement) {
replaceWithKV = true
if !regexp.MustCompile(`\(.+\)`).MatchString(pattern) {
Expand All @@ -183,6 +185,10 @@ func getOptions(cmd *cobra.Command) *Options {
if kvFile == "" {
checkError(fmt.Errorf(`since replacement symbol "{kv}"/"{KV}" found in value of flag -r/--replacement, tab-delimited key-value file should be given by flag -k/--kv-file`))
}

if keepKey && keyMissRepl != "" {
log.Warning("flag -m/--key-miss-repl ignored when flag -K/--keep-key given")
}
log.Infof("read key-value file: %s", kvFile)
kvs, err = readKVs(kvFile, ignoreCase)
if err != nil {
Expand Down Expand Up @@ -235,9 +241,9 @@ func getOptions(cmd *cobra.Command) *Options {

KVs: kvs,
KVFile: kvFile,
KeepKey: getFlagBool(cmd, "keep-key"),
KeepKey: keepKey,
KeyCaptIdx: getFlagPositiveInt(cmd, "key-capt-idx"),
KeyMissRepl: getFlagString(cmd, "key-miss-repl"),
KeyMissRepl: keyMissRepl,

OverwriteMode: overwriteMode,

Expand Down Expand Up @@ -732,8 +738,10 @@ func checkOperation(opt *Options, path string) (bool, operation) {
r = reKV.ReplaceAllString(r, opt.KVs[k])
} else if opt.KeepKey {
r = reKV.ReplaceAllString(r, found[opt.KeyCaptIdx])
} else {
} else if opt.KeyMissRepl != "" {
r = reKV.ReplaceAllString(r, opt.KeyMissRepl)
} else {
return false, operation{path, path, codeUnchanged}
}
}
}
Expand Down

0 comments on commit 6cc7f90

Please sign in to comment.