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

added support for group, a type = 3 block, which is do-ne by default,… #320

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
2 changes: 1 addition & 1 deletion evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7855,7 +7855,7 @@ func RegisterBuiltins(ps *env.ProgramState) {
RegisterBuiltins2(Builtins_ssh, ps, "ssh")
RegisterBuiltins2(Builtins_console, ps, "console")
RegisterBuiltinsInContext(Builtins_math, ps, "math")
RegisterBuiltinsInContext(Builtins_devops, ps, "devops")
RegisterBuiltinsInContext(Builtins_devops, ps, "os")
// ## Archived modules
// RegisterBuiltins2(Builtins_gtk, ps, "gtk")
// RegisterBuiltins2(Builtins_nats, ps, "nats")
Expand Down
73 changes: 51 additions & 22 deletions evaldo/builtins_psutil.go → evaldo/builtins_devops.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
//go:build add_psutil
// +build add_psutil
//go:build !no_devops
// +build !no_devops

package evaldo

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/bitfield/script"
"github.com/refaktor/rye/env"

"github.com/bitfield/script"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/load"
Expand All @@ -40,7 +39,37 @@ func FileExists(filePath string) int {

var Builtins_devops = map[string]*env.Builtin{

"cwd": {
Argsn: 0,
Doc: "Returns current working directory.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
path, err := os.Getwd()
if err != nil {
return MakeBuiltinError(ps, err.Error(), "cwd")
}
return *env.NewUri1(ps.Idx, "file://"+path)
},
},

"cd": {
Argsn: 1,
Doc: "Changes current directory.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch path := arg0.(type) {
case env.Uri:

err := os.Chdir(path.GetPath())
if err != nil {
return MakeBuiltinError(ps, err.Error(), "cd")
}
return arg0
default:
return MakeArgError(ps, 1, []env.Type{env.UriType}, "cd")
}
},
},

"cd_": {
Argsn: 1,
Doc: "Changes current directory.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down Expand Up @@ -108,7 +137,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"cwd": {
"cwd_": {
Argsn: 0,
Doc: "Returns current working directory.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -121,7 +150,7 @@ var Builtins_devops = map[string]*env.Builtin{
Doc: "Returns current working directory.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {

files, err := ioutil.ReadDir(ps.WorkingPath + "/")
files, err := os.ReadDir(".")
if err != nil {
return MakeBuiltinError(ps, "Error reading directory:"+err.Error(), "ls")
}
Expand All @@ -140,7 +169,7 @@ var Builtins_devops = map[string]*env.Builtin{

// SCRIPT PIPES

"p-new-file": {
"(file)": {
Argsn: 1,
Doc: "Creates a new pipe object from a file.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -154,7 +183,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-new-find-files": {
"(find-files)": {
Argsn: 1,
Doc: "Creates a pipe object listing all the files in the directory and its subdirectories recursively, one per line.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -168,7 +197,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-new-list-files": {
"(list-files)": {
Argsn: 1,
Doc: "Creates a pipe object listing all the files in the directory, one per line. Accepts and URI or glob pattern.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -185,7 +214,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-new-block": {
"(block)": {
Argsn: 1,
Doc: "Creates a pipe object from a block of strings, one per line.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -208,7 +237,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-new-echo": {
"(echo)": {
Argsn: 1,
Doc: "Creates a pipe object from a string.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -222,7 +251,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-new-if-exists": {
"(if-exists)": {
Argsn: 1,
Doc: "Creates a pipe object from a file if it exists, otherwise returns an empty pipe object.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -236,7 +265,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-new-exec": {
"(exec)": {
Argsn: 1,
Doc: "Creates a pipe object from a command that is executed.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -250,7 +279,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-exec": {
"exec": {
Argsn: 2,
Doc: "Executes a command by sending it the contents of the pipe as input and returns a pipe object.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -274,7 +303,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-exec-for-each": {
"exec-for-each": {
Argsn: 2,
Doc: "Executes a command from a Go template for each line in the pipe and returns a pipe object with the output of each command.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -298,7 +327,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-string": {
"string": {
Argsn: 1,
Doc: "Returns pipe contents as a string.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -320,7 +349,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-write-file": {
"write-file": {
Argsn: 2,
Doc: "Writes pipe contents to a file and returns the number of bytes written.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -334,7 +363,7 @@ var Builtins_devops = map[string]*env.Builtin{
if err != nil {
return MakeBuiltinError(ps, err.Error(), "p-write-file")
}
return *env.NewInteger(int64(i))
return *env.NewInteger(i)
default:
return MakeArgError(ps, 2, []env.Type{env.UriType}, "p-write-file")
}
Expand All @@ -347,7 +376,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-stdout": {
"stdout": {
Argsn: 1,
Doc: "Prints pipe contents to stdout and returns the number of bytes written.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -369,7 +398,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-first-n": {
"first-n": {
Argsn: 2,
Doc: "Returns a pipe with the first n lines from the pipe.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand All @@ -393,7 +422,7 @@ var Builtins_devops = map[string]*env.Builtin{
},
},

"p-last-n": {
"tail": {
Argsn: 2,
Doc: "Returns a pipe with the last n lines from the pipe.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down Expand Up @@ -856,7 +885,7 @@ var Builtins_devops = map[string]*env.Builtin{
*env.NewInteger(int64(usage.Total)),
*env.NewInteger(int64(usage.Used)),
*env.NewInteger(int64(usage.Free)),
*env.NewDecimal(float64(usage.UsedPercent)),
*env.NewDecimal(usage.UsedPercent),
*env.NewInteger(int64(usage.InodesUsed)),
*env.NewInteger(int64(usage.InodesFree)),
*env.NewInteger(int64(usage.InodesUsedPercent)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !add_psutil
// +build !add_psutil
//go:build no_devops
// +build no_devops

package evaldo

Expand Down
6 changes: 6 additions & 0 deletions evaldo/evaldo.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ func EvalExpressionConcrete(ps *env.ProgramState) *env.ProgramState {
}
ps.Ser = ser
ps.Res = *env.NewBlock(*env.NewTSeries(res))
} else if block.Mode == 2 {
ser := ps.Ser
ps.Ser = block.Series
EvalBlock(ps)
ps.Ser = ser
// return ps.Res
} else {
ps.Res = object
}
Expand Down