diff --git a/cmd/admin-config-help.go b/cmd/admin-config-help.go index 08ea04a3..86f02528 100644 --- a/cmd/admin-config-help.go +++ b/cmd/admin-config-help.go @@ -17,9 +17,11 @@ package cmd import ( - "encoding/base64" - "io/ioutil" + "strings" + "text/tabwriter" + "text/template" + "github.com/fatih/color" "github.com/minio/cli" json "github.com/minio/mc/pkg/colorjson" "github.com/minio/mc/pkg/probe" @@ -32,6 +34,30 @@ var helpFlags = []cli.Flag{ }, } +// Help template used by all sub-systems +const Help = `{{colorBlueBold "Key"}}{{"\t"}}{{colorBlueBold "Description"}} +{{colorYellowBold "----"}}{{"\t"}}{{colorYellowBold "----"}} +{{range $key, $value := .}}{{colorCyanBold $key}}{{ "\t" }}{{$value}} +{{end}}` + +// HelpEnv template used by all sub-systems +const HelpEnv = `{{colorBlueBold "KeyEnv"}}{{"\t"}}{{colorBlueBold "Description"}} +{{colorYellowBold "----"}}{{"\t"}}{{colorYellowBold "----"}} +{{range $key, $value := .}}{{colorCyanBold $key}}{{ "\t" }}{{$value}} +{{end}}` + +var funcMap = template.FuncMap{ + "colorBlueBold": color.New(color.FgBlue, color.Bold).SprintfFunc(), + "colorYellowBold": color.New(color.FgYellow, color.Bold).SprintfFunc(), + "colorCyanBold": color.New(color.FgCyan, color.Bold).SprintFunc(), +} + +// HelpTemplate - captures config help template +var HelpTemplate = template.Must(template.New("config-help").Funcs(funcMap).Parse(Help)) + +// HelpEnvTemplate - captures config help template +var HelpEnvTemplate = template.Must(template.New("config-help-env").Funcs(funcMap).Parse(HelpEnv)) + var adminConfigHelpCmd = cli.Command{ Name: "help", Usage: "show help for each sub-system and keys", @@ -58,19 +84,31 @@ EXAMPLES: // configHelpMessage container to hold locks information. type configHelpMessage struct { - Status string `json:"status"` - Value string `json:"value"` + Status string `json:"status"` + Value map[string]string `json:"help"` + envOnly bool } // String colorized service status message. func (u configHelpMessage) String() string { - return u.Value + var s strings.Builder + w := tabwriter.NewWriter(&s, 1, 8, 2, ' ', 0) + var e error + if !u.envOnly { + e = HelpTemplate.Execute(w, u.Value) + } else { + e = HelpEnvTemplate.Execute(w, u.Value) + } + fatalIf(probe.NewError(e), "Cannot initialize template writer") + + w.Flush() + + return s.String() } // JSON jsonified service status Message message. func (u configHelpMessage) JSON() string { u.Status = "success" - u.Value = base64.StdEncoding.EncodeToString([]byte(u.Value)) statusJSONBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") @@ -100,12 +138,10 @@ func mainAdminConfigHelp(ctx *cli.Context) error { hr, e := client.HelpConfigKV(args.Get(1), args.Get(2), ctx.IsSet("env")) fatalIf(probe.NewError(e), "Cannot get help for the sub-system") - buf, e := ioutil.ReadAll(hr) - fatalIf(probe.NewError(e), "Cannot get help for the sub-system") - // Print printMsg(configHelpMessage{ - Value: string(buf), + Value: hr, + envOnly: ctx.IsSet("env"), }) return nil diff --git a/cmd/admin-config-history-list.go b/cmd/admin-config-history-list.go index 95a693de..7b4e62fa 100644 --- a/cmd/admin-config-history-list.go +++ b/cmd/admin-config-history-list.go @@ -17,8 +17,9 @@ package cmd import ( - "fmt" "strings" + "text/tabwriter" + "text/template" "github.com/fatih/color" "github.com/minio/cli" @@ -28,12 +29,20 @@ import ( "github.com/minio/minio/pkg/madmin" ) +var historyListFlags = []cli.Flag{ + cli.IntFlag{ + Name: "count, n", + Usage: "list only 'n' lines of history output", + Value: 10, + }, +} + var adminConfigHistoryListCmd = cli.Command{ Name: "list", Usage: "list all previously set keys on MinIO server", Before: setGlobalsFromContext, Action: mainAdminConfigHistoryList, - Flags: globalFlags, + Flags: append(append([]cli.Flag{}, globalFlags...), historyListFlags...), CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} @@ -49,23 +58,38 @@ EXAMPLES: `, } +// HistoryList template used by all sub-systems +const HistoryList = `{{range .}}{{colorYellowBold "RestoreId:"}} {{colorYellowBold .RestoreID}} +Date: {{.CreateTime}} + +{{.Targets}} + +{{end}}` + +// HistoryListTemplate - captures history list template +var HistoryListTemplate = template.Must(template.New("history-list").Funcs(funcMap).Parse(HistoryList)) + +type historyEntry struct { + RestoreID string `json:"restoreId"` + CreateTime string `json:"createTime"` + Targets madmin.Targets `json:"targets"` +} + // configHistoryListMessage container to hold locks information. type configHistoryListMessage struct { - Status string `json:"status"` - Entries []madmin.ConfigHistoryEntry `json:"entries"` + Status string `json:"status"` + Entries []historyEntry `json:"entries"` } // String colorized service status message. func (u configHistoryListMessage) String() string { - var s []string - for _, g := range u.Entries { - message := console.Colorize("ConfigHistoryListMessageTime", fmt.Sprintf("[%s] ", - g.CreateTime.Format(printDate))) - message = message + console.Colorize("ConfigHistoryListMessageRestoreID", g.RestoreID) - s = append(s, message) - } - return strings.Join(s, "\n") + var s strings.Builder + w := tabwriter.NewWriter(&s, 1, 8, 2, ' ', 0) + e := HistoryListTemplate.Execute(w, u.Entries) + fatalIf(probe.NewError(e), "Cannot initialize template writer") + w.Flush() + return s.String() } // JSON jsonified service status Message message. @@ -99,12 +123,22 @@ func mainAdminConfigHistoryList(ctx *cli.Context) error { client, err := newAdminClient(aliasedURL) fatalIf(err, "Unable to initialize admin connection.") - chEntries, e := client.ListConfigHistoryKV() + chEntries, e := client.ListConfigHistoryKV(ctx.Int("count")) fatalIf(probe.NewError(e), "Cannot list server history configuration.") + hentries := make([]historyEntry, len(chEntries)) + for i, chEntry := range chEntries { + hentries[i] = historyEntry{ + RestoreID: chEntry.RestoreID, + CreateTime: chEntry.CreateTimeFormatted(), + } + hentries[i].Targets, e = madmin.ParseSubSysTarget([]byte(chEntry.Data)) + fatalIf(probe.NewError(e), "Unable to parse invalid history entry.") + } + // Print printMsg(configHistoryListMessage{ - Entries: chEntries, + Entries: hentries, }) return nil diff --git a/cmd/admin-info-cpu.go b/cmd/admin-info-cpu.go index 0d2b4320..8bfe24bb 100644 --- a/cmd/admin-info-cpu.go +++ b/cmd/admin-info-cpu.go @@ -154,7 +154,7 @@ func mainAdminCPUInfo(ctx *cli.Context) error { return nil } - sort.Stable(&sortCPUWrapper{cpuLoads: cpuLoads}) + sort.Sort(&sortCPUWrapper{cpuLoads: cpuLoads}) for _, cpuLoad := range cpuLoads { if cpuLoad.Error != "" { diff --git a/cmd/admin-info-mem.go b/cmd/admin-info-mem.go index e555ea4c..8cc086cb 100644 --- a/cmd/admin-info-mem.go +++ b/cmd/admin-info-mem.go @@ -155,7 +155,7 @@ func mainAdminMemInfo(ctx *cli.Context) error { // exit immediately if error encountered return nil } - sort.Stable(&sortMemWrapper{memUsages: memUsages}) + sort.Sort(&sortMemWrapper{memUsages: memUsages}) for _, memUsage := range memUsages { diff --git a/cmd/admin-info-server.go b/cmd/admin-info-server.go index 1fcced2f..075d5490 100644 --- a/cmd/admin-info-server.go +++ b/cmd/admin-info-server.go @@ -372,7 +372,7 @@ func mainAdminServerInfo(ctx *cli.Context) error { } - sort.Stable(&sortInfoWrapper{infoMessages}) + sort.Sort(&sortInfoWrapper{infoMessages}) for _, s := range infoMessages { printMsg(s) diff --git a/go.mod b/go.mod index adef8698..166889f2 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/mattn/go-isatty v0.0.7 github.com/mattn/go-runewidth v0.0.5 // indirect github.com/minio/cli v1.22.0 - github.com/minio/minio v0.0.0-20191030070439-47b13cdb8083 + github.com/minio/minio v0.0.0-20191105141826-1e457dda7eed github.com/minio/minio-go/v6 v6.0.40 github.com/minio/sha256-simd v0.1.1 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index 4a2d2929..1cd871df 100644 --- a/go.sum +++ b/go.sum @@ -438,8 +438,8 @@ github.com/minio/minio v0.0.0-20190510004154-ac3b59645e92/go.mod h1:yFbQSwuA61mB github.com/minio/minio v0.0.0-20190903181048-8a71b0ec5a72/go.mod h1:H8OUaOxwJIALXHlfMpk2ESvk81dW+F7tSnrZi7rln/k= github.com/minio/minio v0.0.0-20190927193314-1c5b05c130fa/go.mod h1:47wx8w7yKTFAPw/hbmYPL4prsPivh6QuUqjTykftGgg= github.com/minio/minio v0.0.0-20191001201215-ff5bf519522f/go.mod h1:+vpJpzImdz4NCQto3r6/vF/24jWeEs5i3lsDfRmEcl4= -github.com/minio/minio v0.0.0-20191030070439-47b13cdb8083 h1:+/TZ+0p1lfB54l4GQJZNzTQu/C/ws1jowVJ8bYSde/o= -github.com/minio/minio v0.0.0-20191030070439-47b13cdb8083/go.mod h1:VFaqY+fSaSsys7EdxKShZbCq5zqGTSDUiHrmaSHK8CM= +github.com/minio/minio v0.0.0-20191105141826-1e457dda7eed h1:I2sw0vvldX4BVNk6DQYdIkd50tO2LX1/tHauj3fG6z0= +github.com/minio/minio v0.0.0-20191105141826-1e457dda7eed/go.mod h1:t5CM1NnNY1avHe6QYVhtIcNQy8mobq7+c9DhOz0g+O8= github.com/minio/minio-go v0.0.0-20190227180923-59af836a7e6d h1:gptD0/Hnam7h4Iq9D/33fscRpHfzOOUqUbH2nPw9HcU= github.com/minio/minio-go v0.0.0-20190227180923-59af836a7e6d/go.mod h1:/haSOWG8hQNx2+JOfLJ9GKp61EAmgPwRVw/Sac0NzaM= github.com/minio/minio-go v0.0.0-20190313212832-5d20267d970d/go.mod h1:/haSOWG8hQNx2+JOfLJ9GKp61EAmgPwRVw/Sac0NzaM=