1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00

Added possibility to use any type as a file content (#544)

This commit is contained in:
Tim Voronov 2020-08-07 22:09:02 -04:00 committed by GitHub
parent 734b737263
commit d3c28a43ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 17 deletions

View File

@ -20,16 +20,27 @@ import (
// * w - Write (Default): will create a file if the specified file does not exist
func Write(_ context.Context, args ...core.Value) (core.Value, error) {
err := validateRequiredWriteArgs(args)
if err != nil {
return values.None, err
}
fpath := args[0].String()
data := args[1].(values.Binary)
arg1 := args[1]
var data []byte
if arg1.Type() == types.Binary {
data = arg1.(values.Binary)
} else {
data = []byte(arg1.String())
}
params := defaultParams
if len(args) == 3 {
params, err = parseParams(args[2])
if err != nil {
return values.None, core.Error(
err,
@ -40,12 +51,15 @@ func Write(_ context.Context, args ...core.Value) (core.Value, error) {
// 0666 - read & write
file, err := os.OpenFile(fpath, params.ModeFlag, 0666)
if err != nil {
return values.None, core.Error(err, "open file")
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return values.None, core.Error(err, "write file")
}
@ -55,18 +69,15 @@ func Write(_ context.Context, args ...core.Value) (core.Value, error) {
func validateRequiredWriteArgs(args []core.Value) error {
err := core.ValidateArgs(args, 2, 3)
if err != nil {
return core.Error(err, "validate arguments number")
return err
}
pairs := []core.PairValueType{
core.NewPairValueType(args[0], types.String),
core.NewPairValueType(args[1], types.Binary),
}
err = core.ValidateType(args[0], types.String)
err = core.ValidateValueTypePairs(pairs...)
if err != nil {
return core.Error(err, "validate arguments")
return err
}
return nil
@ -83,20 +94,21 @@ var defaultParams = parsedParams{
}
func parseParams(value core.Value) (parsedParams, error) {
obj, ok := value.(*values.Object)
if !ok {
return parsedParams{}, core.Error(
core.ErrInvalidArgument,
"value should be an object",
)
err := core.ValidateType(value, types.Object)
if err != nil {
return parsedParams{}, err
}
obj := value.(*values.Object)
params := defaultParams
modestr, exists := obj.Get(values.NewString("mode"))
if exists {
if exists {
flag, err := parseWriteMode(modestr.String())
if err != nil {
return parsedParams{}, core.Error(
core.ErrInvalidArgument,
@ -142,10 +154,8 @@ func parseWriteMode(s string) (int, error) {
switch letters[0] {
case 'a':
flag |= os.O_APPEND
case 'w':
flag |= os.O_TRUNC
default:
return -1, core.Errorf(
core.ErrInvalidArgument,

1
test.txt Normal file
View File

@ -0,0 +1 @@
2020-08-07 22:07:40.24257 -0400 EDT m=+45.728591354