Handle errors and clearing previous outputs - json output shouldn't have colors
This commit is contained in:
+13
-3
@@ -26,6 +26,7 @@ import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
@@ -78,7 +79,7 @@ type CopyMessage struct {
|
||||
// String string printer for copy message
|
||||
func (c CopyMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
return fmt.Sprintf("‘%s’ -> ‘%s’", c.Source, c.Target)
|
||||
return console.Colorize("Copy", fmt.Sprintf("‘%s’ -> ‘%s’", c.Source, c.Target))
|
||||
}
|
||||
copyMessageBytes, err := json.Marshal(c)
|
||||
fatalIf(probe.NewError(err), "Failed to marshal copy message.")
|
||||
@@ -115,11 +116,11 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue <-chan bool, wg *sync.WaitGro
|
||||
|
||||
var newReader io.ReadCloser
|
||||
if globalQuietFlag || globalJSONFlag {
|
||||
console.PrintC(CopyMessage{
|
||||
console.Println(CopyMessage{
|
||||
Source: cpURLs.SourceContent.Name,
|
||||
Target: cpURLs.TargetContent.Name,
|
||||
Length: cpURLs.SourceContent.Size,
|
||||
}.String() + "\n")
|
||||
}.String())
|
||||
newReader = reader
|
||||
} else {
|
||||
// set up progress
|
||||
@@ -177,6 +178,11 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) {
|
||||
break
|
||||
}
|
||||
if cpURLs.Error != nil {
|
||||
// Print in new line and adjust to top so that we don't print over the ongoing scan bar
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
console.Printf("%c[2K\n", 27)
|
||||
console.Printf("%c[A", 27)
|
||||
}
|
||||
errorIf(cpURLs.Error.Trace(), "Unable to prepare URLs for copying.")
|
||||
break
|
||||
}
|
||||
@@ -304,6 +310,10 @@ func doCopyCmdSession(session *sessionV2) {
|
||||
func mainCopy(ctx *cli.Context) {
|
||||
checkCopySyntax(ctx)
|
||||
|
||||
console.SetCustomTheme(map[string]*color.Color{
|
||||
"Copy": color.New(color.FgGreen, color.Bold),
|
||||
})
|
||||
|
||||
session := newSessionV2()
|
||||
|
||||
var e error
|
||||
|
||||
+30
-3
@@ -26,6 +26,7 @@ import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
@@ -65,6 +66,23 @@ EXAMPLES:
|
||||
`,
|
||||
}
|
||||
|
||||
// MirrorMessage container for file mirror messages
|
||||
type MirrorMessage struct {
|
||||
Source string `json:"source"`
|
||||
Targets []string `json:"targets"`
|
||||
}
|
||||
|
||||
// String string printer for mirror message
|
||||
func (s MirrorMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
return console.Colorize("Mirror", fmt.Sprintf("‘%s’ -> ‘%s’", s.Source, s.Targets))
|
||||
}
|
||||
mirrorMessageBytes, e := json.Marshal(s)
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
|
||||
return string(mirrorMessageBytes)
|
||||
}
|
||||
|
||||
// doMirror - Mirror an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any.
|
||||
func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
|
||||
defer wg.Done() // Notify that this copy routine is done.
|
||||
@@ -99,10 +117,10 @@ func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *syn
|
||||
|
||||
var newReader io.ReadCloser
|
||||
if globalQuietFlag || globalJSONFlag {
|
||||
console.PrintC(MirrorMessage{
|
||||
console.Println(MirrorMessage{
|
||||
Source: sURLs.SourceContent.Name,
|
||||
Targets: targetURLs,
|
||||
}.String() + "\n")
|
||||
}.String())
|
||||
newReader = reader
|
||||
} else {
|
||||
// set up progress
|
||||
@@ -156,7 +174,12 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) {
|
||||
break
|
||||
}
|
||||
if sURLs.Error != nil {
|
||||
errorIf(sURLs.Error.Trace(), "Unable to prepare mirror arguments.")
|
||||
// Print in new line and adjust to top so that we don't print over the ongoing scan bar
|
||||
if !globalQuietFlag && !globalJSONFlag {
|
||||
console.Printf("%c[2K\n", 27)
|
||||
console.Printf("%c[A", 27)
|
||||
}
|
||||
errorIf(sURLs.Error.Trace(), "Unable to prepare URLs for mirroring.")
|
||||
break
|
||||
}
|
||||
if sURLs.isEmpty() {
|
||||
@@ -285,6 +308,10 @@ func doMirrorCmdSession(session *sessionV2) {
|
||||
func mainMirror(ctx *cli.Context) {
|
||||
checkMirrorSyntax(ctx)
|
||||
|
||||
console.SetCustomTheme(map[string]*color.Color{
|
||||
"Mirror": color.New(color.FgGreen, color.Bold),
|
||||
})
|
||||
|
||||
var e error
|
||||
session := newSessionV2()
|
||||
session.Header.CommandType = "mirror"
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -45,23 +44,6 @@ func (m mirrorURLs) isEmpty() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// MirrorMessage container for file mirror messages
|
||||
type MirrorMessage struct {
|
||||
Source string `json:"source"`
|
||||
Targets []string `json:"targets"`
|
||||
}
|
||||
|
||||
// String string printer for mirror message
|
||||
func (s MirrorMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
return fmt.Sprintf("‘%s’ -> ‘%s’", s.Source, s.Targets)
|
||||
}
|
||||
mirrorMessageBytes, e := json.Marshal(s)
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
|
||||
return string(mirrorMessageBytes)
|
||||
}
|
||||
|
||||
//
|
||||
// * MIRROR ARGS - VALID CASES
|
||||
// =========================
|
||||
|
||||
Reference in New Issue
Block a user