Skip to content

Commit

Permalink
better naming of dir/path/mainFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic committed Oct 19, 2020
1 parent 7991f04 commit 17379fc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
6 changes: 3 additions & 3 deletions pkg/jsonnet/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func EvaluateFile(jsonnetFile string, opts Opts) (string, error) {
}

// Evaluate renders the given jsonnet into a string
func Evaluate(filename, data string, opts Opts) (string, error) {
jpath, _, _, err := jpath.Resolve(filename)
func Evaluate(path, data string, opts Opts) (string, error) {
jpath, _, _, err := jpath.Resolve(path)
if err != nil {
return "", errors.Wrap(err, "resolving import paths")
}
opts.ImportPaths = jpath
vm := MakeVM(opts)
return vm.EvaluateAnonymousSnippet(filename, data)
return vm.EvaluateAnonymousSnippet(path, data)
}
10 changes: 5 additions & 5 deletions pkg/jsonnet/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TransitiveImports(dir string) ([]string, error) {
return nil, err
}

mainFile, err := jpath.Entrypoint(dir)
entrypoint, err := jpath.Entrypoint(dir)
if err != nil {
return nil, err
}

sonnet, err := ioutil.ReadFile(mainFile)
sonnet, err := ioutil.ReadFile(entrypoint)
if err != nil {
return nil, errors.Wrap(err, "opening file")
}
Expand All @@ -47,13 +47,13 @@ func TransitiveImports(dir string) ([]string, error) {
vm.NativeFunction(nf)
}

node, err := jsonnet.SnippetToAST(filepath.Base(mainFile), string(sonnet))
node, err := jsonnet.SnippetToAST(filepath.Base(entrypoint), string(sonnet))
if err != nil {
return nil, errors.Wrap(err, "creating Jsonnet AST")
}

imports := make(map[string]bool)
if err = importRecursive(imports, vm, node, filepath.Base(mainFile)); err != nil {
if err = importRecursive(imports, vm, node, filepath.Base(entrypoint)); err != nil {
return nil, err
}

Expand All @@ -68,7 +68,7 @@ func TransitiveImports(dir string) ([]string, error) {
paths = append(paths, p)

}
paths = append(paths, mainFile)
paths = append(paths, entrypoint)

for i := range paths {
paths[i], _ = filepath.Rel(rootDir, paths[i])
Expand Down
26 changes: 14 additions & 12 deletions pkg/jsonnet/jpath/jpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,15 @@ func (e ErrorFileNotFound) Error() string {
return e.filename + " not found"
}

// Resolve the given directory and resolves the jPath around it. This means it:
// Resolve the given path and resolves the jPath around it. This means it:
// - figures out the project root (the one with .jsonnetfile, vendor/ and lib/)
// - figures out the environments base directory (usually the main.jsonnet)
//
// It then constructs a jPath with the base directory, vendor/ and lib/.
// This results in predictable imports, as it doesn't matter whether the user called
// called the command further down tree or not. A little bit like git.
func Resolve(workdir string) (path []string, base, root string, err error) {
workdir, err = filepath.Abs(workdir)
if err != nil {
return nil, "", "", err
}

entrypoint, err := Entrypoint(workdir)
func Resolve(path string) (jpath []string, base, root string, err error) {
entrypoint, err := Entrypoint(path)
if err != nil {
return nil, "", "", err
}
Expand Down Expand Up @@ -121,14 +116,21 @@ func dirContainsFile(files []os.FileInfo, filename string) bool {
return false
}

func Entrypoint(dir string) (string, error) {
func Entrypoint(path string) (string, error) {
filename := DEFAULT_ENTRYPOINT
stat, err := os.Stat(dir)

entrypoint, err := filepath.Abs(path)
if err != nil {
return "", err
}

stat, err := os.Stat(entrypoint)
if err != nil {
return "", err
}
if !stat.IsDir() {
dir, filename = filepath.Split(dir)
return entrypoint, nil
}
return filepath.Join(dir, filename), nil

return filepath.Join(entrypoint, filename), nil
}
26 changes: 13 additions & 13 deletions pkg/tanka/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (p *loaded) connect() (*kubernetes.Kubernetes, error) {
}

// load runs all processing stages described at the Processed type
func load(dir string, opts Opts) (*loaded, error) {
raw, env, err := eval(dir, opts.JsonnetOpts)
func load(path string, opts Opts) (*loaded, error) {
raw, env, err := eval(path, opts.JsonnetOpts)
if err != nil {
return nil, err
}
Expand All @@ -86,13 +86,13 @@ func load(dir string, opts Opts) (*loaded, error) {

// eval runs all processing stages describe at the Processed type apart from
// post-processing, thus returning the raw Jsonnet result.
func eval(dir string, opts jsonnet.Opts) (raw interface{}, env *v1alpha1.Config, err error) {
env, err = parseSpec(dir)
func eval(path string, opts jsonnet.Opts) (raw interface{}, env *v1alpha1.Config, err error) {
env, err = parseSpec(path)
if err != nil {
return nil, nil, err
}

raw, err = evalJsonnet(dir, env, opts)
raw, err = evalJsonnet(path, env, opts)
if err != nil {
return nil, nil, errors.Wrap(err, "evaluating jsonnet")
}
Expand All @@ -102,8 +102,8 @@ func eval(dir string, opts jsonnet.Opts) (raw interface{}, env *v1alpha1.Config,

// parseEnv parses the `spec.json` of the environment and returns a
// *kubernetes.Kubernetes from it
func parseSpec(dir string) (*v1alpha1.Config, error) {
_, baseDir, rootDir, err := jpath.Resolve(dir)
func parseSpec(path string) (*v1alpha1.Config, error) {
_, baseDir, rootDir, err := jpath.Resolve(path)
if err != nil {
return nil, errors.Wrap(err, "resolving jpath")
}
Expand All @@ -129,8 +129,8 @@ func parseSpec(dir string) (*v1alpha1.Config, error) {
return config, nil
}

// evalJsonnet evaluates the jsonnet environment at the given directory
func evalJsonnet(baseDir string, env *v1alpha1.Config, opts jsonnet.Opts) (interface{}, error) {
// evalJsonnet evaluates the jsonnet environment at the given path
func evalJsonnet(path string, env *v1alpha1.Config, opts jsonnet.Opts) (interface{}, error) {
// make env spec accessible from Jsonnet
jsonEnv, err := json.Marshal(env)
if err != nil {
Expand All @@ -140,19 +140,19 @@ func evalJsonnet(baseDir string, env *v1alpha1.Config, opts jsonnet.Opts) (inter

// evaluate Jsonnet
var raw string
mainFile, err := jpath.Entrypoint(baseDir)
entrypoint, err := jpath.Entrypoint(path)
if err != nil {
return nil, err
}

if opts.EvalPattern != "" {
evalScript := fmt.Sprintf("(import '%s').%s", mainFile, opts.EvalPattern)
raw, err = jsonnet.Evaluate(mainFile, evalScript, opts)
evalScript := fmt.Sprintf("(import '%s').%s", entrypoint, opts.EvalPattern)
raw, err = jsonnet.Evaluate(entrypoint, evalScript, opts)
if err != nil {
return nil, err
}
} else {
raw, err = jsonnet.EvaluateFile(mainFile, opts)
raw, err = jsonnet.EvaluateFile(entrypoint, opts)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 17379fc

Please sign in to comment.