From d24de2233a99979bc58096259e56e4d9a1a9ff38 Mon Sep 17 00:00:00 2001 From: Morlay Date: Tue, 7 May 2024 15:52:58 +0800 Subject: [PATCH] feat(cli): support custom kind for k8s dumper --- cuepkg/component/example/server.cue | 16 ++++++++-------- pkg/cli/app.go | 9 +++++++-- pkg/cli/cmd_dump.go | 23 +++++++++++++++++------ pkg/cli/info.go | 8 +++++++- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/cuepkg/component/example/server.cue b/cuepkg/component/example/server.cue index 4b5056f..b8be025 100755 --- a/cuepkg/component/example/server.cue +++ b/cuepkg/component/example/server.cue @@ -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 diff --git a/pkg/cli/app.go b/pkg/cli/app.go index f555f98..dff6362 100644 --- a/pkg/cli/app.go +++ b/pkg/cli/app.go @@ -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") } @@ -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 } diff --git a/pkg/cli/cmd_dump.go b/pkg/cli/cmd_dump.go index 1b08332..0a12bb8 100644 --- a/pkg/cli/cmd_dump.go +++ b/pkg/cli/cmd_dump.go @@ -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 @@ -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 diff --git a/pkg/cli/info.go b/pkg/cli/info.go index 5db5ed1..5fdd6c5 100644 --- a/pkg/cli/info.go +++ b/pkg/cli/info.go @@ -3,6 +3,7 @@ package cli import ( "context" "fmt" + "net/url" contextx "github.com/octohelm/x/context" ) @@ -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 {