Skip to content

Commit

Permalink
feat(tool): use jpath tool to export JSONNET_PATH (#427)
Browse files Browse the repository at this point in the history
This allows us to export the JSONNET_PATH for use with other tools like
`jsonnet-lint` or `jsonnet-deps`.

Example:

eval $(tk tool jpath environment/prod/main.jsonnet)
JPATH=$(tk tool jpath environments/prod) jsonnet-lint environment/prod/main.jsonnet
  • Loading branch information
Duologic authored Dec 11, 2020
1 parent d5c1a23 commit c06134b
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions cmd/tk/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -30,29 +31,40 @@ func toolCmd() *cli.Command {

func jpathCmd() *cli.Command {
cmd := &cli.Command{
Short: "print information about the jpath",
Use: "jpath",
Run: func(cmd *cli.Command, args []string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
path, base, root, err := jpath.Resolve(pwd)
if err != nil {
return fmt.Errorf("Resolving JPATH: %s", err)
}
entrypoint, err := jpath.Entrypoint(base)
if err != nil {
return fmt.Errorf("Resolving JPATH: %s", err)
}
fmt.Println("main:", entrypoint)
fmt.Println("rootDir:", root)
fmt.Println("baseDir:", base)
fmt.Println("jpath:", path)
Short: "export JSONNET_PATH for use with other jsonnet tools",
Use: "jpath [<file/dir>]",
Args: workflowArgs,
}

debug := cmd.Flags().BoolP("debug", "d", false, "show debug info")

cmd.Run = func(cmd *cli.Command, args []string) error {
path := args[0]

return nil
},
entrypoint, err := jpath.Entrypoint(path)
if err != nil {
return fmt.Errorf("Resolving JPATH: %s", err)
}

jsonnetpath, base, root, err := jpath.Resolve(entrypoint)
if err != nil {
return fmt.Errorf("Resolving JPATH: %s", err)
}

if *debug {
// log to debug info to stderr
log.Println("main:", entrypoint)
log.Println("rootDir:", root)
log.Println("baseDir:", base)
log.Println("jpath:", jsonnetpath)
}

// print export JSONNET_PATH to stdout
fmt.Printf("%s", strings.Join(jsonnetpath, ":"))

return nil
}

return cmd
}

Expand Down

0 comments on commit c06134b

Please sign in to comment.