Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render extra files for single entities #108

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions cmd/swctl/cmd_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error {
// list all entities
if len(args) == 0 {
color.Fprintf(cli.Out(), "Listing <bold>%d</> loaded entities:\n\n", len(cli.Entities()))

for _, e := range cli.Entities() {
name := e.Name
desc := ""
Expand Down Expand Up @@ -171,6 +172,12 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error {
configLines := strings.Count(entity.Config, "\n")
color.Fprintf(cli.Out(), "<lightWhite>CONFIG</>: %s %s\n", color.White.Sprintf("%d lines", configLines), color.Gray.Sprintf("(%d bytes)", configLen))
}
color.Fprintf(cli.Out(), "<lightWhite>FILES</>:\n")
for _, f := range entity.Files {
contentLen := len(f.Content)
contentLines := strings.Count(f.Content, "\n")
color.Fprintf(cli.Out(), " %s: %s %s\n", color.Bold.Sprint(f.Name), color.White.Sprintf("%d lines", contentLines), color.Gray.Sprintf("(%d bytes)", contentLen))
}
return nil
}

Expand Down Expand Up @@ -214,6 +221,9 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error {
if opts.Count > 1 && entity.Single {
return fmt.Errorf("count must be 1 for entity with single instance")
}
if len(entity.Files) > 0 && !entity.Single {
return fmt.Errorf("no files can be defined for multi instance entity")
}

// prepare config
mainConf, err := client.NewDynamicConfig(allModels())
Expand Down Expand Up @@ -254,7 +264,7 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error {
}

// render config template
rawConf, err := renderEntityConfig(entity, vars)
rawConf, err := renderEntityTemplate(entity.Config, vars)
if err != nil {
return fmt.Errorf("failed to render config (idx: %v): %w", i, err)
}
Expand All @@ -279,6 +289,19 @@ func runManageCmd(cli Cli, opts ManageOptions, args []string) error {
if err := mergeConfigs(mainConf, conf); err != nil {
return fmt.Errorf("merging configs failed: %w", err)
}

for _, f := range entity.Files {
rawData, err := renderEntityTemplate(f.Content, vars)
if err != nil {
return fmt.Errorf("failed to render file (%v): %w", f.Name, err)
}

logrus.Tracef(" - final vars:\n%s\nraw file %s:\n%v", yamlTmpl(vars), f.Name, rawData)

if err := os.WriteFile(f.Name, []byte(rawData), 0666); err != nil {
return fmt.Errorf("failed to write file (%v): %w", f.Name, err)
}
}
}

var finalConf protoreflect.ProtoMessage
Expand Down Expand Up @@ -507,8 +530,8 @@ func renderTmpl(t string, data any) (string, error) {
return b.String(), nil
}

func renderEntityConfig(e Entity, evars map[string]string) (string, error) {
tmpl, err := interpolateStr(e.Config, evars)
func renderEntityTemplate(cfg string, evars map[string]string) (string, error) {
tmpl, err := interpolateStr(cfg, evars)
if err != nil {
return "", err
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/swctl/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ type Entity struct {
Vars []EntityVar `json:"vars"`
Config string `json:"config"`
Single bool `json:"single"`
Files []ExtraFile `json:"files"`
}

type ExtraFile struct {
Name string `json:"name"`
Content string `json:"content"`
}

func (e Entity) GetName() string {
Expand Down