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 d24de22
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
16 changes: 8 additions & 8 deletions cuepkg/component/example/server.cue
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
spec: {
version: _

deploy: {
kind: "Deployment"
spec: replicas: _ | *1
}
deploy: kind: "Deployment"

deploy: spec: replicas: _ | *1

config: EXAMPLE_LOG_LEVEL: string | *"info"
config: EXAMPLE_LOG_FILTER: string | *"Always"
config: EXAMPLE_TRACE_COLLECTOR_ENDPOINT: string | *""
config: EXAMPLE_SERVER_ENABLE_DEBUG: string | *"false"
config: EXAMPLE_LOG_LEVEL: string | *"info"
config: EXAMPLE_TRACE_COLLECTOR_ENDPOINT: string | *""
config: EXAMPLE_METRIC_COLLECTOR_ENDPOINT: string | *""
config: EXAMPLE_METRIC_COLLECT_INTERVAL_SECONDS: string | *"0"
config: EXAMPLE_SERVER_ENABLE_DEBUG: string | *"false"

services: "#": ports: containers."server".ports

Expand Down
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
23 changes: 17 additions & 6 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 @@ -38,16 +45,20 @@ metadata: {
spec: {
version: _
deploy: {
kind: "Deployment"
spec: replicas: _ | *1
}
deploy: kind: %q
`,
pkgName,
gengo.UpperCamelCase(componentName),
componentName,
kind,
)

if kind == "Deployment" {
_, _ = fmt.Fprintf(b, `
deploy: spec: replicas: _ | *1
`)
}

var flagExposes []*flagVar

i := 0
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 d24de22

Please sign in to comment.