-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement several pipeline/os tasks; general cleanup
- Loading branch information
Showing
11 changed files
with
203 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package os | ||
|
||
import ( | ||
"fmt" | ||
g_os "os" | ||
|
||
"cuelang.org/go/cue" | ||
"cuelang.org/go/tools/flow" | ||
|
||
"github.com/hofstadter-io/cuetils/utils" | ||
) | ||
|
||
type ReadFileTask struct { | ||
// might need F to be an object the helps us to | ||
// understand how to load the contents back in | ||
// such as a string, bytes, or a cue struct | ||
F cue.Value // the filename as a cue value | ||
C cue.Value // the file contents | ||
} | ||
|
||
func (T* ReadFileTask) Run(t *flow.Task, err error) error { | ||
|
||
if err != nil { | ||
fmt.Println("Dep error", err) | ||
} | ||
|
||
v := t.Value() | ||
|
||
f := v.LookupPath(cue.ParsePath("#F")) | ||
|
||
fn, err := f.String() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bs, err := g_os.ReadFile(fn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// switch on c's type to fill appropriately | ||
c := v.LookupPath(cue.ParsePath("Contents")) | ||
|
||
var res cue.Value | ||
switch k := c.IncompleteKind(); k { | ||
case cue.StringKind: | ||
res = v.FillPath(cue.ParsePath("Contents"), string(bs)) | ||
case cue.BytesKind: | ||
res = v.FillPath(cue.ParsePath("Contents"), bs) | ||
|
||
case cue.StructKind: | ||
ctx := v.Context() | ||
c := ctx.CompileBytes(bs) | ||
if c.Err() != nil { | ||
return c.Err() | ||
} | ||
res = v.FillPath(cue.ParsePath("Contents"), c) | ||
|
||
default: | ||
return fmt.Errorf("Unsupported Content type in ReadFile task: %q", k) | ||
} | ||
|
||
|
||
// Use fill to "return" a result to the workflow engine | ||
t.Fill(res) | ||
|
||
attr := v.Attribute("print") | ||
err = utils.PrintAttr(attr, res) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package os | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
g_os "os" | ||
|
||
"cuelang.org/go/cue" | ||
"cuelang.org/go/tools/flow" | ||
|
||
"github.com/hofstadter-io/cuetils/utils" | ||
) | ||
|
||
type StdinTask struct {} | ||
|
||
func (T* StdinTask) Run(t *flow.Task, err error) error { | ||
|
||
if err != nil { | ||
fmt.Println("Dep error", err) | ||
} | ||
|
||
v := t.Value() | ||
|
||
msg := v.LookupPath(cue.ParsePath("#Msg")) | ||
if msg.Err() != nil { | ||
return err | ||
} else if msg.Exists() { | ||
m, err := msg.String() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Print(m) | ||
} | ||
|
||
reader := bufio.NewReader(g_os.Stdin) | ||
text, _ := reader.ReadString('\n') | ||
|
||
res := v.FillPath(cue.ParsePath("Contents"), text) | ||
// Use fill to "return" a result to the workflow engine | ||
t.Fill(res) | ||
|
||
attr := v.Attribute("print") | ||
err = utils.PrintAttr(attr, v) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package os | ||
|
||
import ( | ||
"fmt" | ||
|
||
"cuelang.org/go/cue" | ||
"cuelang.org/go/tools/flow" | ||
|
||
"github.com/hofstadter-io/cuetils/utils" | ||
) | ||
|
||
type StdoutTask struct {} | ||
|
||
func (T* StdoutTask) Run(t *flow.Task, err error) error { | ||
|
||
if err != nil { | ||
fmt.Println("Dep error", err) | ||
} | ||
|
||
v := t.Value() | ||
|
||
o := v.LookupPath(cue.ParsePath("#O")) | ||
|
||
fmt.Println(o) | ||
|
||
attr := v.Attribute("print") | ||
err = utils.PrintAttr(attr, v) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tasks: { | ||
@pipeline(readfile) | ||
r: { #F: "readfile_001.txt", Contents: string } @task(os/readfile) @print(Contents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
exec cuetils pipeline in.cue | ||
# cmp stdout golden.stdout | ||
|
||
-- pipe.cue -- | ||
tasks: { | ||
@pipeline(readfile) | ||
r1: { #F: "readfile_001.txt", Resp: _ } @task(os/readfile) @print("#Req",Resp) | ||
p1: { #X: r1.Resp, #P: pick } @task(st/pick) @print(Out) | ||
} | ||
|
||
-- in.json -- | ||
"x": { | ||
"a": { | ||
"b": "B" | ||
}, | ||
"b": 1 | ||
"c": 2 | ||
"d": "D" | ||
} | ||
|
||
-- golden.stdout -- | ||
t.b.d. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import "encoding/json" | ||
|
||
tasks: { | ||
@pipeline(readfile) | ||
r: { #F: "../tree.json", Contents: string } @task(os/readfile) | ||
j: json.Unmarshal(r.Contents) | ||
p: { #X: j, #P: { tree: cow: _ } } @task(st/pick) | ||
|
||
final: { #O: p.Out.tree } @task(os/stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import "strings" | ||
|
||
tasks: { | ||
@pipeline(stdin) | ||
input: { #Msg: "enter text: " } @task(os/stdin) | ||
final: { #O: strings.ToUpper(input.Contents) } @task(os/stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters