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

[pipes-functions] Adding exit-status, concat, args and close functions #351

Merged
merged 1 commit into from
Sep 29, 2024
Merged
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
68 changes: 68 additions & 0 deletions evaldo/builtins_pipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,74 @@ var Builtins_pipes = map[string]*env.Builtin{
},
},

"exit-status": {
Argsn: 1,
Doc: "Returns the integer exit status of a previous command. This will be zero unless the pipe's error status is set and the error matches the pattern.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch p := arg0.(type) {
case env.Native:
switch pipe := p.Value.(type) {
case *script.Pipe:
status := pipe.ExitStatus()
return *env.NewInteger(int64(status))
default:
return MakeNativeArgError(ps, 1, []string{"script-pipe"}, "p-exit-status")
}
default:
return MakeArgError(ps, 1, []env.Type{env.NativeType}, "p-exit-status")
}
},
},

"args": {
Argsn: 0,
Doc: "Creates a pipe containing the program's command-line arguments from os.Args, excluding the program name, 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 {
newPipe := script.Args()
return *env.NewNative(ps.Idx, newPipe, "script-pipe")
},
},

"concat": {
Argsn: 1,
Doc: "concat reads paths from the pipe, one per line, and produces the contents of all the corresponding files in sequence. If there are any errors (for example, non-existent files), these will be ignored, execution will continue, and the pipe's error status will not be set.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch p := arg0.(type) {
case env.Native:
switch pipe := p.Value.(type) {
case *script.Pipe:
newPipe := pipe.Concat()
return *env.NewNative(ps.Idx, newPipe, "script-pipe")
default:
return MakeNativeArgError(ps, 1, []string{"script-pipe"}, "p-concat")
}
default:
return MakeArgError(ps, 1, []env.Type{env.NativeType}, "p-concat")
}
},
},

"close": {
Argsn: 1,
Doc: "Closes the pipe's associated reader.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch p := arg0.(type) {
case env.Native:
switch pipe := p.Value.(type) {
case *script.Pipe:
closeErr := pipe.Close()
if closeErr != nil {
return *env.NewError("Error closing pipe")
}
return nil
default:
return MakeNativeArgError(ps, 1, []string{"script-pipe"}, "p-close")
}
default:
return MakeArgError(ps, 1, []env.Type{env.NativeType}, "p-close")
}
},
},
// GOPSUTIL

}