parent
7d95825f97
commit
3ac86e07cf
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
@ -1,8 +0,0 @@ |
||||
package bootstrap |
||||
|
||||
// Paths within the bootstrap FS related to nebula.
|
||||
const ( |
||||
nebulaCertsCACertPath = "nebula/certs/ca.crt" |
||||
nebulaCertsHostCertPath = "nebula/certs/host.crt" |
||||
nebulaCertsHostKeyPath = "nebula/certs/host.key" |
||||
) |
@ -1,24 +0,0 @@ |
||||
// Package tarutil implements utilities which are useful for interacting with
|
||||
// tar and tgz files.
|
||||
package tarutil |
||||
|
||||
import ( |
||||
"compress/gzip" |
||||
"fmt" |
||||
"io" |
||||
"io/fs" |
||||
|
||||
"github.com/nlepage/go-tarfs" |
||||
) |
||||
|
||||
// FSFromReader returns a FS instance which will read the contents of a tgz
|
||||
// file from the given Reader.
|
||||
func FSFromReader(r io.Reader) (fs.FS, error) { |
||||
gf, err := gzip.NewReader(r) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("un-gziping: %w", err) |
||||
} |
||||
defer gf.Close() |
||||
|
||||
return tarfs.New(gf) |
||||
} |
@ -1,112 +0,0 @@ |
||||
package tarutil |
||||
|
||||
import ( |
||||
"archive/tar" |
||||
"bytes" |
||||
"compress/gzip" |
||||
"fmt" |
||||
"io" |
||||
"path/filepath" |
||||
"strings" |
||||
) |
||||
|
||||
// TGZWriter is a utility for writing tgz files. If an internal error is
|
||||
// encountered by any method then all subsequent methods will be no-ops, and
|
||||
// Close() will return that error (after closing out resources).
|
||||
type TGZWriter struct { |
||||
gzipW *gzip.Writer |
||||
tarW *tar.Writer |
||||
err error |
||||
|
||||
dirsWritten map[string]bool |
||||
} |
||||
|
||||
// NewTGZWriter initializes and returns a new instance of TGZWriter which will
|
||||
// write all data to the given io.Writer.
|
||||
func NewTGZWriter(w io.Writer) *TGZWriter { |
||||
gzipW := gzip.NewWriter(w) |
||||
tarW := tar.NewWriter(gzipW) |
||||
return &TGZWriter{ |
||||
gzipW: gzipW, |
||||
tarW: tarW, |
||||
dirsWritten: map[string]bool{}, |
||||
} |
||||
} |
||||
|
||||
// Close cleans up all open resources being held by TGZWriter, and returns the
|
||||
// first internal error which was encountered during its operation (if any).
|
||||
func (w *TGZWriter) Close() error { |
||||
w.tarW.Close() |
||||
w.gzipW.Close() |
||||
return w.err |
||||
} |
||||
|
||||
func (w *TGZWriter) writeDir(path string) { |
||||
|
||||
if w.err != nil { |
||||
return |
||||
|
||||
} else if path != "." { |
||||
w.writeDir(filepath.Dir(path)) |
||||
} |
||||
|
||||
if path == "." { |
||||
path = "./" |
||||
} else { |
||||
path = "./" + strings.TrimPrefix(path, "./") |
||||
path = path + "/" |
||||
} |
||||
|
||||
if w.dirsWritten[path] { |
||||
return |
||||
} |
||||
|
||||
err := w.tarW.WriteHeader(&tar.Header{ |
||||
Name: path, |
||||
Mode: 0700, |
||||
}) |
||||
|
||||
if err != nil { |
||||
w.err = fmt.Errorf("writing header for directory %q: %w", path, err) |
||||
return |
||||
} |
||||
|
||||
w.dirsWritten[path] = true |
||||
} |
||||
|
||||
// WriteFile writes a file to the tgz archive. The file will automatically be
|
||||
// rooted to the "." directory, and any sub-directories the file exists in
|
||||
// should have already been created.
|
||||
func (w *TGZWriter) WriteFile(path string, size int64, body io.Reader) { |
||||
|
||||
if w.err != nil { |
||||
return |
||||
} |
||||
|
||||
path = "./" + strings.TrimPrefix(path, "./") |
||||
|
||||
w.writeDir(filepath.Dir(path)) |
||||
|
||||
err := w.tarW.WriteHeader(&tar.Header{ |
||||
Name: path, |
||||
Size: size, |
||||
Mode: 0400, |
||||
}) |
||||
|
||||
if err != nil { |
||||
w.err = fmt.Errorf("writing header for file %q: %w", path, err) |
||||
return |
||||
} |
||||
|
||||
if _, err := io.Copy(w.tarW, body); err != nil { |
||||
w.err = fmt.Errorf("writing file body of file %q: %w", path, err) |
||||
return |
||||
} |
||||
} |
||||
|
||||
// WriteFileBytes is a shortcut for calling WriteFile with the given byte slice
|
||||
// being used as the file body.
|
||||
func (w *TGZWriter) WriteFileBytes(path string, body []byte) { |
||||
bodyR := bytes.NewReader(body) |
||||
w.WriteFile(path, bodyR.Size(), bodyR) |
||||
} |
Loading…
Reference in new issue