diff --git a/cmd/new.go b/cmd/new.go index 9cd8c2a5025..4453d23b42e 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -28,7 +28,7 @@ func (c *newScriptCmd) flagSet() *pflag.FlagSet { return flags } -func (c *newScriptCmd) run(cmd *cobra.Command, args []string) error { +func (c *newScriptCmd) run(_ *cobra.Command, args []string) error { target := defaultNewScriptName if len(args) > 0 { target = args[0] @@ -46,9 +46,20 @@ func (c *newScriptCmd) run(cmd *cobra.Command, args []string) error { if err != nil { return err } - defer fd.Close() + defer func() { + if cerr := fd.Close(); cerr != nil { + if _, err := fmt.Fprintf(c.gs.Stderr, "error closing file: %v\n", cerr); err != nil { + panic(fmt.Sprintf("error writing error message to stderr: %v", err)) + } + } + }() + + tm, err := templates.NewTemplateManager() + if err != nil { + return err + } - tmpl, err := templates.GetTemplate(c.templateType) + tmpl, err := tm.GetTemplate(c.templateType) if err != nil { return err } @@ -62,7 +73,10 @@ func (c *newScriptCmd) run(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(c.gs.Stdout, "New script created: %s (%s template).\n", target, c.templateType) + if _, err := fmt.Fprintf(c.gs.Stdout, "New script created: %s (%s template).\n", target, c.templateType); err != nil { + return err + } + return nil } @@ -72,10 +86,10 @@ func getCmdNewScript(gs *state.GlobalState) *cobra.Command { exampleText := getExampleText(c.gs, ` # Create a minimal k6 script $ {{.}} new --template minimal - + # Overwrite an existing file with a protocol-based script $ {{.}} new -f --template protocol test.js - + # Create a cloud-ready script with a specific project ID $ {{.}} new --project-id 12315`[1:]) diff --git a/cmd/new_test.go b/cmd/new_test.go index 2a0e6fd05fd..f451889feda 100644 --- a/cmd/new_test.go +++ b/cmd/new_test.go @@ -105,6 +105,7 @@ func TestNewScriptCmd_InvalidTemplateType(t *testing.T) { newRootCommand(ts.GlobalState).execute() assert.Contains(t, ts.Stderr.String(), "invalid template type") } + func TestNewScriptCmd_ProjectID(t *testing.T) { t.Parallel() diff --git a/cmd/newtemplates/templates.go b/cmd/newtemplates/templates.go index 4b14ffc8a6f..cb421bf9e28 100644 --- a/cmd/newtemplates/templates.go +++ b/cmd/newtemplates/templates.go @@ -7,8 +7,6 @@ import ( "text/template" ) -// Embed templates -// //go:embed minimal.js var minimalTemplateContent string @@ -18,34 +16,57 @@ var protocolTemplateContent string //go:embed browser.js var browserTemplateContent string -// Pre-parse templates -var ( - MinimalScriptTemplate = template.Must(template.New("minimal").Parse(minimalTemplateContent)) - ProtocolScriptTemplate = template.Must(template.New("protocol").Parse(protocolTemplateContent)) - BrowserScriptTemplate = template.Must(template.New("browser").Parse(browserTemplateContent)) - DefaultNewScriptName = "script.js" -) +// TemplateManager manages the pre-parsed templates +type TemplateManager struct { + minimalTemplate *template.Template + protocolTemplate *template.Template + browserTemplate *template.Template +} -// TemplateArgs represents arguments passed to templates -type TemplateArgs struct { - ScriptName string - ProjectID string +// NewTemplateManager initializes a new TemplateManager with parsed templates +func NewTemplateManager() (*TemplateManager, error) { + minimalTmpl, err := template.New("minimal").Parse(minimalTemplateContent) + if err != nil { + return nil, fmt.Errorf("failed to parse minimal template: %w", err) + } + + protocolTmpl, err := template.New("protocol").Parse(protocolTemplateContent) + if err != nil { + return nil, fmt.Errorf("failed to parse protocol template: %w", err) + } + + browserTmpl, err := template.New("browser").Parse(browserTemplateContent) + if err != nil { + return nil, fmt.Errorf("failed to parse browser template: %w", err) + } + + return &TemplateManager{ + minimalTemplate: minimalTmpl, + protocolTemplate: protocolTmpl, + browserTemplate: browserTmpl, + }, nil } // GetTemplate selects the appropriate template based on the type -func GetTemplate(templateType string) (*template.Template, error) { +func (tm *TemplateManager) GetTemplate(templateType string) (*template.Template, error) { switch templateType { case "minimal": - return MinimalScriptTemplate, nil + return tm.minimalTemplate, nil case "protocol": - return ProtocolScriptTemplate, nil + return tm.protocolTemplate, nil case "browser": - return BrowserScriptTemplate, nil + return tm.browserTemplate, nil default: return nil, fmt.Errorf("invalid template type: %s", templateType) } } +// TemplateArgs represents arguments passed to templates +type TemplateArgs struct { + ScriptName string + ProjectID string +} + // ExecuteTemplate applies the template with provided arguments and writes to the provided writer func ExecuteTemplate(w io.Writer, tmpl *template.Template, args TemplateArgs) error { return tmpl.Execute(w, args)