1
0
mirror of https://github.com/minio/mc.git synced 2025-11-13 12:22:45 +03:00

Fixes for json review #1

This commit is contained in:
Harshavardhana
2015-08-23 01:24:04 -07:00
parent 34a0d64f97
commit b9c28d588b
6 changed files with 55 additions and 51 deletions

View File

@@ -88,6 +88,12 @@ func mainConfig(ctx *cli.Context) {
if strings.TrimSpace(ctx.Args().First()) == "" {
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code
}
// set new custom coloring
console.SetCustomTheme(map[string]*color.Color{
"Alias": color.New(color.FgCyan, color.Bold),
"URL": color.New(color.FgWhite),
})
arg := ctx.Args().First()
tailArgs := ctx.Args().Tail()
if len(tailArgs) > 2 {
@@ -106,11 +112,6 @@ func mainConfig(ctx *cli.Context) {
// convert interface{} back to its original struct
newConf := config.Data().(*configV2)
// set new custom coloring
console.SetCustomTheme(map[string]*color.Color{
"Alias": color.New(color.FgCyan, color.Bold),
"URL": color.New(color.FgWhite),
})
for k, v := range newConf.Aliases {
console.Println(AliasMessage{k, v})
}

View File

@@ -17,9 +17,9 @@
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/probe"
)
@@ -58,6 +58,10 @@ func mainDiff(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "diff", 1) // last argument is exit code
}
console.SetCustomTheme(map[string]*color.Color{
"Diff": color.New(color.FgGreen, color.Bold),
})
config := mustGetMcConfig()
firstArg := ctx.Args().First()
secondArg := ctx.Args()[1]
@@ -75,7 +79,7 @@ func mainDiff(ctx *cli.Context) {
newFirstURL := stripRecursiveURL(firstURL)
for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) {
fatalIf(diff.err.Trace(newFirstURL, secondURL), "Failed to diff "+firstURL+" and "+secondURL+".")
fmt.Println(diff.message)
console.Println(diff.message)
}
}

50
diff.go
View File

