File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ package scanner
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "regexp"
7
+ "runtime"
6
8
7
9
"golang.org/x/xerrors"
8
10
"gopkg.in/yaml.v3"
@@ -15,6 +17,10 @@ import (
15
17
func createTempFile (artifact * artifacts.Artifact ) (string , error ) {
16
18
filename := fmt .Sprintf ("%s-%s-%s-*.yaml" , artifact .Namespace , artifact .Kind , artifact .Name )
17
19
20
+ if runtime .GOOS == "windows" {
21
+ //removes characters not permitted in file/directory names on Windows
22
+ filename = filenameWindowsFriendly (filename )
23
+ }
18
24
file , err := os .CreateTemp ("" , filename )
19
25
if err != nil {
20
26
return "" , xerrors .Errorf ("creating tmp file error: %w" , err )
@@ -38,3 +44,9 @@ func removeFile(filename string) {
38
44
log .Logger .Errorf ("failed to remove temp file %s: %s:" , filename , err )
39
45
}
40
46
}
47
+
48
+ var r , _ = regexp .Compile ("\\ \\ |/|:|\\ *|\\ ?|<|>" )
49
+
50
+ func filenameWindowsFriendly (name string ) string {
51
+ return r .ReplaceAllString (name , "_" )
52
+ }
Original file line number Diff line number Diff line change
1
+ package scanner
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/stretchr/testify/assert"
7
+ )
8
+
9
+ func Test_FilenameWindowsFriendly (t * testing.T ) {
10
+
11
+ tests := []struct {
12
+ name string
13
+ fileName string
14
+ want string
15
+ }{
16
+ {
17
+ name : "name with invalid char - colon" ,
18
+ fileName : `kube-system-Role-system:controller:bootstrap-signer-2934213283.yaml` ,
19
+ want : `kube-system-Role-system_controller_bootstrap-signer-2934213283.yaml` ,
20
+ },
21
+ {
22
+ name : "name with no invalid chars" ,
23
+ fileName : `kube-system-Role-system-controller-bootstrap-signer-2934213283.yaml` ,
24
+ want : `kube-system-Role-system-controller-bootstrap-signer-2934213283.yaml` ,
25
+ },
26
+ }
27
+
28
+ for _ , test := range tests {
29
+ t .Run (test .name , func (t * testing.T ) {
30
+ got := filenameWindowsFriendly (test .fileName )
31
+ assert .Equal (t , test .want , got )
32
+ })
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments