-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
118 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package builtin | ||
|
||
import ( | ||
"github.com/c4s4/neon/neon/build" | ||
guuid "github.com/google/uuid" | ||
) | ||
|
||
func init() { | ||
build.AddBuiltin(build.BuiltinDesc{ | ||
Name: "uuid", | ||
Func: uuid, | ||
Help: `Generate a random (version 4) UUID. | ||
Returns: | ||
- Generated UUID as a string. | ||
Examples: | ||
# generate an UUID | ||
uuid() | ||
# returns: "9063eafd-40b9-48d9-bf90-ce44e9207821"`, | ||
}) | ||
} | ||
|
||
func uuid() string { | ||
UUID, err := guuid.NewRandom() | ||
if err != nil { | ||
panic("ERROR generating UUID: " + err.Error()) | ||
} | ||
return UUID.String() | ||
} |
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,9 @@ | ||
package builtin | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestUuid(t *testing.T) { | ||
AssertRegexp(uuid(), `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, 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,58 @@ | ||
package task | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"reflect" | ||
|
||
"github.com/c4s4/neon/neon/build" | ||
) | ||
|
||
func init() { | ||
build.AddTask(build.TaskDesc{ | ||
Name: "start", | ||
Func: start, | ||
Args: reflect.TypeOf(startArgs{}), | ||
Help: `Start a command in background and put its PID in a variable. | ||
Arguments: | ||
- start: command to run (string). | ||
- pid: name of the variable to put PID into. | ||
Examples: | ||
# start a command in background and put its PID in 'pid' variable | ||
- start: 'ls -al' | ||
pid: pid | ||
Notes: | ||
- Commands run in the shell defined by shell field at the root of the build file | ||
(or 'sh -c' on Unix and 'cmd /c' on Windows by default).`, | ||
}) | ||
} | ||
|
||
type startArgs struct { | ||
Start string `neon:"name=start,string"` | ||
Pid string `neon:"name=pid,string"` | ||
} | ||
|
||
func start(context *build.Context, args interface{}) error { | ||
params := args.(startArgs) | ||
cmd := exec.Command(params.Start) | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
var err error | ||
cmd.Env, err = context.EvaluateEnvironment() | ||
if err != nil { | ||
return fmt.Errorf("building environment: %v", err) | ||
} | ||
cmd.Environ() | ||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
context.SetProperty(params.Pid, cmd.Process.Pid) | ||
return nil | ||
} |