@@ -56,16 +56,16 @@ func (d DiffMessage) String() string {
if !globalJSONFlag {
msg := ""
switch d.Diff {
case "Only-in":
msg = "" + d.FirstURL + " only in " + d.SecondURL + "."
case "Type":
msg = d.FirstURL + " and " + d.SecondURL + " differs in type."
case "Size":
msg = d.FirstURL + " and " + d.SecondURL + " differs in size."
case "only-in-first":
msg = "" + d.FirstURL + "" + " and " + "" + d.SecondURL + "" + " - only in first."
case "type":
msg = "" + d.FirstURL + "" + " and " + "" + d.SecondURL + "" + " - differ in type."
case "size":
msg = "" + d.FirstURL + "" + " and " + "" + d.SecondURL + "" + " - differ in size."
default:
fatalIf(probe.NewError(errors.New("")), "Unhandled difference between "+d.FirstURL+" and "+d.SecondURL+".")
}
return msg
return console.Colorize("Diff", msg)
}
diffJSONBytes, err := json.Marshal(d)
fatalIf(probe.NewError(err), "Unable to marshal diff message "+d.FirstURL+", "+d.SecondURL+" and "+d.Diff+".")
@@ -127,7 +127,7 @@ func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) {
message: DiffMessage{
FirstURL: firstURL,
SecondURL: secondURL,
Diff: "Type",
Diff: "type",
}.String(),
err: nil,
}
@@ -143,7 +143,7 @@ func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) {
message: DiffMessage{
FirstURL: firstURL,
SecondURL: secondURL,
Diff: "Type",
Diff: "type",
}.String(),
err: nil,
}
@@ -181,7 +181,7 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) {
message: DiffMessage{
FirstURL: firstURL,
SecondURL: secondURL,
Diff: "Type",
Diff: "type",
}.String(),
err: nil,
}
@@ -199,7 +199,7 @@ func doDiffObjects(firstURL, secondURL string, ch chan diff) {
message: DiffMessage{
FirstURL: firstURL,
SecondURL: secondURL,
Diff: "Size",
Diff: "size",
}.String(),
err: nil,
}
@@ -234,22 +234,12 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) {
_, newFirstContent, errFirst := url2Stat(newFirstURL)
_, newSecondContent, errSecond := url2Stat(newSecondURL)
switch {
case errFirst != nil && errSecond == nil:
ch <- diff{
message: DiffMessage{
FirstURL: newSecondURL,
SecondURL: secondClnt.URL().String(),
Diff: "Only-in",
}.String(),
err: nil,
}
continue
case errFirst == nil && errSecond != nil:
ch <- diff{
message: DiffMessage{
FirstURL: newFirstURL,
SecondURL: firstClnt.URL().String(),
Diff: "Only-in",
SecondURL: newSecondURL,
Diff: "only-in-first",
}.String(),
err: nil,
}
@@ -262,7 +252,7 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) {
message: DiffMessage{
FirstURL: newFirstURL,
SecondURL: newSecondURL,
Diff: "Type",
Diff: "type",
}.String(),
err: nil,
}
@@ -274,7 +264,7 @@ func dodiff(firstClnt, secondClnt client.Client, ch chan diff) {
message: DiffMessage{
FirstURL: newFirstURL,
SecondURL: newSecondURL,
Diff: "Type",
Diff: "type",
}.String(),
err: nil,
}
@@ -327,7 +317,9 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) {
for {
select {
case <-time.Tick(100 * time.Millisecond):
console.PrintC("\r" + "Scanning.. " + string(<-cursorCh))
if !globalQuietFlag && !globalJSONFlag {
console.PrintC("\r" + "Scanning.. " + string(<-cursorCh))
}
case <-doneCh:
return
}
@@ -335,7 +327,9 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) {
}(doneCh)
wg.Wait()
doneCh <- struct{}{}
console.PrintC("\r" + "Finished." + "\n")
if !globalQuietFlag && !globalJSONFlag {
console.PrintC("\r" + "Finished." + "\n")
}
matchNameCh := make(chan string, 10000)
go func(matchNameCh chan<- string) {
@@ -354,7 +348,7 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) {
message: DiffMessage{
FirstURL: firstURL,
SecondURL: firstClnt.URL().String(),
Diff: "Only-in",
Diff: "only-in-first",
}.String(),
err: nil,
}

17
ls.go
View File

@@ -22,6 +22,7 @@ import (
"os"
"runtime"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/minio/mc/pkg/client"
@@ -37,17 +38,17 @@ const (
// ContentMessage container for content message structure.
type ContentMessage struct {
Filetype string `json:"type"`
Time string `json:"last-modified"`
Size string `json:"size"`
Name string `json:"name"`
Filetype string `json:"type"`
Time time.Time `json:"last-modified"`
Size int64 `json:"size"`
Name string `json:"name"`
}
// String string printer for Content metadata.
func (c ContentMessage) String() string {
if !globalJSONFlag {
message := console.Colorize("Time", fmt.Sprintf("[%s] ", c.Time))
message = message + console.Colorize("Size", fmt.Sprintf("%6s ", c.Size))
message := console.Colorize("Time", fmt.Sprintf("[%s] ", c.Time.Format(printDate)))
message = message + console.Colorize("Size", fmt.Sprintf("%6s ", humanize.IBytes(uint64(c.Size))))
message = func() string {
if c.Filetype == "folder" {
return message + console.Colorize("Dir", fmt.Sprintf("%s", c.Name))
@@ -66,7 +67,7 @@ func (c ContentMessage) String() string {
// parseContent parse client Content container into printer struct.
func parseContent(c *client.Content) ContentMessage {
content := ContentMessage{}
content.Time = c.Time.Local().Format(printDate)
content.Time = c.Time.Local()
// guess file type
content.Filetype = func() string {
@@ -76,7 +77,7 @@ func parseContent(c *client.Content) ContentMessage {
return "file"
}()
content.Size = humanize.IBytes(uint64(c.Size))
content.Size = c.Size
// Convert OS Type to match console file printing style.
content.Name = func() string {

View File

@@ -58,10 +58,10 @@ type sessionV2Header struct {
// SessionJSONMessage json container for session messages
type SessionJSONMessage struct {
SessionID string `json:"sessionid"`
Time string `json:"time"`
CommandType string `json:"command-type"`
CommandArgs []string `json:"command-args"`
SessionID string `json:"sessionid"`
Time time.Time `json:"time"`
CommandType string `json:"command-type"`
CommandArgs []string `json:"command-args"`
}
// sessionV2
@@ -82,7 +82,7 @@ func (s sessionV2) String() string {
}
sessionMesage := SessionJSONMessage{
SessionID: s.SessionID,
Time: s.Header.When.Local().Format(printDate),
Time: s.Header.When.Local(),
CommandType: s.Header.CommandType,
CommandArgs: s.Header.CommandArgs,
}

View File

@@ -25,6 +25,7 @@ import (
"strings"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console"
@@ -67,6 +68,9 @@ func mainShare(ctx *cli.Context) {
if !ctx.Args().Present() || ctx.Args().First() == "help" || len(ctx.Args()) > 2 {
cli.ShowCommandHelpAndExit(ctx, "share", 1) // last argument is exit code
}
console.SetCustomTheme(map[string]*color.Color{
"Share": color.New(color.FgGreen, color.Bold),
})
args := ctx.Args()
config := mustGetMcConfig()
/// get first and last arguments
@@ -95,7 +99,7 @@ type ShareMessage struct {
// String string printer for share message
func (s ShareMessage) String() string {
if !globalJSONFlag {
return fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s", s.Expires, s.PresignedURL)
return console.Colorize("Share", fmt.Sprintf("Succesfully generated shared URL with expiry %s, please share: %s", s.Expires, s.PresignedURL))
}
shareMessageBytes, err := json.Marshal(s)
fatalIf(probe.NewError(err), "Failed to marshal into JSON.")
@@ -140,7 +144,7 @@ func doShareCmd(targetURL string, recursive bool, expires time.Duration) *probe.
if err != nil {
return err.Trace()
}
console.PrintC(ShareMessage{Expires: expires, PresignedURL: presignedURL}.String() + "\n")
console.Println(ShareMessage{Expires: expires / time.Second, PresignedURL: presignedURL}.String())
}
return nil
}