-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.go
83 lines (70 loc) · 1.87 KB
/
os.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
package api
import (
contextpkg "context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/tliron/commonjs-goja"
"github.com/tliron/exturl"
"github.com/tliron/kutil/util"
)
// ([commonjs.CreateExtensionFunc] signature)
func CreateOSExtension(jsContext *commonjs.Context) any {
return NewOS(jsContext.Environment.URLContext)
}
//
// OS
//
type OS struct {
Stdout io.Writer
Stderr io.Writer
urlContext *exturl.Context
}
func NewOS(urlContext *exturl.Context) OS {
return OS{
Stdout: os.Stdout,
Stderr: os.Stderr,
urlContext: urlContext,
}
}
func (self OS) JoinFilePath(elements ...string) string {
return filepath.Join(elements...)
}
func (self OS) Exec(name string, arguments ...string) (string, error) {
cmd := exec.Command(name, arguments...)
if out, err := cmd.Output(); err == nil {
return util.BytesToString(out), nil
} else if err_, ok := err.(*exec.ExitError); ok {
return "", fmt.Errorf("%s\n%s", err_.Error(), util.BytesToString(err_.Stderr))
} else {
return "", err
}
}
func (self OS) TemporaryFile(pattern string, directory string) (string, error) {
if file, err := os.CreateTemp(directory, pattern); err == nil {
name := file.Name()
os.Remove(name)
return name, nil
} else {
return "", err
}
}
func (self OS) TemporaryDirectory(pattern string, directory string) (string, error) {
return os.MkdirTemp(directory, pattern)
}
func (self OS) Download(sourceUrl string, targetPath string, timeoutSeconds float64) error {
if sourceUrl_, err := self.urlContext.NewURL(sourceUrl); err == nil {
context := contextpkg.Background()
if timeoutSeconds > 0.0 {
var cancelContext contextpkg.CancelFunc
context, cancelContext = contextpkg.WithTimeout(context, time.Duration(timeoutSeconds*float64(time.Second)))
defer cancelContext()
}
return exturl.DownloadTo(context, sourceUrl_, targetPath)
} else {
return err
}
}