Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
74 lines (63 loc) · 1.42 KB
/
Copy pathexample_test.go
File metadata and controls
74 lines (63 loc) · 1.42 KB
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
package exec_test
import (
"context"
"embed"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
execbin "github.com/zlsgo/exec"
)
func ExampleNew_localBinary() {
binFile, cleanup := writeExampleScript()
defer cleanup()
engine, err := execbin.New(embed.FS{}, func(options *execbin.Options) {
options.BinFile = binFile
})
if err != nil {
panic(err)
}
defer engine.Release()
code, out, _, err := engine.Run(context.Background(), []string{"example"})
if err != nil {
panic(err)
}
fmt.Println(code, strings.TrimSpace(out))
// Output:
// 0 local-example
}
func ExampleOptions_DownloadHandlerWithDir() {
_ = execbin.Options{
BinFile: "tool",
DownloadUrl: "https://example.com/tool.gz",
DownloadHandlerWithDir: func(file string, dir string) error {
fmt.Println(filepath.Base(file) != "", dir != "")
return nil
},
}
fmt.Println(true)
// Output:
// true
}
func writeExampleScript() (string, func()) {
dir, err := os.MkdirTemp("", "exec-example-*")
if err != nil {
panic(err)
}
var path string
if runtime.GOOS == "windows" {
path = filepath.Join(dir, "example.cmd")
err = os.WriteFile(path, []byte("@echo off\r\necho local-example\r\n"), 0o755)
} else {
path = filepath.Join(dir, "example.sh")
err = os.WriteFile(path, []byte("#!/bin/sh\nprintf 'local-example\\n'\n"), 0o755)
}
if err != nil {
_ = os.RemoveAll(dir)
panic(err)
}
return path, func() {
_ = os.RemoveAll(dir)
}
}