Skip to content

Commit 6ec040e

Browse files
committed
Simplify the check whether a template type is a file path
1 parent 90f2800 commit 6ec040e

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

internal/cmd/templates/templates.go

+14-29
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var protocolTemplateContent string
2222
var browserTemplateContent string
2323

2424
// Constants for template types
25+
// Template names should not contain path separators to not to be confused with file paths
2526
const (
2627
MinimalTemplate = "minimal"
2728
ProtocolTemplate = "protocol"
@@ -62,9 +63,9 @@ func NewTemplateManager(fs fsext.Fs) (*TemplateManager, error) {
6263
}
6364

6465
// GetTemplate selects the appropriate template based on the type
65-
func (tm *TemplateManager) GetTemplate(templateType string) (*template.Template, error) {
66+
func (tm *TemplateManager) GetTemplate(tpl string) (*template.Template, error) {
6667
// First check built-in templates
67-
switch templateType {
68+
switch tpl {
6869
case MinimalTemplate:
6970
return tm.minimalTemplate, nil
7071
case ProtocolTemplate:
@@ -74,48 +75,32 @@ func (tm *TemplateManager) GetTemplate(templateType string) (*template.Template,
7475
}
7576

7677
// Then check if it's a file path
77-
if isFilePath(templateType) {
78-
content, err := fsext.ReadFile(tm.fs, templateType)
78+
if isFilePath(tpl) {
79+
content, err := fsext.ReadFile(tm.fs, tpl)
7980
if err != nil {
80-
return nil, fmt.Errorf("failed to read template file %s: %w", templateType, err)
81+
return nil, fmt.Errorf("failed to read template file %s: %w", tpl, err)
8182
}
8283

83-
tmpl, err := template.New(filepath.Base(templateType)).Parse(string(content))
84+
tmpl, err := template.New(filepath.Base(tpl)).Parse(string(content))
8485
if err != nil {
85-
return nil, fmt.Errorf("failed to parse template file %s: %w", templateType, err)
86+
return nil, fmt.Errorf("failed to parse template file %s: %w", tpl, err)
8687
}
8788
return tmpl, nil
8889
}
8990

9091
// Check if there's a file with this name in current directory
91-
exists, err := fsext.Exists(tm.fs, fsext.JoinFilePath(".", templateType))
92+
exists, err := fsext.Exists(tm.fs, fsext.JoinFilePath(".", tpl))
9293
if err == nil && exists {
93-
return nil, fmt.Errorf("invalid template type %q, did you mean ./%s?", templateType, templateType)
94+
return nil, fmt.Errorf("invalid template type %q, did you mean ./%s?", tpl, tpl)
9495
}
9596

96-
return nil, fmt.Errorf("invalid template type %q", templateType)
97+
return nil, fmt.Errorf("invalid template type %q", tpl)
9798
}
9899

99-
// isFilePath checks if the given string looks like a file path
100-
// It handles both POSIX-style paths (./, ../, /) and Windows-style paths (C:\, \\, .\)
100+
// isFilePath checks if the given string looks like a file path by detecting path separators
101+
// We assume that built-in template names don't contain path separators
101102
func isFilePath(path string) bool {
102-
// Check POSIX-style paths
103-
if strings.HasPrefix(path, "./") ||
104-
strings.HasPrefix(path, "../") ||
105-
strings.HasPrefix(path, "/") {
106-
return true
107-
}
108-
109-
// Check Windows-style paths
110-
if strings.HasPrefix(path, ".\\") ||
111-
strings.HasPrefix(path, "..\\") ||
112-
strings.HasPrefix(path, "\\") ||
113-
strings.HasPrefix(path, "\\\\") || // UNC paths
114-
(len(path) >= 2 && path[1] == ':') { // Drive letter paths like C:
115-
return true
116-
}
117-
118-
return false
103+
return strings.ContainsRune(path, filepath.Separator) || strings.ContainsRune(path, '/')
119104
}
120105

121106
// TemplateArgs represents arguments passed to templates

0 commit comments

Comments
 (0)