Skip to content

Commit

Permalink
Make linter happy
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel González Lopes <[email protected]>
  • Loading branch information
dgzlopes committed Jan 3, 2025
1 parent 6423d09 commit 1d8d321
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
26 changes: 20 additions & 6 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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:])

Expand Down
1 change: 1 addition & 0 deletions cmd/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
55 changes: 38 additions & 17 deletions cmd/newtemplates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"text/template"
)

// Embed templates
//
//go:embed minimal.js
var minimalTemplateContent string

Expand All @@ -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)
Expand Down

0 comments on commit 1d8d321

Please sign in to comment.