Merge pull request #891 from harshavardhana/pr_out_use_console_json_to_print_in_api_style_for_session_ls_cp_and_sync
Use console.JSON to print in API style for session, ls, cp and sync
This commit is contained in:
@@ -90,7 +90,7 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue chan bool, wg *sync.WaitGroup
|
||||
|
||||
var newReader io.ReadCloser
|
||||
if globalQuietFlag || globalJSONFlag {
|
||||
console.Infoln(CopyMessage{
|
||||
console.PrintC(CopyMessage{
|
||||
Source: cpURLs.SourceContent.Name,
|
||||
Target: cpURLs.TargetContent.Name,
|
||||
Length: cpURLs.SourceContent.Size,
|
||||
|
||||
+1
-1
@@ -62,7 +62,7 @@ func listSessions() error {
|
||||
if err != nil {
|
||||
return NewIodine(iodine.New(err, nil))
|
||||
}
|
||||
console.Prints(s)
|
||||
console.Print(s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+1
-1
@@ -93,7 +93,7 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, wg *sync.WaitGrou
|
||||
|
||||
var newReader io.ReadCloser
|
||||
if globalQuietFlag || globalJSONFlag {
|
||||
console.Infoln(SyncMessage{
|
||||
console.PrintC(SyncMessage{
|
||||
Source: sURLs.SourceContent.Name,
|
||||
Targets: targetURLs,
|
||||
})
|
||||
|
||||
@@ -89,7 +89,7 @@ func doList(clnt client.Client, recursive bool) error {
|
||||
err = contentCh.Err
|
||||
break
|
||||
}
|
||||
console.Prints(parseContent(contentCh.Content))
|
||||
console.Print(parseContent(contentCh.Content))
|
||||
}
|
||||
if err != nil {
|
||||
return NewIodine(iodine.New(err, map[string]string{"Target": clnt.URL().String()}))
|
||||
|
||||
@@ -158,7 +158,8 @@ func newCpBar() barSend {
|
||||
if runtime.GOOS == "windows" {
|
||||
bar.Format("[=> ]")
|
||||
} else {
|
||||
bar.Format("┃▒▓░┃")
|
||||
bar.Format("┃▓█░┃")
|
||||
// bar.Format("█▓▒░█")
|
||||
}
|
||||
for msg := range cmdCh {
|
||||
switch msg.Cmd {
|
||||
|
||||
@@ -100,11 +100,6 @@ var (
|
||||
println(themesDB[currThemeName].Print, data...)
|
||||
}
|
||||
|
||||
// Prints prints a structured message with a newline
|
||||
Prints = func(data ...interface{}) {
|
||||
println(themesDB[currThemeName].Print, data...)
|
||||
return
|
||||
}
|
||||
// Fatal print a error message and exit
|
||||
Fatal = func(data ...interface{}) {
|
||||
defer os.Exit(1)
|
||||
@@ -196,6 +191,8 @@ var (
|
||||
Command = themesDB[currThemeName].Command.SprintfFunc()
|
||||
// SessionID helper to print sessionid theme
|
||||
SessionID = themesDB[currThemeName].SessionID.SprintfFunc()
|
||||
// JSON helper to print json strings
|
||||
JSON = themesDB[currThemeName].JSON.SprintfFunc()
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
+49
-12
@@ -20,12 +20,44 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/minio/mc/pkg/console"
|
||||
)
|
||||
|
||||
// SessionJSONMessage json container for session messages
|
||||
type SessionJSONMessage struct {
|
||||
Version string `json:"version"`
|
||||
SessionID string `json:"sessionid"`
|
||||
Time string `json:"time"`
|
||||
CommandType string `json:"command-type"`
|
||||
CommandArgs []string `json:"command-args"`
|
||||
}
|
||||
|
||||
func (s sessionV2) String() string {
|
||||
if !globalJSONFlag {
|
||||
message := console.SessionID("%s -> ", s.SessionID)
|
||||
message = message + console.Time("[%s]", s.Header.When.Local().Format(printDate))
|
||||
message = message + console.Command(" %s %s", s.Header.CommandType, strings.Join(s.Header.CommandArgs, " "))
|
||||
return message + "\n"
|
||||
}
|
||||
sessionMesage := SessionJSONMessage{
|
||||
Version: s.Header.Version,
|
||||
SessionID: s.SessionID,
|
||||
Time: s.Header.When.Local().Format(printDate),
|
||||
CommandType: s.Header.CommandType,
|
||||
CommandArgs: s.Header.CommandArgs,
|
||||
}
|
||||
sessionJSONBytes, err := json.MarshalIndent(sessionMesage, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return console.JSON(string(sessionJSONBytes) + "\n")
|
||||
}
|
||||
|
||||
// Content container for content message structure
|
||||
type Content struct {
|
||||
Version string `json:"version"`
|
||||
Filetype string `json:"type"`
|
||||
Time string `json:"last-modified"`
|
||||
Size string `json:"size"`
|
||||
@@ -43,36 +75,40 @@ func (c Content) String() string {
|
||||
}
|
||||
return message + console.File("%s", c.Name)
|
||||
}()
|
||||
return message
|
||||
return message + "\n"
|
||||
}
|
||||
cBytes, err := json.Marshal(c)
|
||||
c.Version = "1.0.0"
|
||||
jsonMessageBytes, err := json.MarshalIndent(c, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(cBytes)
|
||||
return console.JSON(string(jsonMessageBytes) + "\n")
|
||||
}
|
||||
|
||||
// CopyMessage container for file copy messages
|
||||
type CopyMessage struct {
|
||||
Source string `json:"source"`
|
||||
Target string `json:"target"`
|
||||
Length int64 `json:"length"`
|
||||
Version string `json:"version"`
|
||||
Source string `json:"source"`
|
||||
Target string `json:"target"`
|
||||
Length int64 `json:"length"`
|
||||
}
|
||||
|
||||
// String string printer for copy message
|
||||
func (c CopyMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
return fmt.Sprintf("‘%s’ -> ‘%s’", c.Source, c.Target)
|
||||
return fmt.Sprintf("‘%s’ -> ‘%s’\n", c.Source, c.Target)
|
||||
}
|
||||
cBytes, err := json.Marshal(c)
|
||||
c.Version = "1.0.0"
|
||||
copyMessageBytes, err := json.MarshalIndent(c, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(cBytes)
|
||||
return console.JSON(string(copyMessageBytes) + "\n")
|
||||
}
|
||||
|
||||
// SyncMessage container for file sync messages, inherits CopyMessage
|
||||
type SyncMessage struct {
|
||||
Version string `json:"version"`
|
||||
Source string `json:"source"`
|
||||
Targets []string `json:"targets"`
|
||||
Length int64 `json:"length"`
|
||||
@@ -81,11 +117,12 @@ type SyncMessage struct {
|
||||
// String string printer for sync message
|
||||
func (s SyncMessage) String() string {
|
||||
if !globalJSONFlag {
|
||||
return fmt.Sprintf("‘%s’ -> ‘%s’", s.Source, s.Targets)
|
||||
return fmt.Sprintf("‘%s’ -> ‘%s’\n", s.Source, s.Targets)
|
||||
}
|
||||
sBytes, err := json.Marshal(s)
|
||||
s.Version = "1.0.0"
|
||||
syncMessageBytes, err := json.MarshalIndent(s, "", "\t")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(sBytes)
|
||||
return console.JSON(string(syncMessageBytes) + "\n")
|
||||
}
|
||||
|
||||
+2
-10
@@ -21,7 +21,6 @@ package main
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -83,19 +82,12 @@ func newSessionV2() *sessionV2 {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *sessionV2) String() string {
|
||||
message := console.SessionID("%s -> ", s.SessionID)
|
||||
message = message + console.Time("[%s]", s.Header.When.Local().Format(printDate))
|
||||
message = message + console.Command(" %s %s", s.Header.CommandType, strings.Join(s.Header.CommandArgs, " "))
|
||||
return message
|
||||
}
|
||||
|
||||
func (s *sessionV2) Info() {
|
||||
func (s sessionV2) Info() {
|
||||
console.Infoln("Session terminated. To resume session type ‘mc session resume " + s.SessionID + "’")
|
||||
}
|
||||
|
||||
// NewDataReader provides reader interface to session data file.
|
||||
func (s *sessionV2) HasData() bool {
|
||||
func (s sessionV2) HasData() bool {
|
||||
if s.Header.LastCopied == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user