Skip to content

Commit eedf94c

Browse files
committed
Add helper to create log files
Not used yet. Signed-off-by: David Gageot <[email protected]>
1 parent bcc4d31 commit eedf94c

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

pkg/skaffold/logfile/logfile.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2020 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logfile
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
"regexp"
24+
)
25+
26+
// Create creates or truncates a file to be used to output logs.
27+
func Create(name string) (*os.File, error) {
28+
root := filepath.Join(os.TempDir(), "skaffold")
29+
if err := os.MkdirAll(root, 0700); err != nil {
30+
return nil, fmt.Errorf("unable to temp directory %q: %w", root, err)
31+
}
32+
33+
path := filepath.Join(root, escape(name))
34+
return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
35+
}
36+
37+
var escapeRegexp = regexp.MustCompile(`[^a-zA-Z0-9-_.]`)
38+
39+
func escape(s string) string {
40+
return escapeRegexp.ReplaceAllString(s, "-")
41+
}

pkg/skaffold/logfile/logfile_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2020 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logfile
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
"github.com/GoogleContainerTools/skaffold/testutil"
25+
)
26+
27+
func TestCreate(t *testing.T) {
28+
var tests = []struct {
29+
description string
30+
name string
31+
expectedName string
32+
}{
33+
{
34+
description: "create file",
35+
name: "logs.txt",
36+
expectedName: "logs.txt",
37+
},
38+
{
39+
description: "escape name",
40+
name: "a/name.txt",
41+
expectedName: "a-name.txt",
42+
},
43+
}
44+
45+
for _, test := range tests {
46+
testutil.Run(t, test.description, func(t *testutil.T) {
47+
file, err := Create(test.name)
48+
defer func() {
49+
file.Close()
50+
os.Remove(file.Name())
51+
}()
52+
53+
t.CheckNoError(err)
54+
t.CheckDeepEqual(filepath.Join(os.TempDir(), "skaffold", test.expectedName), file.Name())
55+
})
56+
}
57+
}
58+
59+
func TestEscape(t *testing.T) {
60+
tests := []struct {
61+
name string
62+
expected string
63+
}{
64+
{name: "img", expected: "img"},
65+
{name: "log.txt", expected: "log.txt"},
66+
{name: "project/img", expected: "project-img"},
67+
{name: "gcr.io/project/img", expected: "gcr.io-project-img"},
68+
}
69+
for _, test := range tests {
70+
testutil.Run(t, test.name, func(t *testutil.T) {
71+
escaped := escape(test.name)
72+
73+
t.CheckDeepEqual(test.expected, escaped)
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)