This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
106 lines (84 loc) · 2.52 KB
/
output.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
package builder
import (
"fmt"
"io"
"os"
"github.com/taubyte/go-interfaces/builders"
spec "github.com/taubyte/go-specs/builders"
"github.com/taubyte/go-specs/builders/wasm"
"github.com/taubyte/utils/bundle"
)
var DeprecatedWasmBuild bool
/*
TODO: build Flag
const DeprecatedWasmBuild
*/
// new sets the working directory and log file of the desired output
func new(wd spec.Dir) (out *output, err error) {
// set working
out = &output{
wd: wd,
}
// logs are set to a temporary directory
logFile, err := os.CreateTemp("", "logs")
if err != nil {
return nil, fmt.Errorf("creating temp log file failed with: %w", err)
}
out.logs = logs{logFile}
return
}
// deferHandler copies std to the output logs
func (o *output) deferHandler() {
io.Copy(os.Stdout, o.logs)
o.logs.Seek(0, io.SeekStart)
}
// Compress takes a CompressionMethod, and returns the compressed output of the files built by Build
func (o *output) Compress(method builders.CompressionMethod) (io.ReadSeekCloser, error) {
var (
zippedFile *os.File
err error
)
switch method {
case builders.WASM:
if DeprecatedWasmBuild {
return o.handleDeprecated()
}
// Try for both artifact/main.wasm
zippedFile, err = bundle.Zip(wasm.WasmOutput(o.outDir), o.wd.Wasm().Zip(), bundle.ZipFile)
if err != nil {
zippedFile, err = bundle.Zip(wasm.WasmDeprecatedOutput(o.outDir), o.wd.Wasm().Zip(), bundle.ZipFile)
}
case builders.Website:
zippedFile, err = bundle.Zip(o.outDir, o.wd.Website().BuildZip(), bundle.ZipDir)
default:
return nil, fmt.Errorf("compression method `%d` not supported", method)
}
return handleRSC(zippedFile, err, "zipping bundle failed with: %w")
}
// Close will Close logs
func (o *output) Close() error {
if o.logs.File != nil {
return o.logs.Close()
}
return nil
}
// handleRSC is a an error wrapper, which will seek the given ReadSeekCloser to start if error is nil
func handleRSC(rsc io.ReadSeekCloser, err error, errFormat string) (io.ReadSeekCloser, error) {
if err != nil {
return nil, fmt.Errorf(errFormat, err)
}
if rsc != nil {
if _, err = rsc.Seek(0, io.SeekStart); err != nil {
return nil, fmt.Errorf("seek to start failed with: %w", err)
}
return rsc, nil
}
return nil, fmt.Errorf("ReadSeekCloser is nil")
}
func (o *output) handleDeprecated() (io.ReadSeekCloser, error) {
compressedWasm, err := bundle.Compress(wasm.WasmOutput(o.outDir), o.wd.Wasm().WasmCompressed(), wasm.BufferSize)
if err != nil {
return nil, err
}
return handleRSC(compressedWasm, err, "compressing wasm failed with: %w")
}