-
Notifications
You must be signed in to change notification settings - Fork 18
/
create.go
87 lines (75 loc) · 3.42 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// Package ffs contains functions that manipulate file system
package ffs
import (
"os"
"path/filepath"
"github.com/onsi/gomega"
)
// CreateBuildContext creates a directory which contains a Dockerfile with the specified content and returns the path to the directory.
// It is the caller's responsibility to remove the directory when it is no longer needed.
func CreateBuildContext(dockerfile string) string {
return filepath.Dir(CreateTempFile("Dockerfile", dockerfile))
}
// WriteFile writs data to the file specified by name. The file will be created if not existing.
func WriteFile(name string, data string) {
err := os.WriteFile(name, []byte(data), 0o644)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}
// CreateTarFilePath creates a directory and a tar file path appended to the directory and returns the tar file path.
// It is the caller's responsibility to remove the directory when it is no longer needed.
// TODO: replace with CreateTempDir.
func CreateTarFilePath() string {
tempDir := CreateTempDir("finch-test-save")
tarFilePath := filepath.Join(tempDir, "test.tar")
return tarFilePath
}
// CreateComposeYmlContext creates a temp directory along with a docker-compose.yml file.
// It is the caller's responsibility to remove the directory when it is no longer needed.
func CreateComposeYmlContext(composeYmlContent string) (string, string) {
composeFileName := "docker-compose.yml"
tempDir := CreateTempDir("finch-compose")
composeFilePath := filepath.Join(tempDir, composeFileName)
WriteFile(composeFilePath, composeYmlContent)
return tempDir, composeFilePath
}
// CreateTempFile creates a temp directory which contains a temp file and returns the path to the temp file.
// It is the caller's responsibility to remove the directory when it is no longer needed.
func CreateTempFile(filename string, content string) string {
tempDir := CreateTempDir("finch-test")
filepath := filepath.Join(tempDir, filename)
WriteFile(filepath, content)
return filepath
}
// CreateTempDir creates a temp directory and returns the path of the created directory.
// It is the caller's responsibility to remove the directory when it is no longer needed.
func CreateTempDir(directoryPrefix string) string {
homeDir, err := os.UserHomeDir()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
tempDir, err := os.MkdirTemp(homeDir, directoryPrefix)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
return tempDir
}
// CreateNestedDir creates a nested directory and returns the path of the created directory.
// It is the caller's responsibility to remove the directory when it is no longer needed.
func CreateNestedDir(dirPath string) string {
homeDir, err := os.UserHomeDir()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
fullPath := filepath.Join(homeDir, dirPath)
err = os.MkdirAll(fullPath, 0o740)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
return fullPath
}
// CreateFilePathInHome creates a filepath and returns the path.
func CreateFilePathInHome(dirPath string) string {
homeDir, err := os.UserHomeDir()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
fullPath := filepath.Join(homeDir, dirPath)
return fullPath
}
// DeleteDirectory deletes the directory including nested directories.
func DeleteDirectory(directoryPath string) {
err := os.RemoveAll(directoryPath)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
}