-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommon.go
226 lines (191 loc) · 4.71 KB
/
common.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 openserverless
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
_ "embed"
)
// default files
const OPSFILE = "opsfile.yml"
const OPSROOT = "opsroot.json"
const DOCOPTS_TXT = "docopts.txt"
const DOCOPTS_MD = "docopts.md"
const PREREQ = "prereq.yml"
const CONFIGFILE = "config.json"
// repo where download tasks
const OPSREPO = "http://github.com/apache/openserverless-task"
// branch where download tasks
// defaults to test - will be changed in compilation
//go:embed version.txt
var OpsVersion string
//go:embed branch.txt
var OpsBranch string
//go:embed runtimes.json
var WSK_RUNTIMES_JSON string
// Represents opsroot.json. It is used to parse the file.
type OpsRootJSON struct {
Version string `json:"version"`
Config map[string]interface{} `json:"config"`
}
// default port for ops server
const DefaultOpsPort = 9768
func getOpsPort() string {
port := os.Getenv("OPS_PORT")
if port == "" {
port = fmt.Sprintf("%d", DefaultOpsPort)
}
//nolint:errcheck
os.Setenv("OPS_PORT", port)
return port
}
// get defaults
func getOpsRoot() (string, error) {
root := os.Getenv("OPS_ROOT")
if root == "" {
dir, err := os.Getwd()
if err == nil {
root, err = locateOpsRoot(dir)
}
if err != nil {
return "", err
}
}
//nolint:errcheck
os.Setenv("OPS_ROOT", root)
return root, nil
}
func getOpsRepo() string {
repo := os.Getenv("OPS_REPO")
if repo == "" {
repo = OPSREPO
}
//nolint:errcheck
os.Setenv("OPS_REPO", repo)
return repo
}
func getOpsBranch() string {
branch := os.Getenv("OPS_BRANCH")
if branch == "" {
branch = strings.TrimSpace(OpsBranch)
}
//nolint:errcheck
os.Setenv("OPS_BRANCH", branch)
return branch
}
func readOpsRootFile(dir string) (OpsRootJSON, error) {
data := OpsRootJSON{}
json_buf, err := os.ReadFile(joinpath(dir, OPSROOT))
if err != nil {
return OpsRootJSON{}, err
}
if err := json.Unmarshal(json_buf, &data); err != nil {
warn("opsroot.json parsed with an error", err)
}
return data, nil
}
// utils
func joinpath(dir string, file string) string {
return filepath.Join(dir, file)
}
func split(s string) []string {
return strings.Fields(s)
}
func exists(dir string, file string) bool {
_, err := os.Stat(joinpath(dir, file))
return !os.IsNotExist(err)
}
func isDir(fileOrDir string) bool {
fileInfo, err := os.Stat(fileOrDir)
if err != nil {
debug(err)
return false
}
// Check if the file is a directory
if fileInfo.IsDir() {
return true
}
return false
}
func parent(dir string) string {
return filepath.Dir(dir)
}
func readfile(file string) string {
dat, err := os.ReadFile(file)
if err != nil {
return ""
}
return string(dat)
}
//var logger log.Logger = log.New(os.Stderr, "", 0)
func warn(args ...any) {
log.Println(args...)
}
var tracing = os.Getenv("TRACE") != ""
func trace(args ...any) {
if tracing {
log.Println(append([]any{"TRACE: "}, args...))
}
}
func Trace(args ...any) {
trace(args...)
}
var debugging = os.Getenv("DEBUG") != "" || os.Getenv("TRACE") != ""
func debug(args ...any) {
if debugging {
log.Println(append([]any{"DEBUG: "}, args...))
}
}
func debugf(format string, args ...any) {
if debugging {
log.Printf("DEBUG: "+format+"\n", args...)
}
}
func touch(dir string, name string) error {
tgt := filepath.Join(dir, name)
trace("touch", tgt)
f, err := os.Create(tgt)
if err != nil {
return err
}
f.Close()
return nil
}
// confirm prompts the user for a yes/no answer and returns true if the answer is "yes".
func confirm(prompt string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print(prompt)
response, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("Error reading input: %v\n", err)
return false
}
response = strings.TrimSpace(strings.ToLower(response))
if response == "yes" || response == "y" {
return true
} else if response == "no" || response == "n" {
return false
}
fmt.Println("Please type yes or no and then press enter.")
}
}