Initial QSfera import
This commit is contained in:
+75
@@ -0,0 +1,75 @@
|
||||
package archive
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TarGzWriter struct {
|
||||
*tar.Writer
|
||||
|
||||
gw *gzip.Writer
|
||||
}
|
||||
|
||||
func NewTarGzWriter(w io.Writer) *TarGzWriter {
|
||||
gw := gzip.NewWriter(w)
|
||||
tw := tar.NewWriter(gw)
|
||||
|
||||
return &TarGzWriter{
|
||||
Writer: tw,
|
||||
gw: gw,
|
||||
}
|
||||
}
|
||||
|
||||
func (tgw *TarGzWriter) WriteFile(path string, bs []byte) (err error) {
|
||||
hdr := &tar.Header{
|
||||
Name: path,
|
||||
Mode: 0600,
|
||||
Typeflag: tar.TypeReg,
|
||||
Size: int64(len(bs)),
|
||||
}
|
||||
|
||||
if err = tgw.WriteHeader(hdr); err == nil {
|
||||
_, err = tgw.Write(bs)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (tgw *TarGzWriter) WriteJSONFile(path string, v any) error {
|
||||
buf := &bytes.Buffer{}
|
||||
if err := json.NewEncoder(buf).Encode(v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tgw.WriteFile(path, buf.Bytes())
|
||||
}
|
||||
|
||||
func (tgw *TarGzWriter) Close() error {
|
||||
return errors.Join(tgw.Writer.Close(), tgw.gw.Close())
|
||||
}
|
||||
|
||||
// MustWriteTarGz writes the list of file names and content into a tarball.
|
||||
// Paths are prefixed with "/".
|
||||
func MustWriteTarGz(files [][2]string) *bytes.Buffer {
|
||||
buf := &bytes.Buffer{}
|
||||
tgw := NewTarGzWriter(buf)
|
||||
defer tgw.Close()
|
||||
|
||||
for _, file := range files {
|
||||
if !strings.HasPrefix(file[0], "/") {
|
||||
file[0] = "/" + file[0]
|
||||
}
|
||||
|
||||
if err := tgw.WriteFile(file[0], []byte(file[1])); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
// Copyright 2019 The OPA Authors. All rights reserved.
|
||||
// Use of this source code is governed by an Apache2
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package url contains helpers for dealing with file paths and URLs.
|
||||
package url
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var goos = runtime.GOOS
|
||||
|
||||
// Clean returns a cleaned file path that may or may not be a URL.
|
||||
func Clean(path string) (string, error) {
|
||||
|
||||
if strings.Contains(path, "://") {
|
||||
|
||||
url, err := url.Parse(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if url.Scheme != "file" {
|
||||
return "", fmt.Errorf("unsupported URL scheme: %v", path)
|
||||
}
|
||||
|
||||
path = url.Path
|
||||
|
||||
// Trim leading slash on Windows if present. The url.Path field returned
|
||||
// by url.Parse has leading slash that causes CreateFile() calls to fail
|
||||
// on Windows. See https://github.com/golang/go/issues/6027 for details.
|
||||
if goos == "windows" && len(path) >= 1 && path[0] == '/' {
|
||||
path = path[1:]
|
||||
}
|
||||
}
|
||||
|
||||
return path, nil
|
||||
}
|
||||
Reference in New Issue
Block a user