Skip to content

Commit

Permalink
feat(cli): support custom kind for k8s dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed May 7, 2024
1 parent 0c0a0f2 commit 4897d73
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
9 changes: 7 additions & 2 deletions pkg/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (a *app) newFrom(cc Command, parent Command) *cobra.Command {

cmd.Flags().BoolVarP(&showConfiguration, "list-configuration", "c", os.Getenv("ENV") == "DEV", "show configuration")

if c.i.Component != "" {
if c.i.Component != nil {
cmd.Flags().BoolVarP(&dumpK8s, "dump-k8s", "", false, "dump k8s component")
}

Expand Down Expand Up @@ -181,7 +181,12 @@ func (a *app) bindCommandFromStruct(c *C, rv reflect.Value, flags *pflag.FlagSet
n.i.Name = name
}
if component, ok := ft.Tag.Lookup("component"); ok {
n.i.Component = component
tag := parseTag(component)

n.i.Component = &Component{
Name: tag.Name,
Options: tag.Values,
}
}
continue
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/cli/cmd_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ import (
)

func (c *C) dumpK8sConfiguration(ctx context.Context, dest string) error {
if c.i.Component == "" {
if c.i.Component == nil {
return nil
}

pkgName := strings.ToLower(camelcase.LowerCamelCase(c.i.App.Name))
componentName := camelcase.LowerKebabCase(c.i.Component)
componentName := camelcase.LowerKebabCase(c.i.Component.Name)

dest = path.Join(dest, pkgName)

kind := "Deployment"

if k := c.i.Component.Options.Get("kind"); k != "" {
kind = k
}

b := bytes.NewBuffer(nil)

_, _ = fmt.Fprintf(b, `
package %s
Expand All @@ -39,13 +46,14 @@ spec: {
version: _
deploy: {
kind: "Deployment"
kind: %q
spec: replicas: _ | *1
}
`,
pkgName,
gengo.UpperCamelCase(componentName),
componentName,
kind,
)

var flagExposes []*flagVar
Expand Down
8 changes: 7 additions & 1 deletion pkg/cli/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"fmt"
"net/url"

contextx "github.com/octohelm/x/context"
)
Expand All @@ -11,7 +12,12 @@ type Info struct {
App *App
Name string
Desc string
Component string
Component *Component
}

type Component struct {
Name string
Options url.Values
}

func (info Info) InjectContext(ctx context.Context) context.Context {
Expand Down

0 comments on commit 4897d73

Please sign in to comment.