Beispiel #1
0
func command(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
	var err error
	stmt.Args, err = gohanscript.MapToValue(util.MaybeMap(stmt.RawData["args"]))
	if err != nil {
		return nil, err
	}
	stmt.Args["command"], err = gohanscript.NewValue(stmt.RawData["command"])
	if err != nil {
		return nil, err
	}
	return func(context *gohanscript.Context) (interface{}, error) {
		chdir := stmt.Arg("chdir", context)
		if chdir != nil {
			currentDir, _ := filepath.Abs(".")
			os.Chdir(util.MaybeString(chdir))
			defer os.Chdir(currentDir)
		}
		command := util.MaybeString(stmt.Arg("command", context))
		parts := strings.Fields(command)
		result, err := exec.Command(parts[0], parts[1:]...).CombinedOutput()
		return string(result), err
	}, nil
}
Beispiel #2
0
func backgroundJob(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
	stmts, err := gohanscript.MakeStmts(stmt.File, stmt.RawNode["job"])
	if err != nil {
		return nil, stmt.Errorf("background code error: %s", err)
	}
	queueArg, err := gohanscript.NewValue(stmt.RawData["queue"])
	if err != nil {
		return nil, stmt.Errorf("background code error: %s", err)
	}
	f, err := gohanscript.StmtsToFunc("job", stmts)
	if err != nil {
		return nil, err
	}
	return func(context *gohanscript.Context) (interface{}, error) {
		queue := queueArg.Value(context).(*job.Queue)
		newContext := context.Extend(map[string]interface{}{})
		queue.Add(
			job.NewJob(func() {
				f(newContext)
				newContext.VM.Stop()
			}))
		return nil, nil
	}, nil
}