Skip to content

Commit 7bd5ab8

Browse files
add: implemented the executable tool for prereqs
1 parent cb2b888 commit 7bd5ab8

5 files changed

Lines changed: 41 additions & 29 deletions

File tree

common.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import (
3232
// default files
3333
const OPSFILE = "opsfile.yml"
3434
const OPSROOT = "opsroot.json"
35-
const DOCOPTS = "docopts.txt"
35+
const DOCOPTS_TXT = "docopts.txt"
36+
const DOCOPTS_MD = "docopts.txt"
37+
3638
const PREREQ = "prereq.yml"
3739
const CONFIGFILE = "config.json"
3840

ops.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func (e *TaskNotFoundErr) Error() string {
4141
}
4242

4343
func help() error {
44-
if os.Getenv("OPS_NO_DOCOPTS") == "" && exists(".", DOCOPTS) {
45-
os.Args = []string{"envsubst", "-no-unset", "-i", DOCOPTS}
44+
if os.Getenv("OPS_NO_DOCOPTS") == "" && exists(".", DOCOPTS_TXT) {
45+
os.Args = []string{"envsubst", "-no-unset", "-i", DOCOPTS_TXT}
4646
return envsubst.EnvsubstMain()
4747
}
4848
// In case of syntax error, Task will return an error
@@ -223,9 +223,9 @@ func Ops(base string, args []string) error {
223223
savedArgs := loadSavedArgs()
224224

225225
// parsed args
226-
if os.Getenv("OPS_NO_DOCOPTS") == "" && exists(".", DOCOPTS) {
226+
if os.Getenv("OPS_NO_DOCOPTS") == "" && exists(".", DOCOPTS_TXT) {
227227
debug("PREPARSE:", rest)
228-
opts := readfile(DOCOPTS)
228+
opts := readfile(DOCOPTS_TXT)
229229
trace("DOCOPTS: size=", len(opts))
230230
parsedArgs := parseArgs(opts, rest)
231231
trace("DOCOPTS: parsedargs=", parsedArgs)

prereq.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
"os"
2323
"os/exec"
2424
"path/filepath"
25-
"runtime"
2625
"strings"
2726

27+
"github.com/apache/openserverless-cli/tools"
2828
"github.com/mitchellh/go-homedir"
2929
"gopkg.in/yaml.v3"
3030
)
@@ -88,27 +88,11 @@ func loadPrereq(dir string) (Prereq, error) {
8888
return prereq, err
8989
}
9090

91-
func getOS() string {
92-
res := os.Getenv("__OS")
93-
if res == "" {
94-
res = runtime.GOOS
95-
}
96-
return res
97-
}
98-
99-
func getARCH() string {
100-
res := os.Getenv("__ARCH")
101-
if res == "" {
102-
res = runtime.GOARCH
103-
}
104-
return res
105-
}
106-
10791
func binDir() (string, error) {
10892
var err error
10993
bindir := os.Getenv("OPS_BIN")
11094
if bindir == "" {
111-
bindir, err = homedir.Expand(fmt.Sprintf("~/.ops/%s-%s/bin", getOS(), getARCH()))
95+
bindir, err = homedir.Expand(fmt.Sprintf("~/.ops/%s-%s/bin", tools.GetOS(), tools.GetARCH()))
11296
if err != nil {
11397
return "", err
11498
}
@@ -118,7 +102,7 @@ func binDir() (string, error) {
118102
}
119103

120104
func addExeExt(name string) string {
121-
if getOS() == "windows" {
105+
if tools.GetOS() == "windows" {
122106
return name + ".exe"
123107
}
124108
return name

tests/utils.bats

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,21 @@ setup() {
6767
}
6868

6969
@test "-executable" {
70-
skip
7170
touch _hello
72-
run __OS=linux ops -executable _hello
71+
chmod 0600 _hello
72+
run env __OS=linux ops -executable _hello
7373
assert_success
74+
assert_line "Successfully added execute permissions to _hello"
7475
assert test -x _hello
75-
run __OS=windows ops -executable _hello
76+
run env __OS=windows ops -executable _hello
7677
assert_success
7778
assert test -e _hello.exe
78-
run __OS=windows ops -executable _hello.exe
79+
assert_line "Successfully renamed _hello to _hello.exe"
80+
run env __OS=windows ops -executable _hello.exe
7981
assert_success
8082
assert test -e _hello.exe
81-
rm _hello
83+
assert_line "Nothing to do"
84+
rm _hello.exe
8285
}
8386

8487
@test "-copy" {

tools/tools.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"log"
2222
"os"
23+
"runtime"
2324
"strings"
2425

2526
gojq "github.com/itchyny/gojq/cli"
@@ -39,6 +40,23 @@ func trace(args ...any) {
3940
}
4041
}
4142

43+
// shared with main
44+
func GetOS() string {
45+
res := os.Getenv("__OS")
46+
if res == "" {
47+
res = runtime.GOOS
48+
}
49+
return res
50+
}
51+
52+
func GetARCH() string {
53+
res := os.Getenv("__ARCH")
54+
if res == "" {
55+
res = runtime.GOARCH
56+
}
57+
return res
58+
}
59+
4260
// available in taskfiles
4361
// note some of them are implemented in main.go (config, retry)
4462
// Note the comment with @DOC which is used to generate the list of tools in documentation
@@ -64,6 +82,7 @@ var ToolList = []string{
6482
"gron", "jj",
6583
"rename",
6684
"remove",
85+
"executable",
6786
"empty",
6887
"extract",
6988
}
@@ -190,6 +209,10 @@ func RunTool(name string, args []string) (int, error) {
190209
os.Args = append([]string{"remove"}, args...)
191210
return Remove()
192211

212+
case "executable":
213+
os.Args = append([]string{"executable"}, args...)
214+
return Executable()
215+
193216
case "extract":
194217
os.Args = append([]string{"extract"}, args...)
195218
return Extract()

0 commit comments

Comments
 (0)