-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #887 from lmorg/develop
v6.4
- Loading branch information
Showing
123 changed files
with
3,284 additions
and
986 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
37 changes: 0 additions & 37 deletions
37
builtins/core/io/file_doc.yaml → builtins/core/io/pt_doc.yaml
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,18 @@ | ||
package io_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lmorg/murex/test" | ||
) | ||
|
||
func TestPipeTelemetry(t *testing.T) { | ||
tests := []test.MurexTest{ | ||
{ | ||
Block: `tout * 12345 -> pt`, | ||
Stdout: `12345`, | ||
}, | ||
} | ||
|
||
test.RunMurexTests(tests, t) | ||
} |
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,123 @@ | ||
package io | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/lmorg/murex/lang" | ||
"github.com/lmorg/murex/lang/state" | ||
"github.com/lmorg/murex/lang/types" | ||
"github.com/lmorg/murex/utils/lists" | ||
) | ||
|
||
func init() { | ||
lang.DefineMethod(">", cmdTruncateFile, types.Any, types.Null) | ||
lang.DefineMethod("fwrite", cmdTruncateFile, types.Any, types.Null) | ||
lang.DefineMethod(">>", cmdAppendFile, types.Any, types.Null) | ||
lang.DefineMethod("fappend", cmdAppendFile, types.Any, types.Null) | ||
} | ||
|
||
const ( | ||
_WAIT_EOF_SHORT = "-w" | ||
_WAIT_EOF_LONG = "--wait-for-eof" | ||
_IGNORE_PIPELINE_SHORT = "-i" | ||
_IGNORE_PIPELINE_LONG = "--ignore-pipeline-check" | ||
) | ||
|
||
func cmdTruncateFile(p *lang.Process) error { return writeFile(p, truncateFile) } | ||
func cmdAppendFile(p *lang.Process) error { return writeFile(p, appendFile) } | ||
|
||
func writeFile(p *lang.Process, fn func(io.Reader, string) error) error { | ||
p.Stdout.SetDataType(types.Null) | ||
|
||
filename, err := p.Parameters.String(0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !p.IsMethod { | ||
// nothing to write | ||
return fn(bytes.NewBuffer([]byte{}), filename) | ||
} | ||
|
||
if filename == _IGNORE_PIPELINE_SHORT || filename == _IGNORE_PIPELINE_LONG { | ||
parameter2, err := p.Parameters.String(1) | ||
if err == nil { | ||
return fn(p.Stdin, parameter2) | ||
} | ||
// no second parameter so lets assume the flag was actually a file name | ||
} | ||
|
||
wait := filename == _WAIT_EOF_SHORT || filename == _WAIT_EOF_LONG | ||
|
||
if wait { | ||
parameter2, err := p.Parameters.String(1) | ||
if err == nil { | ||
filename = parameter2 | ||
|
||
} else { | ||
// no second parameter so lets assume the flag was actually a file name | ||
wait = false | ||
} | ||
} | ||
|
||
if !wait { | ||
wait = isFileOpen(p, filename) | ||
if wait { | ||
_, _ = p.Stderr.Writeln([]byte(fmt.Sprintf("warning: '%s' appears as a parameter elsewhere in the pipeline so I'm going to cache the file in RAM before writing to disk.\n : This message can be suppressed using `%s` or `%s`.", filename, _WAIT_EOF_LONG, _IGNORE_PIPELINE_LONG))) | ||
} | ||
} | ||
|
||
if !wait { | ||
return fn(p.Stdin, filename) | ||
} | ||
|
||
b, err := p.Stdin.ReadAll() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return fn(bytes.NewReader(b), filename) | ||
} | ||
|
||
func isFileOpen(p *lang.Process, filename string) bool { | ||
p = p.Previous | ||
for { | ||
if p.State.Get() < state.Executing { | ||
continue | ||
} | ||
if lists.Match(p.Parameters.StringArray(), filename) { | ||
return true | ||
} | ||
if !p.IsMethod || p.Id == lang.ShellProcess.Id { | ||
return false | ||
} | ||
p = p.Previous | ||
} | ||
} | ||
|
||
func truncateFile(reader io.Reader, filename string) error { | ||
file, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
|
||
_, err = io.Copy(file, reader) | ||
return err | ||
} | ||
|
||
func appendFile(reader io.Reader, filename string) error { | ||
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0664) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer file.Close() | ||
|
||
_, err = io.Copy(file, reader) | ||
return err | ||
} |
Oops, something went wrong.