Skip to content

Commit

Permalink
start getting jump to work
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed May 25, 2020
1 parent 81a815b commit f5dd77d
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .hof/shadow/Cli/cmd/hof/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var GenCmd = &cobra.Command{
Use: "gen [files...]",

Aliases: []string{
"g",
"G",
},

Short: "generate code, data, and config from your data models and designs",
Expand Down
4 changes: 2 additions & 2 deletions cmd/hof/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var GenCmd = &cobra.Command{
Use: "gen [files...]",

Aliases: []string{
"g",
"G",
},

Short: "generate code, data, and config from your data models and designs",
Expand Down Expand Up @@ -85,4 +85,4 @@ func init() {
GenCmd.SetHelpFunc(thelp)
GenCmd.SetUsageFunc(tusage)

}
}
2 changes: 1 addition & 1 deletion design/cli/cmds/hof.cue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
TBD: ""
Name: "gen"
Usage: "gen [files...]"
Aliases: ["g"]
Aliases: ["G"]
Short: "generate code, data, and config from your data models and designs"
Long: """
generate all the things, from code to data to config...
Expand Down
125 changes: 124 additions & 1 deletion lib/ops/jump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,134 @@ package ops

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"cuelang.org/go/cue"
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/load"

"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/cuetils"
"github.com/hofstadter-io/hof/lib/yagu"
)

const JUMP_FILE_NAME = "jumps.cue"

func RunJumpFromArgs(args []string) error {
fmt.Println("lib/ops.Jump", args)
// fmt.Println("lib/ops.Jump", args)

jfn := JUMP_FILE_NAME

// Check our flags
if flags.RootResourcesDirPflag != "" {
rDir := flags.RootResourcesDirPflag
jfn = filepath.Join(rDir, JUMP_FILE_NAME)
} else if flags.RootLocalPflag {
rDir := "resources"
jfn = filepath.Join(rDir, JUMP_FILE_NAME)
} else {
bDir, err := os.UserConfigDir()
if err != nil {
return err
}
rDir := "resources"
jfn = filepath.Join(bDir, "hof", rDir, JUMP_FILE_NAME)
}
exists, _ := yagu.CheckPathExists(jfn)
if !exists {
content := `
package resources
jumps: {}
`
yagu.Mkdir(filepath.Dir(jfn))
ioutil.WriteFile(jfn, []byte(content), 0644)
}

var err error

entrypoints := []string {jfn}

rCRT := &cuetils.CueRuntime{
Entrypoints: entrypoints,
CueConfig: &load.Config{
ModuleRoot: "",
Module: "",
Package: "",
Dir: "",
},
}

err = rCRT.Load()
if err != nil {
return err
}

if len(args) == 0 {
bytes, err := format.Node(rCRT.CueValue.Syntax())
if err != nil {
return err
}
fmt.Printf("%s\n\n", string(bytes))
return nil
}

rest := []string{}
for i, arg := range args {
if arg == "__" {
rest = args[i+1:]
args = args[:i]
}
}

// fmt.Println("jump", args, rest)

args = append([]string{"jumps"}, args...)

V := rCRT.CueValue.Lookup(args...)

switch K := V.Kind(); K {

// we found a jump, now leap!
case cue.StringKind:
str, err := V.String()
if err != nil {
return err
}
return runJumpCommand(str, rest)

// there's more so print it
case cue.StructKind:
bytes, err := format.Node(V.Syntax())
if err != nil {
return err
}

fmt.Printf("%s\n\n", string(bytes))
return nil

// TODO, case for list of strings for multiple commands to run?

// some sort of error or non-existant path
case cue.BottomKind:
return fmt.Errorf("Path not found: %q (or it has errors)", args)

// unhandled kind
default:
return fmt.Errorf("Unsupported kind '%q' at requested path %q", K, args)
}

return nil
}

func runJumpCommand(cmd string, args []string) error {
// TODO, handle or pass in flags
// TODO, how to parse? is that why runtime is a thing? ('bash -c'), except we are assuming these are all bash
// fmt.Printf("running: %q\n", cmd)

out, err := yagu.BashTmpScriptWithArgs(cmd, args)
fmt.Print(out)
return err
}
50 changes: 49 additions & 1 deletion lib/yagu/exec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
package yagu

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)

func Bash(script string) (string, error) {
cmd := exec.Command("bash", "-c", script)
cmd := exec.Command("bash", "-p", "-c", script)
out, err := cmd.CombinedOutput()
return string(out), err
}

func BashTmpScriptWithArgs(script string, args []string) (string, error) {

// make sure we have our temp dir
err := Mkdir(".hof")
if err != nil {
return "", err
}

// create temp file
f, err := ioutil.TempFile(".hof", "tmp-jumps-*")
if err != nil {
return "", err
}
// and cleanup for sure
defer os.Remove(f.Name())

// write script
_, err = fmt.Fprintln(f, script)
if err != nil {
return "", err
}

// close
err = f.Close()
if err != nil {
return "", err
}

// make exec'd
err = os.Chmod(f.Name(), 0755)
if err != nil {
return "", err
}

// prepare args to bash invocation as a "/path/to/scripts.sh"
doargs := append([]string{ f.Name()}, args...)
dorun := strings.Join(doargs, " ")

// run script and return output / status
cmd := exec.Command("bash", "-p", "-c", dorun)
out, err := cmd.CombinedOutput()
return string(out), err
}
Expand Down
7 changes: 7 additions & 0 deletions schema/resources/hook.cue
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
package resources

#Hooks: [N=string]: #Hook & { Name: N, ... }
#Hook: {
Name: string
Evt: string
Cmds: [...string]
}
8 changes: 8 additions & 0 deletions schema/resources/jump.cue
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
package resources

#Jumps: #Jump1 | #Jump2 | #Jump3 | #Jump4 | #Jump5

#Jump1: [_=string]: string
#Jump2: [_=string]: [_=string]: string
#Jump3: [_=string]: [_=string]: [_=string]: string
#Jump4: [_=string]: [_=string]: [_=string]: [_=string]: string
#Jump5: [_=string]: [_=string]: [_=string]: [_=string]: [_=string]: string

0 comments on commit f5dd77d

Please sign in to comment.