-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df5de11
commit 855523c
Showing
3 changed files
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package dynaml | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func func_write(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) { | ||
var err error | ||
info := DefaultInfo() | ||
|
||
if len(arguments) < 2 || len(arguments) > 3 { | ||
return info.Error("write requires two arguments") | ||
} | ||
file, ok := getArg(0, arguments[0]) | ||
if !ok || file == "" { | ||
return info.Error("file argument must be a non-empty string") | ||
} | ||
data, _ := getArg(1, arguments[1]) | ||
permissions := int64(0644) | ||
if len(arguments) == 3 { | ||
switch v := arguments[2].(type) { | ||
case string: | ||
permissions, err = strconv.ParseInt(v, 10, 64) | ||
if err != nil { | ||
return info.Error("permissions must be given as int or int string: %s", err) | ||
} | ||
case int64: | ||
permissions = v | ||
default: | ||
return info.Error("permissions must be given as int or int string") | ||
} | ||
} | ||
|
||
err = ioutil.WriteFile(file, []byte(data), os.FileMode(permissions)) | ||
if err != nil { | ||
return info.Error("cannot write file: %s", err) | ||
} | ||
|
||
return convertOutput([]byte(data)) | ||
} |