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()) == "" { if strings.TrimSpace(ctx.Args().First()) == "" {
cli.ShowCommandHelpAndExit(ctx, "config", 1) // last argument is exit code 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() arg := ctx.Args().First()
tailArgs := ctx.Args().Tail() tailArgs := ctx.Args().Tail()
if len(tailArgs) > 2 { if len(tailArgs) > 2 {
@@ -106,11 +112,6 @@ func mainConfig(ctx *cli.Context) {
// convert interface{} back to its original struct // convert interface{} back to its original struct
newConf := config.Data().(*configV2) 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 { for k, v := range newConf.Aliases {
console.Println(AliasMessage{k, v}) console.Println(AliasMessage{k, v})
} }

View File

@@ -17,9 +17,9 @@
package main package main
import ( import (
"fmt" "github.com/fatih/color"
"github.com/minio/cli" "github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/probe" "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 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() config := mustGetMcConfig()
firstArg := ctx.Args().First() firstArg := ctx.Args().First()
secondArg := ctx.Args()[1] secondArg := ctx.Args()[1]
@@ -75,7 +79,7 @@ func mainDiff(ctx *cli.Context) {
newFirstURL := stripRecursiveURL(firstURL) newFirstURL := stripRecursiveURL(firstURL)
for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) { for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) {
fatalIf(diff.err.Trace(newFirstURL, secondURL), "Failed to diff "+firstURL+" and "+secondURL+".") fatalIf(diff.err.Trace(newFirstURL, secondURL), "Failed to diff "+firstURL+" and "+secondURL+".")
fmt.Println(diff.message) console.Println(diff.message)
} }
} }

46
diff.go
View File

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

13
ls.go
View File

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

View File

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

View File

@@ -25,6 +25,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/fatih/color"
"github.com/minio/cli" "github.com/minio/cli"
"github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console" "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 { if !ctx.Args().Present() || ctx.Args().First() == "help" || len(ctx.Args()) > 2 {
cli.ShowCommandHelpAndExit(ctx, "share", 1) // last argument is exit code 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() args := ctx.Args()
config := mustGetMcConfig() config := mustGetMcConfig()
/// get first and last arguments /// get first and last arguments
@@ -95,7 +99,7 @@ type ShareMessage struct {
// String string printer for share message // String string printer for share message
func (s ShareMessage) String() string { func (s ShareMessage) String() string {
if !globalJSONFlag { 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) shareMessageBytes, err := json.Marshal(s)
fatalIf(probe.NewError(err), "Failed to marshal into JSON.") 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 { if err != nil {
return err.Trace() return err.Trace()
} }
console.PrintC(ShareMessage{Expires: expires, PresignedURL: presignedURL}.String() + "\n") console.Println(ShareMessage{Expires: expires / time.Second, PresignedURL: presignedURL}.String())
} }
return nil return nil
} }