62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/wild-cloud/wild-cli/internal/config"
|
|
"github.com/wild-cloud/wild-cli/internal/environment"
|
|
)
|
|
|
|
func newCompileCommand() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "compile",
|
|
Short: "Compile template from stdin",
|
|
Long: `Compile a template from stdin using Wild Cloud configuration context.
|
|
|
|
This command reads template content from stdin and processes it using the
|
|
current project's config.yaml and secrets.yaml as context.
|
|
|
|
Examples:
|
|
echo 'Hello {{.config.cluster.name}}' | wild template compile
|
|
cat template.yml | wild template compile`,
|
|
RunE: runCompileTemplate,
|
|
}
|
|
}
|
|
|
|
func runCompileTemplate(cmd *cobra.Command, args []string) error {
|
|
// Initialize environment
|
|
env := environment.New()
|
|
if err := env.RequiresProject(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create config manager
|
|
mgr := config.NewManager(env.ConfigPath(), env.SecretsPath())
|
|
|
|
// Create template engine
|
|
engine, err := config.NewTemplateEngine(mgr)
|
|
if err != nil {
|
|
return fmt.Errorf("creating template engine: %w", err)
|
|
}
|
|
|
|
// Read template from stdin
|
|
templateContent, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
return fmt.Errorf("reading template from stdin: %w", err)
|
|
}
|
|
|
|
// Process template
|
|
result, err := engine.Process(string(templateContent))
|
|
if err != nil {
|
|
return fmt.Errorf("processing template: %w", err)
|
|
}
|
|
|
|
// Output result
|
|
fmt.Print(result)
|
|
return nil
|
|
}
|