Skip to content

Commit

Permalink
Cleaning up gdg-template (#267)
Browse files Browse the repository at this point in the history
* Updating documentation

* Cleaning up gdg-template

ChangeLog:
  - Refactoring GDG template tool
  - Refactoring GDG CLI tool

* Additional documentation prepping for the v0.6.0 release.
  • Loading branch information
safaci2000 authored Mar 18, 2024
1 parent 0e945da commit 09e5db5
Show file tree
Hide file tree
Showing 32 changed files with 629 additions and 384 deletions.
82 changes: 41 additions & 41 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ version: "3"

vars:
BIN_NAME: gdg
VERSION : { sh: grep "const Version " internal/version/version.go | sed -E 's/.*"(.+)"$$/\1/' }
VERSION: { sh: grep "const Version " internal/version/version.go | sed -E 's/.*"(.+)"$$/\1/' }
GIT_COMMIT: { sh: git rev-parse HEAD }
GIT_DIRTY: { sh: test -n "`git status --porcelain`" && echo "+CHANGES" || true }
BUILD_DATE: { sh: date '+%Y-%m-%d-%H:%M:%S' }
IMAGE_NAME: "esnet/gdg"
LD_FLAGS: "-s -w -X github.com/esnet/gdg/internal/version.GitCommit={{ .GIT_COMMIT}}{{ .GIT_DIRTY}} -X github.com/esnet/gdg/internal/version.BuildDate={{ .BUILD_DATE }} "

dotenv: ['.env']
dotenv: [ '.env' ]

tasks:
default:
Expand All @@ -23,100 +23,100 @@ tasks:
- go install github.com/securego/gosec/v2/cmd/gosec@master
- go install golang.org/x/vuln/cmd/govulncheck@latest
- go install github.com/vektra/mockery/[email protected]

- go install github.com/goreleaser/[email protected]
security:
desc: "Run security scan"
cmds:
- gosec --exclude=G402,G304 ./...
lint:
desc: "Lint project, skipping test files."
cmds:
- golangci-lint run --timeout=30m --skip-dirs "(^|/)test($|/)" --skip-files "_test.go" ./...
- golangci-lint run --timeout=30m --skip-dirs "(^|/)test($|/)" --skip-files "_test.go" ./...
spellcheck:
desc: "Check Spelling across code"
cmds:
- misspell . | grep -v "website"
- misspell . | grep -v "website"
lint_tests:
desc: "Lint project, including test files."
cmds:
- golangci-lint run ./...
- golangci-lint run ./...
authors:
desc: "Building GDG"
cmds:
- echo "Authors\n=======\n" > AUTHORS.md
- "git log --raw | grep \"^Author: \" | sort | uniq | cut -d ' ' -f2 | sed 's/^/- /' >> AUTHORS.md"
silent: false
mocks:
mocks:
desc: "Re-generate Mocks"
cmds:
- rm -fr internal/service/mocks
- mockery
- mockery
linux:
desc: "Build linux binary"
cmds:
- env GOOS='linux' GOARCH='amd64' go build -ldflags "{{ .LD_FLAGS }}" -o bin/{{ .BIN_NAME }}_linux cmd/gdg/main.go
- env GOOS='linux' GOARCH='amd64' go build -ldflags "{{ .LD_FLAGS }}" -o bin/{{ .BIN_NAME }}_linux cmd/gdg/main.go
build_all:
desc: "Buiding All binaries"
desc: "Buiding All binaries"
cmds:
- task: build
- task: build_generate
build:
desc: "Buiding {{ .BIN_NAME }} {{ .VERSION }}"
desc: "Buiding {{ .BIN_NAME }} {{ .VERSION }}"
cmds:
- echo "GOPATH=${GOPATH}"
- go build -ldflags "{{ .LD_FLAGS }}" -o bin/{{ .BIN_NAME }} cmd/gdg/main.go
- go build -ldflags "{{ .LD_FLAGS }}" -o bin/{{ .BIN_NAME }} cmd/gdg/main.go
build_generate:
desc: "Buiding {{ .BIN_NAME }}-generate {{ .VERSION }}"
desc: "Buiding {{ .BIN_NAME }}-generate {{ .VERSION }}"
cmds:
- echo "GOPATH=${GOPATH}"
- go build -ldflags "{{ .LD_FLAGS }}" -o bin/{{ .BIN_NAME }}-generate cmd/gdg-generate/main.go
install:
desc: "installing {{ .BIN_NAME }} {{ .VERSION }}"
cmds:
- go build -ldflags "{{ .LD_FLAGS }}" -o bin/{{ .BIN_NAME }}-generate cmd/gdg-generate/main.go
install:
desc: "installing {{ .BIN_NAME }} {{ .VERSION }}"
cmds:
- echo "GOPATH=${GOPATH}"
- go install -ldflags "{{ .LD_FLAGS}}" cmd/gdg/main.go
silent: false
push:
push:
desc: "Pushing docker image to registry: latest {{ .VERSION }} {{ .GIT_COMMIT }}"
deps: [tag]
deps: [ tag ]
cmds:
- docker push $(IMAGE_NAME):{{ .GIT_COMMIT }}
- docker push $(IMAGE_NAME):{{ .VERSION }}
- docker push $(IMAGE_NAME):latest
clean:
desc: "clean up data"
cmds:
- "test ! -e bin/{{ .BIN_NAME }} || rm bin/{{ .BIN_NAME }}"
- "test ! -e bin/{{ .BIN_NAME }} || rm bin/{{ .BIN_NAME }}"
- "rm -fr dist/"
release-snapshot:
deps: [clean]
release-snapshot:
deps: [ clean ]
desc: "Release Snapshot"
cmds:
- goreleaser build --snapshot
release:
deps: [clean]
release:
deps: [ clean ]
desc: "TEST Release of GDG, no validation, no publish"
cmds:
- goreleaser release --skip=publish,validate

test:
desc: "test check"
cmds:
- go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./...
- go tool cover -html=coverage.out
env:
GRAFANA_INTEGRATION: "1"
TEST_TOKEN_CONFIG: "0"
desc: "test check"
cmds:
- go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./...
- go tool cover -html=coverage.out
env:
GRAFANA_INTEGRATION: "1"
TEST_TOKEN_CONFIG: "0"
test_tokens:
desc: "test Token Based Only"
cmds:
- go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./...
- go tool cover -html=coverage.out
env:
GRAFANA_INTEGRATION: "1"
TEST_TOKEN_CONFIG: "1"
desc: "test Token Based Only"
cmds:
- go test -v -coverpkg=./... -covermode=atomic -coverprofile=coverage.out ./...
- go tool cover -html=coverage.out
env:
GRAFANA_INTEGRATION: "1"
TEST_TOKEN_CONFIG: "1"

vuln_check:
desc: "Vulnerability check"
cmds:
- govulncheck ./...
desc: "Vulnerability check"
cmds:
- govulncheck ./...
8 changes: 1 addition & 7 deletions cli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@ package cli

import (
"context"
"fmt"
"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/version"
"github.com/spf13/cobra"
"log/slog"
)

func newVersionCmd() simplecobra.Commander {
return &support.SimpleCommand{
NameP: "version",
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, r *support.RootCommand, args []string) error {
slog.Info(fmt.Sprintf("Build Date: %s", version.BuildDate))
slog.Info(fmt.Sprintf("Git Commit: %s", version.GitCommit))
slog.Info(fmt.Sprintf("Version: %s", version.Version))
slog.Info(fmt.Sprintf("Go Version: %s", version.GoVersion))
slog.Info(fmt.Sprintf("OS / Arch: %s", version.OsArch))
version.PrintVersionInfo()
return nil
},
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
Expand Down
31 changes: 31 additions & 0 deletions cmd/gdg-generate/cli/config.go
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)
}
60 changes: 60 additions & 0 deletions cmd/gdg-generate/cli/root.go
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())
}
}
70 changes: 70 additions & 0 deletions cmd/gdg-generate/cli/templates.go
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.")
}
19 changes: 19 additions & 0 deletions cmd/gdg-generate/cli/version.go
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)
}
Loading

0 comments on commit 09e5db5

Please sign in to comment.