Skip to content

Commit

Permalink
refactor: change sample ep to support cfg struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Aug 30, 2023
1 parent a9e9ed1 commit 46d60f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"v1/scaffolds/internal/templates/api"

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"sigs.k8s.io/yaml"
)

var ApiFlags = []external.Flag{
Expand Down Expand Up @@ -59,38 +59,23 @@ func ApiCmd(pr *external.PluginRequest) external.PluginResponse {
number, _ := flags.GetInt("number")

// Update the project config with GVK
cfg := PluginConfig{}
err := yaml.Unmarshal([]byte(pr.Config), &cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}
cfg := pr.Config

// Create and append the new config info
newResource := ResourceData{
Group: "group",
Domain: "my.domain",
Version: "v1",
Kind: "Externalpluginsample",
newResource := resource.Resource{
// Fill in the GVK
GVK: resource.GVK{
Group: "group",
Domain: "my.domain",
Version: "v1",
Kind: "Externalpluginsample",
},
}
cfg.Resources = append(cfg.Resources, newResource)

updatedConfig, err := yaml.Marshal(cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}
cfg.Resources = append(cfg.Resources, newResource)

// Update the PluginResponse with the modified config string
pluginResponse.Config = string(updatedConfig)
// Update the PluginResponse with the modified config
pluginResponse.Config = cfg

apiFile := api.NewApiFile(api.WithNumber(number))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/yaml"

"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"v1/scaffolds/internal/templates"
Expand Down Expand Up @@ -61,17 +60,8 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse {
flags.Parse(pr.Args)
domain, _ := flags.GetString("domain")

// Update the project config with ProjectName
cfg := PluginConfig{}
err := yaml.Unmarshal([]byte(pr.Config), &cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}
// Update the project config with project name
cfg := pr.Config

// Get current directory as the project name
cwd, err := os.Getwd()
Expand All @@ -86,20 +76,10 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse {

dirName := filepath.Base(cwd)

cfg.ProjectName = dirName

updatedConfig, err := yaml.Marshal(cfg)
if err != nil {
return external.PluginResponse{
Error: true,
ErrorMsgs: []string{
err.Error(),
},
}
}
cfg.Name = dirName

// Update the PluginResponse with the modified config string
pluginResponse.Config = string(updatedConfig)
// Update the PluginResponse with the modified config
pluginResponse.Config = cfg

initFile := templates.NewInitFile(templates.WithDomain(domain))

Expand Down

This file was deleted.

0 comments on commit 46d60f1

Please sign in to comment.