/
paths.go
160 lines (129 loc) · 3.88 KB
/
paths.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package paths
import (
"errors"
"fmt"
"go/build"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
// slashSplitter - used to split strings, with the same output regardless of leading or trailing slashes
// e.g - strings.FieldsFunc("/one/two/three/", f) == strings.FieldsFunc("/one/two/three", f) == strings.FieldsFunc("one/two/three", f) == ["one" "two" "three"]
func slashSplitter(c rune) bool {
return c == '/'
}
// SplitPath - splits a path into its component parts, ignoring leading or trailing slashes.
// e.g - SplitPath("/one/two/three/") == SplitPath("/one/two/three") == SplitPath("one/two/three") == ["one" "two" "three"]
func SplitPath(p string) []string {
return strings.FieldsFunc(p, slashSplitter)
}
// NitricHomeDir gets the nitric home directory
func NitricHomeDir() string {
nitricHomeEnv := os.Getenv("NITRIC_HOME")
if nitricHomeEnv != "" {
return nitricHomeEnv
}
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
return filepath.Join(dirname, ".nitric")
}
// NitricProviderDir returns the directory to place provider deployment binaries.
func NitricProviderDir() string {
return filepath.Join(NitricHomeDir(), "providers")
}
// NitricTemplatesDir returns the directory to place template related data.
func NitricTemplatesDir() string {
return filepath.Join(NitricHomeDir(), "store")
}
func NitricStacksDir() (string, error) {
homeDir := NitricHomeDir()
stacksDir := path.Join(homeDir, "stacks")
// ensure .nitric exists
err := os.MkdirAll(stacksDir, os.ModePerm)
if err != nil {
return "", err
}
return stacksDir, nil
}
// NitricConfigDir returns the directory to find configuration.
func NitricConfigDir() string {
if runtime.GOOS == "linux" {
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
return filepath.Join(dirname, ".config", "nitric")
}
return NitricHomeDir()
}
func NitricLocalPassphrasePath() string {
return filepath.Join(NitricHomeDir(), ".local-stack-pass")
}
// NitricLogDir returns the directory to find log files.
func NitricLogDir(stackPath string) string {
return filepath.Join(stackPath, ".nitric")
}
// NewNitricLogFile returns a path to a unique log file that does not exist.
func NewNitricLogFile(stackPath string) (string, error) {
logDir := NitricLogDir(stackPath)
// ensure .nitric exists
err := os.MkdirAll(logDir, os.ModePerm)
if err != nil {
return "", err
}
tf, err := os.CreateTemp(logDir, "run-*.log")
if err != nil {
return "", err
}
tf.Close()
return tf.Name(), nil
}
// NitricHistoryFile returns a path to a request history file, making one if it doesn't exist
func NitricHistoryFile(stackPath string, historyType string) (string, error) {
logDir := NitricLogDir(stackPath)
fileName := filepath.Clean(fmt.Sprintf("%s/history-%s.json", logDir, historyType))
// ensure .nitric exists
err := os.MkdirAll(logDir, os.ModePerm)
if err != nil {
return "", err
}
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
tf, err := os.Create(fileName)
if err != nil {
return "", err
}
_, err = tf.Write([]byte("[]"))
if err != nil {
return "", err
}
tf.Close()
}
return fileName, nil
}
func GoPath() (string, error) {
goPath := os.Getenv("GOPATH")
if goPath == "" {
goPath = build.Default.GOPATH
}
return goPath, nil
}