github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-config-help.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "strings" 22 "text/tabwriter" 23 "text/template" 24 25 "github.com/fatih/color" 26 json "github.com/minio/colorjson" 27 "github.com/minio/madmin-go/v3" 28 "github.com/minio/mc/pkg/probe" 29 ) 30 31 // HelpTmpl template used by all sub-systems 32 const HelpTmpl = `{{if ne .SubSys ""}}{{colorBlueBold "KEY:"}} 33 {{if .MultipleTargets}}{{colorYellowBold .SubSys}}[:name]{{"\t"}}{{else}}{{colorYellowBold .SubSys}}{{"\t"}}{{end}}{{.Description}} 34 35 {{colorBlueBold "ARGS:"}}{{range .KeysHelp}} 36 {{if .Optional}}{{colorYellowBold .Key}}{{else}}{{colorRedBold .Key}}*{{end}}{{"\t"}}({{.Type}}){{"\t"}}{{.Description}}{{end}}{{else}}{{colorBlueBold "KEYS:"}}{{range .KeysHelp}} 37 {{colorGreenBold .Key}}{{"\t"}}{{.Description}}{{end}}{{end}}` 38 39 var funcMap = template.FuncMap{ 40 "colorBlueBold": color.New(color.FgBlue, color.Bold).SprintfFunc(), 41 "colorYellowBold": color.New(color.FgYellow, color.Bold).SprintfFunc(), 42 "colorCyanBold": color.New(color.FgCyan, color.Bold).SprintFunc(), 43 "colorRedBold": color.New(color.FgRed, color.Bold).SprintfFunc(), 44 "colorGreenBold": color.New(color.FgGreen, color.Bold).SprintfFunc(), 45 } 46 47 // HelpTemplate - captures config help template 48 var HelpTemplate = template.Must(template.New("config-help").Funcs(funcMap).Parse(HelpTmpl)) 49 50 // HelpEnvTemplate - captures config help template 51 var HelpEnvTemplate = template.Must(template.New("config-help-env").Funcs(funcMap).Parse(HelpTmpl)) 52 53 // configHelpMessage container to hold locks information. 54 type configHelpMessage struct { 55 Status string `json:"status"` 56 Value madmin.Help `json:"help"` 57 envOnly bool 58 } 59 60 // String colorized service status message. 61 func (u configHelpMessage) String() string { 62 var s strings.Builder 63 w := tabwriter.NewWriter(&s, 1, 8, 2, ' ', 0) 64 var e error 65 if !u.envOnly { 66 e = HelpTemplate.Execute(w, u.Value) 67 } else { 68 e = HelpEnvTemplate.Execute(w, u.Value) 69 } 70 fatalIf(probe.NewError(e), "Unable to initialize template writer") 71 72 w.Flush() 73 74 return s.String() 75 } 76 77 // JSON jsonified service status Message message. 78 func (u configHelpMessage) JSON() string { 79 u.Status = "success" 80 statusJSONBytes, e := json.MarshalIndent(u, "", " ") 81 fatalIf(probe.NewError(e), "Unable to marshal into JSON.") 82 83 return string(statusJSONBytes) 84 }