Skip to content

Commit

Permalink
boopa
Browse files Browse the repository at this point in the history
  • Loading branch information
Cictrone committed Apr 16, 2020
1 parent 0a82422 commit f80f82b
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 847 deletions.
8 changes: 5 additions & 3 deletions pkg/script/stdlib/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ package assets
import (
"net/http"

"github.com/kcarretto/paragon/pkg/cdn"
"github.com/kcarretto/paragon/pkg/script"
)

// Environment used to configure the behaviour of calls to the ssh library.
type Environment struct {
Assets http.FileSystem
Assets http.FileSystem
Files []NamedReader
Downloader cdn.Downloader
}

// Library prepares a new assets library for use within a script environment.
Expand All @@ -22,8 +25,7 @@ func (env *Environment) Library(options ...func(*Environment)) script.Library {
return script.Library{
"openFile": script.Func(env.openFile),
"drop": script.Func(env.drop),
// "exec": script.Func(env.exec),
// "openFile": script.Func(env.openFile),
"require": script.Func(env.drop),
}
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/script/stdlib/assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ func TestTarGZBundleConsistent(t *testing.T) {
t.Errorf("failed to open file: %w", err)
}
targzbundlr := assets.TarGZBundler{}
err = targzbundlr.Bundle(f1, f2)
err = targzbundlr.Bundle(
assets.NamedReader{
Reader: f1,
Name: file1Name,
},
assets.NamedReader{
Reader: f2,
Name: file2Name,
},
)
if err != nil {
t.Errorf("failed to bundle files: %w", err)
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/script/stdlib/assets/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import (

// Bundler is an interface for handling multi-file (de)serialization and creation of a http.FileSystem
type Bundler interface {
Bundle(...http.File) error
Bundle(...NamedReader) error
}

// NamedReader is a basic struct to organize the data needed for the Bundler
type NamedReader struct {
io.Reader
Name string
}

// TarGZBundler is the concrete implementation of Bundle using Tar and GZip
Expand All @@ -22,20 +28,16 @@ type TarGZBundler struct {
}

// Bundle is used to add multiple files into a tar bundle
func (tb *TarGZBundler) Bundle(files ...http.File) error {
func (tb *TarGZBundler) Bundle(files ...NamedReader) error {
tb.Buffer = &bytes.Buffer{}
tw := tar.NewWriter(tb.Buffer)
for _, file := range files {
info, err := file.Stat()
if err != nil {
return err
}
body, err := ioutil.ReadAll(file)
if err != nil {
return err
}
hdr := &tar.Header{
Name: info.Name(),
Name: file.Name,
Mode: 0644,
Size: int64(len(body)),
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/script/stdlib/assets/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,32 @@ func (env *Environment) drop(parser script.ArgParser) (script.Retval, error) {
retErr := env.Drop(f, dstPath, perms)
return retErr, nil
}

// Require will be used in the init function for the worker to specify which files you wish to
// include in the asset bundle which will be accessible on the target.
//
//go:generate go run ../gendoc.go -lib assets -func require -param filePath@String -retval err@Error -doc "Require will be used in the init function for the worker to specify which files you wish to include in the asset bundle which will be accessible on the target."
//
// @callable: assets.require
// @param: filePath @String
// @retval: err @Error
//
// @usage: f, err = assets.require("file_on_cdn")
func (env *Environment) Require(filePath string) (err error) {
f, err := env.Downloader.Download(filePath)
if err != nil {
return err
}
env.Files = append(env.Files, NamedReader{Reader: f, Name: filePath})
return nil

}

func (env *Environment) require(parser script.ArgParser) (script.Retval, error) {
filePath, err := parser.GetString(0)
if err != nil {
return nil, err
}
retErr := env.Require(filePath)
return retErr, nil
}
392 changes: 196 additions & 196 deletions www/assets.gen.go

Large diffs are not rendered by default.

0 comments on commit f80f82b

Please sign in to comment.