Skip to content

Commit

Permalink
Merge pull request #69 from gchiesa/61-support-for-list-rename-delete…
Browse files Browse the repository at this point in the history
…-ska-configs

feat: ska config command, package and output rendering
  • Loading branch information
gchiesa authored Oct 22, 2024
2 parents c7ae7fd + bc11ac2 commit 7b76246
Show file tree
Hide file tree
Showing 24 changed files with 624 additions and 180 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/go-ci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
name: Golangci-lint
runs-on: ubuntu-latest
env:
GO_VERSION: '~1.22'
GO_VERSION: '~1.23'
steps:
- uses: actions/checkout@v3
- name: Set up Go
Expand All @@ -23,7 +23,6 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.56
version: v1.61
args: --config .golang-ci.yml
github_token: ${{ secrets.GITHUB_TOKEN }}
reporter: github-pr-check
github-token: ${{ secrets.GITHUB_TOKEN }}
25 changes: 13 additions & 12 deletions .golang-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ run:
tests: false
timeout: 240s
skip-dirs-use-default: true
skip-dirs:
- ^vendor$
- ^go/pkg/mod/*
- ../../../../go/pkg/mod/*
- ../../../../.gobrew/*
- .*_test\.go$
skip-files:
- .*\.pb\.go
- .*_generated\.go
linters-settings:
depguard:
rules:
Expand All @@ -22,7 +13,7 @@ linters-settings:
- pkg: "notexist"
desc: "notexist is not allowed or blacklisted"
govet:
check-shadowing: true
check-shadowing: false
gocyclo:
min-complexity: 15
maligned:
Expand Down Expand Up @@ -66,7 +57,6 @@ linters:
- gosec
- gosimple
- nakedret
- exportloopref
- staticcheck
- stylecheck
- typecheck
Expand All @@ -79,6 +69,15 @@ linters:
issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-files:
- .*\.pb\.go
- .*_generated\.go
exclude-dirs:
- ^vendor$
- ^go/pkg/mod/*
- ../../../../go/pkg/mod/*
- ../../../../.gobrew/*
- .*_test\.go$
exclude-rules:
- linters:
- gosec
Expand All @@ -103,4 +102,6 @@ issues:
- gosec
text: G204
output:
format: line-number
formats:
- format: colored-line-number
path: stdout
54 changes: 54 additions & 0 deletions cmd/config-delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"context"
"errors"
"fmt"
"github.com/apex/log"
"github.com/gchiesa/ska/pkg/skaffolder"
"github.com/gchiesa/ska/pkg/util"
"github.com/manifoldco/promptui"
)

type ConfigDeleteCmd struct {
Name string `arg:"-n,--name,required" help:"The name of the named configuration to delete"`
AutoApprove bool `arg:"-y,--auto-approve" help:"Skip the confirmation prompt"`
}

func (c *ConfigDeleteCmd) Execute(ctx context.Context) error {
ska := skaffolder.NewSkaConfigTask(ctx.Value(configFolderPath("path")).(string))

lastUpdate, err := ska.QueryNamedConfigJSON(c.Name, "{.State.LastUpdate}") // nolint:govet
if err != nil {
return err
}
var result = make([]ConfigListResultItem, 0)
result = append(result, ConfigListResultItem{
NamedConfig: c.Name,
LastUpdate: lastUpdate,
})
if !c.AutoApprove {
output, err := util.RenderWithOutputFormat(result, "table")
if err != nil {
return err
}
fmt.Println(string(output))
p := promptui.Prompt{
Label: fmt.Sprintf("Do you really want to delete this configuration named %s", c.Name),
IsConfirm: true,
}
response, err := p.Run()
if err != nil {
if errors.Is(err, promptui.ErrAbort) {
log.Infof("You responded: %s, so not proceeding further.", response)
return nil
}
return err
}
}
if err := ska.DeleteConfig(c.Name); err != nil {
return err
}
log.Infof("Deleted configuration: %s", c.Name)
return nil
}
44 changes: 44 additions & 0 deletions cmd/config-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"context"
"fmt"
"github.com/gchiesa/ska/pkg/skaffolder"
"github.com/gchiesa/ska/pkg/util"
)

type ConfigListCmd struct {
}

type ConfigListResultItem struct {
NamedConfig string `json:"NamedConfig" csv:"NamedConfig"`
LastUpdate string `json:"LastUpdate" csv:"LastUpdate"`
}

func (c *ConfigListCmd) Execute(ctx context.Context) error {
ska := skaffolder.NewSkaConfigTask(ctx.Value(configFolderPath("path")).(string))

namedConfigs, err := ska.ListNamedConfigs()
if err != nil {
return err
}
result := make([]ConfigListResultItem, 0, len(namedConfigs))
for _, namedConfig := range namedConfigs {
lastUpdate, err := ska.QueryNamedConfigJSON(namedConfig, "{.State.LastUpdate}") // nolint:govet
if err != nil {
return err
}
result = append(result, ConfigListResultItem{
NamedConfig: namedConfig,
LastUpdate: lastUpdate,
})
}

outputFormat := ctx.Value(consoleOutputFormat("output-format")).(string)
output, err := util.RenderWithOutputFormat(result, outputFormat)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
20 changes: 20 additions & 0 deletions cmd/config-rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"context"
"github.com/gchiesa/ska/pkg/skaffolder"
)

type ConfigRenameCmd struct {
Name string `arg:"-o,--name,required" help:"The name of the named configuration to rename"`
NewName string `arg:"-n,--new-name,required" help:"The new name to give to the named configuration"`
}

func (c *ConfigRenameCmd) Execute(ctx context.Context) error {
ska := skaffolder.NewSkaConfigTask(ctx.Value(configFolderPath("path")).(string))

if err := ska.RenameNamedConfig(c.Name, c.NewName); err != nil {
return err
}
return nil
}
37 changes: 37 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"context"
"fmt"
"github.com/apex/log"
)

type ConfigCmd struct {
*ConfigListCmd `arg:"subcommand:list"`
*ConfigRenameCmd `arg:"subcommand:rename"`
*ConfigDeleteCmd `arg:"subcommand:delete"`
FolderPath string `arg:"-p,--path,required" help:"Local path where the .ska-config folder is located"`
}

type configFolderPath string

func (c *ConfigCmd) Execute(ctx context.Context) error {
configCtx := context.WithValue(ctx, configFolderPath("path"), c.FolderPath)
switch {
case c.ConfigListCmd != nil:
if err := args.ConfigCmd.ConfigListCmd.Execute(configCtx); err != nil {
log.Fatalf("error executing config list command: %v", err)
}
case c.ConfigRenameCmd != nil:
if err := args.ConfigCmd.ConfigRenameCmd.Execute(configCtx); err != nil {
log.Fatalf("error executing config rename command: %v", err)
}
case c.ConfigDeleteCmd != nil:
if err := args.ConfigCmd.ConfigDeleteCmd.Execute(configCtx); err != nil {
log.Fatalf("error executing config delete command: %v", err)
}
default:
fmt.Println("no subcommand specified, please use the --help flag to check available commands")
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type CreateCmd struct {
}

func (c *CreateCmd) Execute(ctx context.Context) error {
options := &skaffolder.SkaOptions{
options := &skaffolder.SkaTaskOptions{
NonInteractive: c.NonInteractive,
Engine: ctx.Value(contextEngineKey("engine")).(templateprovider.TemplateType),
}
ska := skaffolder.NewSkaCreate(
ska := skaffolder.NewSkaCreateTask(
c.TemplateURI,
c.DestinationPath,
c.NamedConfig,
Expand Down
21 changes: 15 additions & 6 deletions cmd/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ const githubRepo = "https://github.com/gchiesa/ska"
var commandVersion = "development"

type arguments struct {
CreateCmd *CreateCmd `arg:"subcommand:create"`
UpdateCmd *UpdateCmd `arg:"subcommand:update"`
Debug bool `arg:"-d"`
JSONOutput bool `arg:"-j,--json" help:"Enable JSON output for logging"`
Engine string `arg:"--engine" default:"sprig" help:"Template engine to use (sprig or jinja)"`
CreateCmd *CreateCmd `arg:"subcommand:create"`
UpdateCmd *UpdateCmd `arg:"subcommand:update"`
ConfigCmd *ConfigCmd `arg:"subcommand:config"`
OutputFormat string `arg:"--format" default:"table" help:"The format for output. It can be: csv,json,markdown,table"`
Debug bool `arg:"-d"`
JSONOutput bool `arg:"-j,--json" help:"Enable JSON output for logging"`
Engine string `arg:"--engine" default:"sprig" help:"Template engine to use (sprig or jinja)"`
}

type contextEngineKey string
type consoleOutputFormat string

var args arguments

func Execute(version string) error {
commandVersion = version
log.SetHandler(cli.New(os.Stderr))
log.SetLevel(log.InfoLevel)

var args arguments
arg.MustParse(&args)

if args.Debug {
Expand All @@ -47,6 +51,7 @@ func Execute(version string) error {

ctx := context.TODO()
ctx = context.WithValue(ctx, contextEngineKey("engine"), templateprovider.GetTypeFromString(args.Engine))
ctx = context.WithValue(ctx, consoleOutputFormat("output-format"), args.OutputFormat)
switch {
case args.CreateCmd != nil:
if err := args.CreateCmd.Execute(ctx); err != nil {
Expand All @@ -56,6 +61,10 @@ func Execute(version string) error {
if err := args.UpdateCmd.Execute(ctx); err != nil {
log.Fatalf("error executing update command: %v", err)
}
case args.ConfigCmd != nil:
if err := args.ConfigCmd.Execute(ctx); err != nil {
log.Fatalf("error executing config command: %v", err)
}
default:
fmt.Println("no subcommand specified, please use the --help flag to check available commands")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type UpdateCmd struct {
}

func (c *UpdateCmd) Execute(ctx context.Context) error {
options := &skaffolder.SkaOptions{
options := &skaffolder.SkaTaskOptions{
NonInteractive: c.NonInteractive,
Engine: ctx.Value(contextEngineKey("engine")).(templateprovider.TemplateType),
}
ska := skaffolder.NewSkaUpdate(
ska := skaffolder.NewSkaUpdateTask(
c.FolderPath,
c.NamedConfig,
c.Variables,
Expand Down
Loading

0 comments on commit 7b76246

Please sign in to comment.