--- a/cmd/config.go Sun May 07 19:30:24 2017 +0200
+++ b/cmd/config.go Sun May 07 19:31:37 2017 +0200
@@ -42,6 +42,14 @@
return configDisplayToken()
},
},
+ &cobra.Command{
+ Use: "themes",
+ //Aliases: []string{},
+ Short: "Display available themes",
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return configDisplayThemes()
+ },
+ },
}
const configurationTemplate = `---
@@ -68,7 +76,7 @@
// Try to sign in if a login was provided
if viper.GetString("token") != "" || viper.GetString("login") != "" {
if err := madonLogin(); err != nil {
- errPrint("Error: could not log in: %s", err)
+ errPrint("Error: could not log in: %v", err)
os.Exit(-1)
}
}
@@ -89,7 +97,7 @@
p, err = getPrinter()
}
if err != nil {
- errPrint("Error: %s", err.Error())
+ errPrint("Error: %v", err)
os.Exit(1)
}
return p.PrintObj(gClient, nil, "")
@@ -107,8 +115,32 @@
p, err := getPrinter()
if err != nil {
- errPrint("Error: %s", err.Error())
+ errPrint("Error: %v", err)
os.Exit(1)
}
return p.PrintObj(gClient.UserToken, nil, "")
}
+
+// configDisplayThemes lists the available themes
+// It is intended for shell completion.
+func configDisplayThemes() error {
+ var p printer.ResourcePrinter
+
+ themes, err := getThemes()
+ if err != nil {
+ errPrint("Error: %v", err)
+ os.Exit(1)
+ }
+
+ if getOutputFormat() == "plain" {
+ pOptions := printer.Options{"template": `{{printf "%s\n" .}}`}
+ p, err = printer.NewPrinterTemplate(pOptions)
+ } else {
+ p, err = getPrinter()
+ }
+ if err != nil {
+ errPrint("Error: %v", err)
+ os.Exit(1)
+ }
+ return p.PrintObj(themes, nil, "")
+}
--- a/cmd/root.go Sun May 07 19:30:24 2017 +0200
+++ b/cmd/root.go Sun May 07 19:31:37 2017 +0200
@@ -47,6 +47,13 @@
__madonctl_color() {
COMPREPLY=( auto on off )
}
+__madonctl_theme() {
+ local madonctl_output out
+ # This doesn't handle spaces or special chars...
+ if out=$(madonctl config themes 2>/dev/null); then
+ COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
+ fi
+}
`
// RootCmd represents the base command when called without any subcommands
@@ -152,9 +159,12 @@
annotationOutput[cobra.BashCompCustom] = []string{"__madonctl_output"}
annotationColor := make(map[string][]string)
annotationColor[cobra.BashCompCustom] = []string{"__madonctl_color"}
+ annotationTheme := make(map[string][]string)
+ annotationTheme[cobra.BashCompCustom] = []string{"__madonctl_theme"}
RootCmd.PersistentFlags().Lookup("output").Annotations = annotationOutput
RootCmd.PersistentFlags().Lookup("color").Annotations = annotationColor
+ RootCmd.PersistentFlags().Lookup("theme").Annotations = annotationTheme
}
// initConfig reads in config file and ENV variables if set.
--- a/cmd/utils.go Sun May 07 19:30:24 2017 +0200
+++ b/cmd/utils.go Sun May 07 19:31:37 2017 +0200
@@ -95,6 +95,24 @@
return ioutil.ReadFile(name)
}
+func getThemes() ([]string, error) {
+ templDir := viper.GetString("template_directory")
+ if templDir == "" {
+ return nil, errors.New("template_directory not defined")
+ }
+ files, err := ioutil.ReadDir(filepath.Join(templDir, "themes"))
+ if err != nil {
+ return nil, errors.Wrap(err, "cannot read theme directory")
+ }
+ var tl []string
+ for _, f := range files {
+ if f.IsDir() {
+ tl = append(tl, f.Name())
+ }
+ }
+ return tl, nil
+}
+
func fileExists(filename string) bool {
if _, err := os.Stat(filename); err != nil {
return false