-
Notifications
You must be signed in to change notification settings - Fork 232
/
view.go
72 lines (62 loc) · 1.71 KB
/
view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package config
import (
"bytes"
"fmt"
"io"
"os"
"sort"
"text/template"
"github.com/code-ready/crc/pkg/crc/config"
"github.com/spf13/cobra"
)
const (
DefaultConfigViewFormat = "- {{.ConfigKey | printf \"%-38s\"}}: {{.ConfigValue}}"
)
var configViewFormat string
type configViewTemplate struct {
ConfigKey string
ConfigValue interface{}
}
func configViewCmd(config config.Storage) *cobra.Command {
configViewCmd := &cobra.Command{
Use: "view",
Short: "Display all assigned crc configuration properties",
Long: `Displays all assigned crc configuration properties and their values.`,
RunE: func(cmd *cobra.Command, args []string) error {
tmpl, err := determineTemplate(configViewFormat)
if err != nil {
return err
}
return runConfigView(config.AllConfigs(), tmpl, os.Stdout)
},
}
configViewCmd.Flags().StringVar(&configViewFormat, "format", DefaultConfigViewFormat,
`Go template format to apply to the configuration file. For more information about Go templates, see: https://golang.org/pkg/text/template/`)
return configViewCmd
}
func determineTemplate(tempFormat string) (*template.Template, error) {
tmpl, err := template.New("view").Parse(tempFormat)
if err != nil {
return nil, err
}
return tmpl, nil
}
func runConfigView(cfg map[string]config.SettingValue, tmpl *template.Template, writer io.Writer) error {
var lines []string
for k, v := range cfg {
if v.IsDefault {
continue
}
viewTmplt := configViewTemplate{k, v.AsString()}
var buffer bytes.Buffer
if err := tmpl.Execute(&buffer, viewTmplt); err != nil {
return err
}
lines = append(lines, buffer.String())
}
sort.Strings(lines)
for _, line := range lines {
fmt.Fprintln(writer, line)
}
return nil
}