-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.go
executable file
·95 lines (73 loc) · 2.2 KB
/
activity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package xsltactivity
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
)
func init() {
_ = activity.Register(&Activity{}) //activity.Register(&Activity{}, New) to create instances using factory method 'New'
}
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
//New optional factory method, should be used if one activity instance per configuration is desired
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
ctx.Logger().Debugf("Setting: %s", s.ASetting)
act := &Activity{} //add aSetting to instance
return act, nil
}
// Activity is an sample Activity that can be used as a base to create a custom activity
type Activity struct {
}
// Metadata returns the activity's metadata
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
// Eval implements api.Activity.Eval - Logs the Message
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
input := &Input{}
err = ctx.GetInputObject(input)
if err != nil {
return true, err
}
// fmt.Println("Inside xsltactivity Eval")
fmt.Println("Going to create temp file")
// ctx.Logger().Debugf("Input: %s", input.Xml)
xml, xmlErr := ioutil.TempFile("", "xml")
if xmlErr != nil {
fmt.Println("Going to create temp file - Error: ", xmlErr)
return false, xmlErr
}
defer os.Remove(xml.Name())
fmt.Println(xml.Name())
fmt.Println("Temp file created, going to write")
_, xmlWriteErr := xml.WriteString(input.Xml)
if xmlWriteErr != nil {
fmt.Println("Temp file created, going to write - Error: ", xmlWriteErr)
return false, xmlWriteErr
}
fmt.Println("String written, lets execute")
cmd := exec.Cmd{
Args: []string{"xsltproc", input.XslFile, xml.Name()},
Env: os.Environ(),
Path: "/usr/bin/xsltproc",
}
xmlString, cmdErr := cmd.Output()
if cmdErr != nil {
fmt.Println("Error running CMD: ", cmdErr)
return false, cmdErr
}
// xmlString := "Boo!"
output := &Output{OutputXml: string(xmlString)}
err = ctx.SetOutputObject(output)
if err != nil {
return true, err
}
return true, nil
}