Fix config history to list in git style format (#2951)

```
~ mc admin config history list -n 2 myminio
RestoreId: df0ebb1e-69b0-4043-b9dd-ab54508f2897
Date: Mon, 04 Nov 2019 17:27:27 GMT

region name="us-east-1" state="on"
region name="us-east-1" state="on"
region name="us-east-1" state="on"
region name="us-east-1" state="on"

RestoreId: ecc6873a-0ed3-41f9-b03e-a2a1bab48b5f
Date: Mon, 04 Nov 2019 17:28:23 GMT

region name=us-east-1 state=off
```
This commit is contained in:
Harshavardhana
2019-11-05 14:41:52 -08:00
committed by kannappanr
parent 71f5a7568f
commit e0c6fabda6
7 changed files with 100 additions and 30 deletions
+46 -10
View File
@@ -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
+48 -14
View File
@@ -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
+1 -1
View File
@@ -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 != "" {
+1 -1
View File
@@ -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 {
+1 -1
View File
@@ -372,7 +372,7 @@ func mainAdminServerInfo(ctx *cli.Context) error {
}
sort.Stable(&sortInfoWrapper{infoMessages})
sort.Sort(&sortInfoWrapper{infoMessages})
for _, s := range infoMessages {
printMsg(s)
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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=