-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.go
92 lines (81 loc) · 2.77 KB
/
util.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
88
89
90
91
92
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
)
const sanitisedWindowsPathPattern = "/%s%s"
// SanitisePath takes an absolute path as provided by filepath.Abs() and
// makes it ready to be passed to Docker based on the current OS. So far
// the only OS format that requires transforming is Windows which is provided
// in the form 'C:\some\path' but Docker requires '/c/some/path'.
func SanitisePath(path string, platform string) string {
sanitised := path
if platform == "windows" {
windowsPathPattern := regexp.MustCompile("^([A-Za-z]):(.*)")
match := windowsPathPattern.FindStringSubmatch(path)
driveLetter := strings.ToLower(match[1])
pathRemainder := strings.Replace(match[2], "\\", "/", -1)
sanitised = fmt.Sprintf(sanitisedWindowsPathPattern, driveLetter, pathRemainder)
}
return sanitised
}
// RetrievePath takes an array whose first element may contain an overridden
// path and converts either this, or the default of "." to an absolute path
// using Go's file utilities. This is then passed to SanitisedPath with the
// current OS to get it into a Docker ready format.
func RetrievePath(targetDirs []string) string {
path := "."
if len(targetDirs) > 0 {
path = targetDirs[0]
}
absPath, _ := filepath.Abs(path)
return SanitisePath(absPath, runtime.GOOS)
}
// AddPrefix takes a string slice and returns a new string slice
// with the supplied prefix inserted before every string in the
// original slice.
func AddPrefix(inSlice []string, prefix string) []string {
var outSlice []string
for _, option := range inSlice {
outSlice = append(outSlice, []string{prefix, option}...)
}
return outSlice
}
// JoinStringSlices takes an arbitrary number of string slices
// and concatenates them in the order supplied.
func JoinStringSlices(slices ...[]string) []string {
var outSlice []string
for _, slice := range slices {
outSlice = append(outSlice, slice...)
}
return outSlice
}
// ExtractFileExtension extracts the extension from a filename. This is defined
// as the remainder of the string after the last '.'.
func ExtractFileExtension(filename string) string {
patternPermission := regexp.MustCompile(`.*\.(.*):.*`)
permissionMatch := patternPermission.FindStringSubmatch(filename)
if len(permissionMatch) > 0 {
return permissionMatch[1]
}
patternFilename := regexp.MustCompile(`.*\.(.*)`)
return patternFilename.FindStringSubmatch(filename)[1]
}
// WriteFile writes a file.
func WriteFile(filename string, content []byte) {
if err := ioutil.WriteFile(filename, content, 0644); err != nil {
log.Fatalf("Unable to write %s\n%q", filename, err)
}
}
// DeleteFile deletes a file.
func DeleteFile(filename string) {
if err := os.Remove(filename); err != nil {
log.Fatalf("Unable to delete %s\n%q", filename, err)
}
}