/
file-api.go
59 lines (50 loc) · 1.34 KB
/
file-api.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
package js
import (
contextpkg "context"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/tliron/exturl"
"github.com/tliron/kutil/util"
)
type FileAPI struct {
context *exturl.Context
}
func NewFileAPI(context *exturl.Context) FileAPI {
return FileAPI{
context: context,
}
}
func (self FileAPI) JoinFilePath(elements ...string) string {
return filepath.Join(elements...)
}
func (self FileAPI) 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 FileAPI) 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 FileAPI) TemporaryDirectory(pattern string, directory string) (string, error) {
return os.MkdirTemp(directory, pattern)
}
func (self FileAPI) Download(sourceUrl string, targetPath string) error {
if sourceUrl_, err := self.context.NewURL(sourceUrl); err == nil {
return exturl.DownloadTo(contextpkg.TODO(), sourceUrl_, targetPath)
} else {
return err
}
}