-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ChangeLog: - Refactoring GDG template tool - Refactoring GDG CLI tool
- Loading branch information
1 parent
e4a8229
commit 48bb17b
Showing
11 changed files
with
206 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"github.com/esnet/gdg/internal/config" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
"log" | ||
"log/slog" | ||
) | ||
|
||
var showConfigCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "Show current templates configuration", | ||
Long: `Show current templates configuration`, | ||
Aliases: []string{"cfg"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
data, err := yaml.Marshal(config.Config().GetTemplateConfig()) | ||
if err != nil { | ||
log.Fatalf("unable to load template configuration: %v", err) | ||
} | ||
slog.Info("Configuration", | ||
slog.String("template-config", tplCfgFile), | ||
slog.String("gdg-config", cfgFile)) | ||
fmt.Println(string(data)) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(showConfigCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cli | ||
|
||
import ( | ||
assets "github.com/esnet/gdg/config" | ||
"github.com/esnet/gdg/internal/config" | ||
appconfig "github.com/esnet/gdg/internal/log" | ||
"github.com/esnet/gdg/internal/templating" | ||
"github.com/spf13/cobra" | ||
"log" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
var ( | ||
cfgFile string | ||
tplCfgFile string | ||
template templating.Templating | ||
rootCmd = &cobra.Command{ | ||
Use: "gdg-generate", | ||
Short: "Generates dashboard templates for use with GDG given a valid configuration", | ||
Long: `Generates dashboard templates for use with GDG given a valid configuration`, | ||
} | ||
) | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
rootCmd.CompletionOptions.DisableDefaultCmd = true | ||
|
||
rootCmd.PersistentFlags().StringP("config", "c", "", "config file (default: config/importer.yml)") | ||
rootCmd.PersistentFlags().StringP("template-config", "", "", "GDG Template configuration file override. (default: config/templates.yml)") | ||
} | ||
|
||
func initConfig() { | ||
var err error | ||
cfgFile, err = rootCmd.Flags().GetString("config") | ||
if err != nil { | ||
log.Fatal("unable to get config file") | ||
} | ||
tplCfgFile, err = rootCmd.Flags().GetString("template-config") | ||
if err != nil { | ||
log.Fatal("unable to get template config file") | ||
} | ||
|
||
defaultConfiguration, err := assets.GetFile("importer-example.yml") | ||
if err != nil { | ||
slog.Warn("unable to load default configuration, no fallback") | ||
} | ||
|
||
config.InitGdgConfig(cfgFile, defaultConfiguration) | ||
config.InitTemplateConfig(tplCfgFile) | ||
cfg := config.Config() | ||
appconfig.InitializeAppLogger(os.Stdout, os.Stderr, cfg.IsDebug()) | ||
template = templating.NewTemplate() | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
"log" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
var tplCmd = &cobra.Command{ | ||
Use: "template", | ||
Aliases: []string{"tpl", "templates"}, | ||
Short: "Templating Utilities", | ||
Long: `Templating Utilities`, | ||
} | ||
|
||
var listTemplatesCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List current templates", | ||
Long: `List current templates`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
templates := template.ListTemplates() | ||
slog.Info("Available templates for current configuration", | ||
slog.String("template-config", tplCfgFile), | ||
slog.String("gdg-config", cfgFile)) | ||
for ndx, t := range templates { | ||
slog.Info(fmt.Sprintf("%d: %s", ndx+1, t)) | ||
} | ||
}, | ||
} | ||
|
||
var generateTemplatesCmd = &cobra.Command{ | ||
Use: "generate", | ||
Aliases: []string{}, | ||
Short: "Generate current templates", | ||
Long: `Generate current templates`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
templateFilter, _ := cmd.Flags().GetString("template") | ||
payload, err := template.Generate(templateFilter) | ||
if err != nil { | ||
log.Fatal("Failed to generate templates", slog.Any("err", err)) | ||
} | ||
|
||
tableObj := table.NewWriter() | ||
tableObj.SetOutputMirror(os.Stdout) | ||
tableObj.SetStyle(table.StyleLight) | ||
|
||
tableObj.AppendHeader(table.Row{"Template Name", "Output"}) | ||
count := 0 | ||
for key, val := range payload { | ||
count += len(val) | ||
for _, file := range val { | ||
tableObj.AppendRow(table.Row{key, file}) | ||
} | ||
} | ||
slog.Info("Generate dashboards for the given templates", | ||
slog.Any("template-count", len(payload)), | ||
slog.Any("dashboard-count", count)) | ||
tableObj.Render() | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(tplCmd) | ||
tplCmd.AddCommand(listTemplatesCmd) | ||
tplCmd.AddCommand(generateTemplatesCmd) | ||
generateTemplatesCmd.PersistentFlags().StringP("template", "t", "", "Specify template name, optional. Default is to operate on all configured templates that are found.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/esnet/gdg/internal/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number of gdg-generate", | ||
Long: `Print the version number of gdg-generate`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
version.PrintVersionInfo() | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
assets "github.com/esnet/gdg/config" | ||
"github.com/esnet/gdg/internal/config" | ||
appconfig "github.com/esnet/gdg/internal/log" | ||
"github.com/esnet/gdg/internal/templating" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
flag "github.com/spf13/pflag" | ||
"gopkg.in/yaml.v3" | ||
"log" | ||
"log/slog" | ||
"os" | ||
"github.com/esnet/gdg/cmd/gdg-generate/cli" | ||
) | ||
|
||
func main() { | ||
//Using pflag over corba for now, as this should be a simple enough CLI tool | ||
var cfgName = flag.StringP("config", "c", "importer.yml", "GDG Configuration file override.") | ||
var tmpCfgName = flag.StringP("ct", "", "templates.yml", "GDG Template configuration file override.") | ||
var showTemplateCfg = flag.BoolP("show-config", "", false, "Will display the current template configuration") | ||
var listTemplates = flag.BoolP("list-templates", "", false, "List all current templates") | ||
var templateName = flag.StringP("template", "t", "", "Specify template name, optional. Default is to operate on all configured templates that are found.") | ||
flag.Parse() | ||
defaultConfiguration, err := assets.GetFile("importer-example.yml") | ||
if err != nil { | ||
slog.Warn("unable to load default configuration, no fallback") | ||
} | ||
|
||
config.InitGdgConfig(*cfgName, defaultConfiguration) | ||
config.InitTemplateConfig(*tmpCfgName) | ||
cfg := config.Config() | ||
appconfig.InitializeAppLogger(os.Stdout, os.Stderr, cfg.IsDebug()) | ||
|
||
if *showTemplateCfg { | ||
data, err := yaml.Marshal(cfg.GetTemplateConfig()) | ||
if err != nil { | ||
log.Fatalf("unable to load template configuration: %v", err) | ||
} | ||
slog.Info(fmt.Sprintf("Configuration\n%s", string(data))) | ||
return | ||
} | ||
slog.Info("Context is set to: ", slog.String("context", cfg.GetGDGConfig().ContextName)) | ||
template := templating.NewTemplate() | ||
|
||
if *listTemplates { | ||
templates := template.ListTemplates() | ||
for ndx, t := range templates { | ||
slog.Info(fmt.Sprintf("%d: %s", ndx+1, t)) | ||
} | ||
|
||
return | ||
} | ||
|
||
payload, err := template.Generate(*templateName) | ||
if err != nil { | ||
log.Fatal("Failed to generate templates", slog.Any("err", err)) | ||
} | ||
|
||
tableObj := table.NewWriter() | ||
tableObj.SetOutputMirror(os.Stdout) | ||
tableObj.SetStyle(table.StyleLight) | ||
|
||
tableObj.AppendHeader(table.Row{"Template Name", "Output"}) | ||
for key, val := range payload { | ||
for _, file := range val { | ||
tableObj.AppendRow(table.Row{key, file}) | ||
} | ||
} | ||
|
||
tableObj.Render() | ||
cli.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters