68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package build
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
)
|
|
|
|
func (b *Builder) GenerateTarball() error {
|
|
buf := new(bytes.Buffer)
|
|
|
|
tarW := tar.NewWriter(buf)
|
|
|
|
err := tarW.WriteHeader(&tar.Header{
|
|
Name: "bin/",
|
|
Mode: int64(os.ModeDir) | 0o755,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("writing bin directory: %w", err)
|
|
}
|
|
|
|
for _, bin := range b.Binaries {
|
|
base := filepath.Base(bin)
|
|
|
|
binB, err := os.ReadFile(bin)
|
|
if err != nil {
|
|
return fmt.Errorf("reading binary %v: %w", bin, err)
|
|
}
|
|
|
|
err = tarW.WriteHeader(&tar.Header{
|
|
Name: filepath.Join("bin", base),
|
|
Size: int64(len(binB)),
|
|
Mode: 0o755,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("writing header for binary %v: %w", bin, err)
|
|
}
|
|
|
|
_, err = tarW.Write(binB)
|
|
if err != nil {
|
|
return fmt.Errorf("writing binary %v: %w", bin, err)
|
|
}
|
|
}
|
|
|
|
err = tarW.Close()
|
|
if err != nil {
|
|
return fmt.Errorf("writing tar footer: %w", err)
|
|
}
|
|
|
|
f, err := os.Create(filepath.Join(b.TempDir, fmt.Sprintf("%v-%v-%v.tar", b.Name, b.Version, runtime.GOARCH)))
|
|
if err != nil {
|
|
return fmt.Errorf("creating tarball: %w", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, buf)
|
|
if err != nil {
|
|
return fmt.Errorf("writing to tarball: %w", err)
|
|
}
|
|
|
|
fmt.Println("wrote tarball at", f.Name())
|
|
|
|
return nil
|
|
}
|