Add force flag support to avoid over-writing destination
This commit is contained in:
@@ -147,6 +147,13 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) {
|
||||
console.Errorln(cpURLs.Error)
|
||||
break
|
||||
}
|
||||
if !globalForceFlag {
|
||||
if cpURLs.TargetContent.Size != 0 || !cpURLs.TargetContent.Time.IsZero() {
|
||||
console.Fatalf("Destination already exists, cannot overwrite ‘%s’ with ‘%s’. "+
|
||||
"Use ‘--force’ flag to override.\n", cpURLs.TargetContent.Name, cpURLs.SourceContent.Name)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(cpURLs)
|
||||
if err != nil {
|
||||
@@ -177,7 +184,6 @@ func doCopyCmdSession(session *sessionV2) {
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
cpQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1)))
|
||||
defer close(cpQueue)
|
||||
|
||||
scanner := bufio.NewScanner(session.NewDataReader())
|
||||
isCopied := isCopiedFactory(session.Header.LastCopied)
|
||||
|
||||
@@ -42,6 +42,11 @@ var (
|
||||
Usage: "Suppress chatty console output",
|
||||
}
|
||||
|
||||
forceFlag = cli.BoolFlag{
|
||||
Name: "force",
|
||||
Usage: "Force copying when destination exists",
|
||||
}
|
||||
|
||||
aliasFlag = cli.BoolFlag{
|
||||
Name: "alias",
|
||||
Usage: "Mimic operating system toolchain behavior wherever it makes sense",
|
||||
|
||||
+9
-1
@@ -147,6 +147,15 @@ func doPrepareSyncURLs(session *sessionV2, trapCh <-chan bool) {
|
||||
console.Errorln(sURLs.Error)
|
||||
break
|
||||
}
|
||||
if !globalForceFlag {
|
||||
if len(sURLs.TargetContents) > 0 {
|
||||
if sURLs.TargetContents[0].Size != 0 || !sURLs.TargetContents[0].Time.IsZero() {
|
||||
console.Fatalf("Destination already exists, cannot overwrite ‘%s’ with ‘%s’. "+
|
||||
"Use ‘--force’ flag to override.\n", sURLs.TargetContents[0].Name, sURLs.SourceContent.Name)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(sURLs)
|
||||
if err != nil {
|
||||
@@ -177,7 +186,6 @@ func doSyncCmdSession(session *sessionV2) {
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
syncQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1)))
|
||||
defer close(syncQueue)
|
||||
|
||||
scanner := bufio.NewScanner(session.NewDataReader())
|
||||
isCopied := isCopiedFactory(session.Header.LastCopied)
|
||||
|
||||
@@ -136,6 +136,8 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) <-chan copyURLs {
|
||||
copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
|
||||
return
|
||||
}
|
||||
copyURLsCh <- copyURLs{SourceContent: sourceContent, TargetContent: targetContent}
|
||||
return
|
||||
}
|
||||
// All OK.. We can proceed. Type A
|
||||
sourceContent.Name = sourceURL
|
||||
|
||||
@@ -21,6 +21,7 @@ import "github.com/minio/minio/pkg/iodine"
|
||||
|
||||
var (
|
||||
globalQuietFlag = false // Quiet flag set via command line
|
||||
globalForceFlag = false // Force flag set via command line
|
||||
globalAliasFlag = false // Alias flag set via command line
|
||||
globalJSONFlag = false // Json flag set via command line
|
||||
globalDebugFlag = false // Debug flag set via command line
|
||||
|
||||
@@ -105,6 +105,7 @@ func main() {
|
||||
// register all the flags
|
||||
registerFlag(configFlag) // path to config folder
|
||||
registerFlag(quietFlag) // suppress console output
|
||||
registerFlag(forceFlag) // force copying data
|
||||
registerFlag(aliasFlag) // OS toolchain mimic
|
||||
registerFlag(themeFlag) // console theme flag
|
||||
registerFlag(jsonFlag) // json formatted output
|
||||
@@ -123,6 +124,7 @@ func main() {
|
||||
}
|
||||
|
||||
globalQuietFlag = ctx.GlobalBool("quiet")
|
||||
globalForceFlag = ctx.GlobalBool("force")
|
||||
globalAliasFlag = ctx.GlobalBool("alias")
|
||||
globalDebugFlag = ctx.GlobalBool("debug")
|
||||
globalJSONFlag = ctx.GlobalBool("json")
|
||||
|
||||
@@ -100,6 +100,9 @@ func prepareSingleSyncURLsTypeA(sourceURL string, targetURL string) syncURLs {
|
||||
if !targetContent.Type.IsRegular() { // Target is not a regular file
|
||||
return syncURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))}
|
||||
}
|
||||
var targetContents []*client.Content
|
||||
targetContents = append(targetContents, targetContent)
|
||||
return syncURLs{SourceContent: sourceContent, TargetContents: targetContents}
|
||||
}
|
||||
// All OK.. We can proceed. Type A
|
||||
sourceContent.Name = sourceURL
|
||||
|
||||
Reference in New Issue
Block a user