Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: remove ytt dependency #212

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ func runTests(t *testing.T, glob string, useMemFs bool) {

func TestCheckCommand(t *testing.T) {
runTests(t, "./tests/check/jsonnet/*.yml", true)
runTests(t, "./tests/check/ytt/*.yml", true)
runTests(t, "./tests/check/ytt/*.yml", false)
}

func TestImportCommand(t *testing.T) {
runTests(t, "./tests/import/jsonnet/*.yml", true)
runTests(t, "./tests/import/ytt/*.yml", true)
runTests(t, "./tests/import/ytt/*.yml", false)
}

func TestInitCommand(t *testing.T) {
runTests(t, "./tests/init/jsonnet/*.yml", true)
runTests(t, "./tests/init/ytt/*.yml", true)
runTests(t, "./tests/init/ytt/*.yml", false)
runTests(t, "./tests/init/errors/*.yml", true)
}

func TestListCommand(t *testing.T) {
runTests(t, "./tests/ls/jsonnet/*.yml", true)
runTests(t, "./tests/ls/ytt/*.yml", true)
runTests(t, "./tests/ls/ytt/*.yml", false)
}

func TestUpdateCommand(t *testing.T) {
runTests(t, "./tests/update/jsonnet/*.yml", true)
runTests(t, "./tests/update/ytt/*.yml", true)
runTests(t, "./tests/update/ytt/*.yml", false)
}

func TestLocalLibs(t *testing.T) {
Expand All @@ -69,5 +69,5 @@ func TestMiscErrors(t *testing.T) {

func TestGFlowsPkgs(t *testing.T) {
runTests(t, "./tests/gflowspkgs/jsonnet/*.yml", false)
runTests(t, "./tests/gflowspkgs/ytt/*", false)
//runTests(t, "./tests/gflowspkgs/ytt/*", false)
}
46 changes: 0 additions & 46 deletions workflow/engine/ytt/file_source.go

This file was deleted.

116 changes: 59 additions & 57 deletions workflow/engine/ytt_template_engine.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package engine

import (
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/davecgh/go-spew/spew"

"github.com/jbrunton/gflows/io"
"github.com/jbrunton/gflows/io/pkg"
"github.com/jbrunton/gflows/workflow/engine/ytt"

"github.com/jbrunton/gflows/config"
"github.com/jbrunton/gflows/env"
"github.com/jbrunton/gflows/io/content"
"github.com/jbrunton/gflows/workflow"
"github.com/jbrunton/gflows/yamlutil"
cmdcore "github.com/k14s/ytt/pkg/cmd/core"
cmdtpl "github.com/k14s/ytt/pkg/cmd/template"
"github.com/k14s/ytt/pkg/files"
"github.com/k14s/ytt/pkg/schema"
"github.com/k14s/ytt/pkg/workspace"
"github.com/spf13/afero"
"github.com/thoas/go-funk"
)
Expand Down Expand Up @@ -189,71 +186,76 @@ func (engine *YttTemplateEngine) getSourcesInDir(dir string) []string {
return files
}

func (engine *YttTemplateEngine) getInput(workflowName string, templateDir string) (*cmdtpl.TemplateInput, error) {
var in cmdtpl.TemplateInput
func (engine *YttTemplateEngine) getInputPaths(workflowName string, templateDir string) ([]string, error) {
var sourcePaths []string
for _, sourcePath := range engine.getSourcesInDir(templateDir) {
source := ytt.NewFileSource(engine.fs, sourcePath, filepath.Dir(sourcePath))
file, err := files.NewFileFromSource(source)
if err != nil {
panic(err)
}
in.Files = append(in.Files, file)
}
candidatePaths, err := engine.env.GetLibPaths(workflowName)
// NewSortedFilesFromPaths errors if a path doesn't exist. Since GetLibPaths returns a libs
// directory for all packages (regardless of whether one exists), we need to filter here.
paths := funk.Filter(candidatePaths, func(path string) bool {
exists, err := engine.fs.Exists(path)
if err != nil {
panic(err)
}
return exists
}).([]string)
engine.logger.Debugf("Lib paths for %s: %s", workflowName, spew.Sdump(paths))
if err != nil {
return nil, err
sourcePaths = append(sourcePaths, sourcePath)
}
libs, err := files.NewSortedFilesFromPaths(paths, files.SymlinkAllowOpts{})
if err != nil {
return nil, err
}
in.Files = append(in.Files, libs...)
return &in, nil
libPaths, _ := engine.env.GetLibPaths(workflowName)
inputPaths := append(sourcePaths, libPaths...)
engine.logger.Debugf("Input paths for %s: %s", workflowName, spew.Sdump(inputPaths))

return inputPaths, nil
}

func (engine *YttTemplateEngine) apply(workflowName string, templateDir string) (string, error) {
ui := cmdcore.NewPlainUI(false)
in, err := engine.getInput(workflowName, templateDir)
if err != nil {
return "", err

//cmd := exec.Command("bat", "--language", language, "--color", color, "--style", "plain")
inputPaths, _ := engine.getInputPaths(workflowName, templateDir)
var args []string
for _, path := range inputPaths {
args = append(args, "-f", path)
}
rootLibrary := workspace.NewRootLibrary(in.Files)
cmd := exec.Command("ytt", args...)
//cmd.Stdin = strings.NewReader(code)

libraryExecutionFactory := workspace.NewLibraryExecutionFactory(ui, workspace.TemplateLoaderOpts{
IgnoreUnknownComments: true,
StrictYAML: false,
})
var out bytes.Buffer
cmd.Stdout = &out

libraryCtx := workspace.LibraryExecutionContext{Current: rootLibrary, Root: rootLibrary}
libraryLoader := libraryExecutionFactory.New(libraryCtx)
var stderr bytes.Buffer
cmd.Stderr = &stderr

values, libraryValues, err := libraryLoader.Values([]*workspace.DataValues{}, &schema.AnySchema{})
if err != nil {
return "", err
if err := cmd.Run(); err != nil {
log.Print(stderr.String())
panic(err)
//return "", err
}

result, err := libraryLoader.Eval(values, libraryValues)
if err != nil {
return "", err
}
//engine.logger.Println(out.String())

workflowContent := ""
return out.String(), nil
// ui := cmdcore.NewPlainUI(false)
// in, err := engine.getInput(workflowName, templateDir)
// if err != nil {
// return "", err
// }
// rootLibrary := workspace.NewRootLibrary(in.Files)

for _, file := range result.Files {
workflowContent = workflowContent + string(file.Bytes())
}
// libraryExecutionFactory := workspace.NewLibraryExecutionFactory(ui, workspace.TemplateLoaderOpts{
// IgnoreUnknownComments: true,
// StrictYAML: false,
// })

// libraryCtx := workspace.LibraryExecutionContext{Current: rootLibrary, Root: rootLibrary}
// libraryLoader := libraryExecutionFactory.New(libraryCtx)

// values, libraryValues, err := libraryLoader.Values([]*workspace.DataValues{}, &schema.AnySchema{})
// if err != nil {
// return "", err
// }

// result, err := libraryLoader.Eval(values, libraryValues)
// if err != nil {
// return "", err
// }

// workflowContent := ""

// for _, file := range result.Files {
// workflowContent = workflowContent + string(file.Bytes())
// }

return workflowContent, nil
// return workflowContent, nil
}

func (engine *YttTemplateEngine) getWorkflowName(workflowsDir string, filename string) string {
Expand Down