@@ -22,6 +22,7 @@ var protocolTemplateContent string
22
22
var browserTemplateContent string
23
23
24
24
// Constants for template types
25
+ // Template names should not contain path separators to not to be confused with file paths
25
26
const (
26
27
MinimalTemplate = "minimal"
27
28
ProtocolTemplate = "protocol"
@@ -62,9 +63,9 @@ func NewTemplateManager(fs fsext.Fs) (*TemplateManager, error) {
62
63
}
63
64
64
65
// 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 ) {
66
67
// First check built-in templates
67
- switch templateType {
68
+ switch tpl {
68
69
case MinimalTemplate :
69
70
return tm .minimalTemplate , nil
70
71
case ProtocolTemplate :
@@ -74,48 +75,32 @@ func (tm *TemplateManager) GetTemplate(templateType string) (*template.Template,
74
75
}
75
76
76
77
// 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 )
79
80
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 )
81
82
}
82
83
83
- tmpl , err := template .New (filepath .Base (templateType )).Parse (string (content ))
84
+ tmpl , err := template .New (filepath .Base (tpl )).Parse (string (content ))
84
85
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 )
86
87
}
87
88
return tmpl , nil
88
89
}
89
90
90
91
// 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 ))
92
93
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 )
94
95
}
95
96
96
- return nil , fmt .Errorf ("invalid template type %q" , templateType )
97
+ return nil , fmt .Errorf ("invalid template type %q" , tpl )
97
98
}
98
99
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
101
102
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 , '/' )
119
104
}
120
105
121
106
// TemplateArgs represents arguments passed to templates
0 commit comments