Refactor debugging output, implement --json output currently only supported on 'ls'

This commit is contained in:
Harshavardhana
2015-05-21 22:30:32 -07:00
parent b80394c842
commit edb8381400
18 changed files with 94 additions and 86 deletions
+1 -1
View File
@@ -44,6 +44,6 @@ func (s *CmdTestSuite) TestCatCmd(c *C) {
sourceConfigMap[objectPath] = &hostConfig{}
sourceConfigMap[server.URL+"/bucket/object1"] = &hostConfig{}
_, err = doCatCmd(sourceConfigMap, false)
_, err = doCatCmd(sourceConfigMap)
c.Assert(err, IsNil)
}
+3 -9
View File
@@ -35,7 +35,6 @@ func runAccessCmd(ctx *cli.Context) {
}
config, err := getMcConfig()
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("loading config file failed with following reason: [%s]\n", iodine.ToError(err))
}
targetURLConfigMap := make(map[string]*hostConfig)
@@ -43,44 +42,39 @@ func runAccessCmd(ctx *cli.Context) {
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unknown type of URL ‘%s’. Reason: %s.\n", e.url, e)
default:
console.Debugln(iodine.New(err, nil))
console.Fatalf("reading URLs failed with following Reason: %s\n", e)
}
}
acl := bucketACL(ctx.Args().First())
if !acl.isValidBucketACL() {
console.Debugln(iodine.New(errInvalidACL{acl: acl.String()}, nil))
console.Fatalf("Access type ‘%s’ is not supported. Valid types are [private, public, readonly].\n", acl)
}
targetURLs = targetURLs[1:] // 1 or more target URLs
for _, targetURL := range targetURLs {
targetConfig, err := getHostConfig(targetURL)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read configuration for host ‘%s’. Reason: %s.\n", targetURL, iodine.ToError(err))
}
targetURLConfigMap[targetURL] = targetConfig
}
for targetURL, targetConfig := range targetURLConfigMap {
errorMsg, err := doUpdateAccessCmd(targetURL, acl.String(), targetConfig, globalDebugFlag)
errorMsg, err := doUpdateAccessCmd(targetURL, acl.String(), targetConfig)
err = iodine.New(err, nil)
if err != nil {
if errorMsg == "" {
errorMsg = "Empty error message. Please rerun this command with --debug and file a bug report."
}
console.Debugln(err)
console.Errorf("%s", errorMsg)
}
}
}
func doUpdateAccessCmd(targetURL, targetACL string, targetConfig *hostConfig, debug bool) (string, error) {
func doUpdateAccessCmd(targetURL, targetACL string, targetConfig *hostConfig) (string, error) {
var err error
var clnt client.Client
clnt, err = getNewClient(targetURL, targetConfig, debug)
clnt, err = getNewClient(targetURL, targetConfig)
if err != nil {
err := iodine.New(err, nil)
msg := fmt.Sprintf("Unable to initialize client for ‘%s’. Reason: %s.\n",
+3 -8
View File
@@ -35,7 +35,6 @@ func runCatCmd(ctx *cli.Context) {
}
config, err := getMcConfig()
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read config file ‘%s’. Reason: %s.\n", mustGetMcConfigPath(), iodine.ToError(err))
}
@@ -44,10 +43,8 @@ func runCatCmd(ctx *cli.Context) {
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unknown type of URL ‘%s’. Reason: %s.\n", e.url, e)
default:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to parse arguments. Reason: %s.\n", iodine.ToError(err))
}
}
@@ -55,20 +52,18 @@ func runCatCmd(ctx *cli.Context) {
sourceURLs := urls
sourceURLConfigMap, err := getHostConfigs(sourceURLs)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read host configuration for ‘%s’ from config file ‘%s’. Reason: %s.\n",
sourceURLs, mustGetMcConfigPath(), iodine.ToError(err))
}
humanReadable, err := doCatCmd(sourceURLConfigMap, globalDebugFlag)
humanReadable, err := doCatCmd(sourceURLConfigMap)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalln(humanReadable)
}
}
func doCatCmd(sourceURLConfigMap map[string]*hostConfig, debug bool) (string, error) {
func doCatCmd(sourceURLConfigMap map[string]*hostConfig) (string, error) {
for url, config := range sourceURLConfigMap {
sourceClnt, err := getNewClient(url, config, debug)
sourceClnt, err := getNewClient(url, config)
if err != nil {
return "Unable to create client: " + url, iodine.New(err, nil)
}
-2
View File
@@ -34,12 +34,10 @@ func runConfigCmd(ctx *cli.Context) {
arg := ctx.Args().First()
tailArgs := ctx.Args().Tail()
if len(tailArgs) > 2 {
console.Debugln(iodine.New(errInvalidArgument{}, nil))
console.Fatalln("Incorrect number of arguments, please use \"mc config help\"")
}
msg, err := doConfig(arg, tailArgs)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalln(msg)
}
console.Infoln(msg)
+3 -8
View File
@@ -32,7 +32,6 @@ func runListCmd(ctx *cli.Context) {
}
config, err := getMcConfig()
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read config file ‘%s’. Reason: %s.\n", mustGetMcConfigPath(), iodine.ToError(err))
}
targetURLConfigMap := make(map[string]*hostConfig)
@@ -41,16 +40,13 @@ func runListCmd(ctx *cli.Context) {
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unknown type of URL ‘%s’. Reason: %s.\n", e.url, e)
default:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to parse argument ‘%s’. Reason: %s.\n", arg, iodine.ToError(err))
}
}
targetConfig, err := getHostConfig(targetURL)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read host configuration for ‘%s’ from config file ‘%s’. Reason: %s.\n",
targetURL, mustGetMcConfigPath(), iodine.ToError(err))
}
@@ -59,18 +55,17 @@ func runListCmd(ctx *cli.Context) {
for targetURL, targetConfig := range targetURLConfigMap {
// if recursive strip off the "..."
newTargetURL := stripRecursiveURL(targetURL)
err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL), globalDebugFlag)
err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL))
err = iodine.New(err, nil)
if err != nil {
console.Debugln(err)
console.Fatalf("Failed to list ‘%s’. Reason: %s.\n", targetURL, iodine.ToError(err))
}
}
}
// doListCmd -
func doListCmd(targetURL string, targetConfig *hostConfig, recursive, debug bool) error {
clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
func doListCmd(targetURL string, targetConfig *hostConfig, recursive bool) error {
clnt, err := getNewClient(targetURL, targetConfig)
if err != nil {
return iodine.New(err, map[string]string{"Target": targetURL})
}
+3 -8
View File
@@ -36,7 +36,6 @@ func runMakeBucketCmd(ctx *cli.Context) {
}
config, err := getMcConfig()
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read config file ‘%s’. Reason: %s\n", mustGetMcConfigPath(), iodine.ToError(err))
}
targetURLConfigMap := make(map[string]*hostConfig)
@@ -44,39 +43,35 @@ func runMakeBucketCmd(ctx *cli.Context) {
if err != nil {
switch e := iodine.ToError(err).(type) {
case errUnsupportedScheme:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unknown URL type ‘%s’ passed. Reason: %s.\n", e.url, e)
default:
console.Debugln(iodine.New(err, nil))
console.Fatalf("Error in parsing path or URL. Reason: %s.\n", e)
}
}
for _, targetURL := range targetURLs {
targetConfig, err := getHostConfig(targetURL)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read configuration for host ‘%s’. Reason: %s.\n", targetURL, iodine.ToError(err))
}
targetURLConfigMap[targetURL] = targetConfig
}
for targetURL, targetConfig := range targetURLConfigMap {
errorMsg, err := doMakeBucketCmd(targetURL, targetConfig, globalDebugFlag)
errorMsg, err := doMakeBucketCmd(targetURL, targetConfig)
err = iodine.New(err, nil)
if err != nil {
if errorMsg == "" {
errorMsg = "Empty error message. Please rerun this command with --debug and file a bug report."
}
console.Debugln(err)
console.Errorf("%s", errorMsg)
}
}
}
// doMakeBucketCmd -
func doMakeBucketCmd(targetURL string, targetConfig *hostConfig, debug bool) (string, error) {
func doMakeBucketCmd(targetURL string, targetConfig *hostConfig) (string, error) {
var err error
var clnt client.Client
clnt, err = getNewClient(targetURL, targetConfig, debug)
clnt, err = getNewClient(targetURL, targetConfig)
if err != nil {
err := iodine.New(err, nil)
msg := fmt.Sprintf("Unable to initialize client for ‘%s’. Reason: %s.\n",
+6 -1
View File
@@ -340,7 +340,8 @@ var commands = []cli.Command{
// Collection of mc flags currently supported
//
// --theme "minimal"Choose a console theme from this list [*minimal*, nocolor, white]
// --theme "minimal" Choose a console theme from this list [*minimal*, nocolor, white]
// --json Enable json formatted output
// --debug Enable HTTP tracing
// --quiet, -q Supress chatty console output
// --retry "5" Number of retry count
@@ -362,6 +363,10 @@ var (
return strings.Join(keys, ", ")
}()),
},
cli.BoolFlag{
Name: "json",
Usage: "Enable json formatted output",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable HTTP tracing",
+1 -3
View File
@@ -30,7 +30,7 @@ const (
)
func doUpdateCheck(config *hostConfig) (string, error) {
clnt, err := getNewClient(mcUpdateURL, config, globalDebugFlag)
clnt, err := getNewClient(mcUpdateURL, config)
if err != nil {
return "Unable to create client: " + mcUpdateURL, iodine.New(err, map[string]string{"failedURL": mcUpdateURL})
}
@@ -60,12 +60,10 @@ func runUpdateCmd(ctx *cli.Context) {
}
hostConfig, err := getHostConfig(mcUpdateURL)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read configuration for host ‘%s’. Reason: %s.\n", mcUpdateURL, iodine.ToError(err))
}
msg, err := doUpdateCheck(hostConfig)
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalln(msg)
}
if msg != "" {
+4 -4
View File
@@ -66,13 +66,13 @@ func (s *CmdTestSuite) TearDownSuite(c *C) {
}
func (s *CmdTestSuite) TestGetNewClient(c *C) {
_, err := getNewClient("http://example.com/bucket1", &hostConfig{}, false)
_, err := getNewClient("http://example.com/bucket1", &hostConfig{})
c.Assert(err, IsNil)
_, err = getNewClient("C:\\Users\\Administrator\\MyDocuments", &hostConfig{}, false)
_, err = getNewClient("C:\\Users\\Administrator\\MyDocuments", &hostConfig{})
c.Assert(err, IsNil)
_, err = getNewClient("/usr/bin/pandoc", &hostConfig{}, false)
_, err = getNewClient("/usr/bin/pandoc", &hostConfig{})
c.Assert(err, IsNil)
_, err = getNewClient("pkg/client", &hostConfig{}, false)
_, err = getNewClient("pkg/client", &hostConfig{})
c.Assert(err, IsNil)
}
+5 -5
View File
@@ -29,7 +29,7 @@ import (
// getSource -
func getSource(sourceURL string, sourceConfig *hostConfig) (reader io.Reader, length uint64, err error) {
sourceClnt, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag)
sourceClnt, err := getNewClient(sourceURL, sourceConfig)
if err != nil {
return nil, 0, iodine.New(err, map[string]string{"failedURL": sourceURL})
}
@@ -38,7 +38,7 @@ func getSource(sourceURL string, sourceConfig *hostConfig) (reader io.Reader, le
// putTarget -
func putTarget(targetURL string, targetConfig *hostConfig, length uint64, data io.Reader) error {
targetClnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
targetClnt, err := getNewClient(targetURL, targetConfig)
if err != nil {
return iodine.New(err, nil)
}
@@ -50,7 +50,7 @@ func putTarget(targetURL string, targetConfig *hostConfig, length uint64, data i
}
// getNewClient gives a new client interface
func getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Client, err error) {
func getNewClient(urlStr string, auth *hostConfig) (clnt client.Client, err error) {
url, err := client.Parse(urlStr)
if err != nil {
return nil, iodine.New(errInvalidURL{url: urlStr}, nil)
@@ -67,7 +67,7 @@ func getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Clie
s3Config.AppVersion = Version
s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH}
s3Config.HostURL = urlStr
s3Config.Debug = debug
s3Config.Debug = globalDebugFlag
return s3.New(s3Config)
case client.Filesystem:
return fs.New(urlStr)
@@ -82,7 +82,7 @@ func url2Stat(urlStr string) (client client.Client, content *client.Content, err
return nil, nil, iodine.New(err, map[string]string{"URL": urlStr})
}
client, err = getNewClient(urlStr, config, globalDebugFlag)
client, err = getNewClient(urlStr, config)
if err != nil {
return nil, nil, iodine.New(err, map[string]string{"URL": urlStr})
}
+3 -3
View File
@@ -83,7 +83,7 @@ func source2Client(sourceURL string) (client.Client, error) {
return nil, iodine.New(errInvalidSource{path: sourceURL}, nil)
}
sourceClient, err := getNewClient(sourceURL, sourceConfig, globalDebugFlag)
sourceClient, err := getNewClient(sourceURL, sourceConfig)
if err != nil {
return nil, iodine.New(errInvalidSource{path: sourceURL}, nil)
}
@@ -105,7 +105,7 @@ func target2Client(targetURL string) (client.Client, error) {
return nil, iodine.New(errInvalidTarget{path: targetURL}, nil)
}
targetClient, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
targetClient, err := getNewClient(targetURL, targetConfig)
if err != nil {
return nil, iodine.New(errInvalidTarget{path: targetURL}, nil)
}
@@ -123,7 +123,7 @@ func isTargetURLDir(targetURL string) bool {
return false
}
targetClient, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
targetClient, err := getNewClient(targetURL, targetConfig)
if err != nil {
return false
}
-8
View File
@@ -24,14 +24,6 @@ func (e errNotAnObject) Error() string {
return "Not an object " + e.url
}
type errInvalidACL struct {
acl string
}
func (e errInvalidACL) Error() string {
return "Invalid ACL: " + e.acl
}
type errInvalidArgument struct{}
func (e errInvalidArgument) Error() string {
+1
View File
@@ -21,6 +21,7 @@ var (
globalDebugFlag = false // Debug flag set via command line
globalQuietFlag = false // Quiet flag set via command line
globalMaxRetryFlag = 5 // Maximum number of retries
globalJSONFlag = false // Json flag set via command line
mcCurrentConfigVersion = "1.0.0"
)
+40 -8
View File
@@ -17,6 +17,7 @@
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
@@ -28,25 +29,51 @@ import (
"github.com/minio/minio/pkg/iodine"
)
/// LS - related internal functions
/// ls - related internal functions
// iso8601 date
const (
printDate = "2006-01-02 15:04:05 MST"
)
// printJSON rather than colored output
func printJSON(content *client.Content) {
type jsonContent struct {
Filetype string `json:"content-type"`
Date string `json:"last-modified"`
Size string `json:"size"`
Name string `json:"name"`
}
contentJSON := new(jsonContent)
contentJSON.Date = content.Time.Local().Format(printDate)
contentJSON.Size = humanize.IBytes(uint64(content.Size))
contentJSON.Filetype = func() string {
if content.Type.IsDir() {
return "inode/directory"
}
if content.Type.IsRegular() {
return "application/octet-stream"
}
return "application/octet-stream"
}()
contentJSON.Name = content.Name
contentBytes, _ := json.MarshalIndent(contentJSON, "", "\t")
fmt.Println(string(contentBytes))
}
// printContent prints content meta-data
func printContent(date time.Time, v int64, name string, fileType os.FileMode) {
func printContent(date time.Time, size int64, name string, fileType os.FileMode) {
fmt.Printf(console.Time("[%s] ", date.Local().Format(printDate)))
fmt.Printf(console.Size("%6s ", humanize.IBytes(uint64(v))))
fmt.Printf(console.Size("%6s ", humanize.IBytes(uint64(size))))
// just making it explicit
switch fileType.IsDir() {
case true:
switch {
case fileType.IsDir() == true:
// if one finds a prior suffix no need to append a new one
if strings.HasSuffix(name, "/") {
switch {
case strings.HasSuffix(name, "/") == true:
fmt.Println(console.Dir("%s", name))
} else {
default:
fmt.Println(console.Dir("%s/", name))
}
default:
@@ -70,7 +97,12 @@ func doList(clnt client.Client, targetURL string, recursive bool) error {
// To be consistent we have to filter them out
contentName = strings.TrimPrefix(contentName, strings.TrimSuffix(targetURL, "/")+"/")
}
printContent(contentCh.Content.Time, contentCh.Content.Size, contentName, contentCh.Content.Type)
switch {
case globalJSONFlag == true:
printJSON(contentCh.Content)
default:
printContent(contentCh.Content.Time, contentCh.Content.Size, contentName, contentCh.Content.Type)
}
}
if err != nil {
return iodine.New(err, map[string]string{"Target": targetURL})
+4 -4
View File
@@ -40,10 +40,10 @@ func (s *CmdTestSuite) TestLSCmd(c *C) {
c.Assert(err, IsNil)
}
err = doListCmd(root, &hostConfig{}, false, false)
err = doListCmd(root, &hostConfig{}, false)
c.Assert(err, IsNil)
err = doListCmd(root, &hostConfig{}, true, false)
err = doListCmd(root, &hostConfig{}, true)
c.Assert(err, IsNil)
for i := 0; i < 10; i++ {
@@ -53,10 +53,10 @@ func (s *CmdTestSuite) TestLSCmd(c *C) {
err := putTarget(objectPath, &hostConfig{}, uint64(dataLen), bytes.NewReader([]byte(data)))
c.Assert(err, IsNil)
}
err = doListCmd(server.URL+"/bucket", &hostConfig{}, false, false)
err = doListCmd(server.URL+"/bucket", &hostConfig{}, false)
c.Assert(err, IsNil)
err = doListCmd(server.URL+"/bucket", &hostConfig{}, true, false)
err = doListCmd(server.URL+"/bucket", &hostConfig{}, true)
c.Assert(err, IsNil)
}
+5 -5
View File
@@ -34,7 +34,6 @@ import (
func checkConfig() {
_, err := user.Current()
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalln("Unable to determine current user")
}
@@ -46,7 +45,6 @@ func checkConfig() {
// Ensures config file is sane
_, err = getMcConfig()
if err != nil {
console.Debugln(iodine.New(err, nil))
console.Fatalf("Unable to read config file: %s\n", mustGetMcConfigPath())
}
}
@@ -104,18 +102,20 @@ func main() {
globalQuietFlag = ctx.GlobalBool("quiet")
globalDebugFlag = ctx.GlobalBool("debug")
globalMaxRetryFlag = ctx.GlobalInt("retry")
globalJSONFlag = ctx.GlobalBool("json")
if globalDebugFlag {
app.ExtraInfo = getSystemData()
console.NoDebugPrint = false
}
themeName := ctx.GlobalString("theme")
if console.IsValidTheme(themeName) {
switch {
case console.IsValidTheme(themeName) != true:
console.Fatalf("Theme ‘%s’ is not supported. Please choose from this list: %s.\n", themeName, console.GetThemeNames())
default:
err := console.SetTheme(themeName)
if err != nil {
console.Fatalf("Failed to set theme ‘%s’. Reason: %s.\n", themeName, iodine.ToError(err))
}
} else {
console.Fatalf("Theme ‘%s’ is not supported. Please choose from this list: %s.\n", themeName, console.GetThemeNames())
}
checkConfig()
return nil
+6 -6
View File
@@ -30,22 +30,22 @@ func (s *CmdTestSuite) TestMbAndAccessCmd(c *C) {
c.Assert(err, IsNil)
defer os.RemoveAll(root)
_, err = doMakeBucketCmd(filepath.Join(root, "bucket"), &hostConfig{}, false)
_, err = doMakeBucketCmd(filepath.Join(root, "bucket"), &hostConfig{})
c.Assert(err, IsNil)
_, err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "public-read-write", &hostConfig{}, false)
_, err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "public-read-write", &hostConfig{})
c.Assert(err, IsNil)
_, err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "invalid", &hostConfig{}, false)
_, err = doUpdateAccessCmd(filepath.Join(root, "bucket"), "invalid", &hostConfig{})
c.Assert(err, Not(IsNil))
_, err = doMakeBucketCmd(server.URL+"/bucket", &hostConfig{}, false)
_, err = doMakeBucketCmd(server.URL+"/bucket", &hostConfig{})
c.Assert(err, IsNil)
_, err = doUpdateAccessCmd(server.URL+"/bucket", "public-read-write", &hostConfig{}, false)
_, err = doUpdateAccessCmd(server.URL+"/bucket", "public-read-write", &hostConfig{})
c.Assert(err, IsNil)
_, err = doUpdateAccessCmd(server.URL+"/bucket", "invalid", &hostConfig{}, false)
_, err = doUpdateAccessCmd(server.URL+"/bucket", "invalid", &hostConfig{})
c.Assert(err, Not(IsNil))
}
+6 -3
View File
@@ -28,7 +28,7 @@ import (
"github.com/shiena/ansicolor"
)
// NoDebugPrint defines if the input should be printed or not. By default it's set to true.
// NoDebugPrint defines if the input should be printed in debug or not. By default it's set to true.
var NoDebugPrint = true
var (
@@ -38,7 +38,11 @@ var (
stderrColoredOutput = ansicolor.NewAnsiColorWriter(os.Stderr)
// themesDB contains supported list of Themes
themesDB = map[string]Theme{"minimal": MiniTheme, "nocolor": NoColorTheme, "white": WhiteTheme}
themesDB = map[string]Theme{
"minimal": MiniTheme,
"nocolor": NoColorTheme,
"white": WhiteTheme,
}
// currTheme is current theme
currThemeName = func() string {
@@ -120,7 +124,6 @@ type Theme struct {
File *color.Color
Dir *color.Color
Retry *color.Color
// Reason *color.Color
}
var (