1
+ // DO NOT EDIT - IMPLEMENTS THE RUNTIME PROTOCOL AN SHOULD NOT BE CHANGED
2
+ /*
3
+ * Licensed to the Apache Software Foundation (ASF) under one or more
4
+ * contributor license agreements. See the NOTICE file distributed with
5
+ * this work for additional information regarding copyright ownership.
6
+ * The ASF licenses this file to You under the Apache License, Version 2.0
7
+ * (the "License"); you may not use this file except in compliance with
8
+ * the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ package main
20
+
21
+ import (
22
+ "bufio"
23
+ "bytes"
24
+ "encoding/json"
25
+ "fmt"
26
+ "io"
27
+ "log"
28
+ "os"
29
+ "reflect"
30
+ "strings"
31
+ )
32
+
33
+ // OwExecutionEnv is the execution environment set at compile time
34
+ var OwExecutionEnv = ""
35
+
36
+ func main () {
37
+ // check if the execution environment is correct
38
+ if OwExecutionEnv != "" && OwExecutionEnv != os .Getenv ("__OW_EXECUTION_ENV" ) {
39
+ fmt .Println ("Execution Environment Mismatch" )
40
+ fmt .Println ("Expected: " , OwExecutionEnv )
41
+ fmt .Println ("Actual: " , os .Getenv ("__OW_EXECUTION_ENV" ))
42
+ os .Exit (1 )
43
+ }
44
+
45
+ // debugging
46
+ var debug = os .Getenv ("OW_DEBUG" ) != ""
47
+ if debug {
48
+ f , err := os .OpenFile ("/tmp/action.log" , os .O_RDWR | os .O_CREATE | os .O_APPEND , 0666 )
49
+ if err == nil {
50
+ log .SetOutput (f )
51
+ }
52
+ log .Printf ("Environment: %v" , os .Environ ())
53
+ }
54
+
55
+ resultKind := reflect .TypeOf (Main ).Out (0 ).Kind ()
56
+ if resultKind != reflect .Map && resultKind != reflect .Slice && resultKind != reflect .Array {
57
+ fmt .Println ("Support map and slice and array only" )
58
+ os .Exit (1 )
59
+ }
60
+
61
+ // input
62
+ out := os .NewFile (3 , "pipe" )
63
+ defer out .Close ()
64
+ reader := bufio .NewReader (os .Stdin )
65
+
66
+ // acknowledgement of started action
67
+ fmt .Fprintf (out , `{ "ok": true}%s` , "\n " )
68
+ if debug {
69
+ log .Println ("action started" )
70
+ }
71
+
72
+ // read-eval-print loop
73
+ for {
74
+ // read one line
75
+ inbuf , err := reader .ReadBytes ('\n' )
76
+ if err != nil {
77
+ if err != io .EOF {
78
+ log .Println (err )
79
+ }
80
+ break
81
+ }
82
+ if debug {
83
+ log .Printf (">>>'%s'>>>" , inbuf )
84
+ }
85
+ // parse one line
86
+ var input map [string ]interface {}
87
+ err = json .Unmarshal (inbuf , & input )
88
+ if err != nil {
89
+ log .Println (err .Error ())
90
+ fmt .Fprintf (out , "{ error: %q}\n " , err .Error ())
91
+ continue
92
+ }
93
+ if debug {
94
+ log .Printf ("%v\n " , input )
95
+ }
96
+ // set environment variables
97
+ for k , v := range input {
98
+ if k == "value" {
99
+ continue
100
+ }
101
+ if s , ok := v .(string ); ok {
102
+ os .Setenv ("__OW_" + strings .ToUpper (k ), s )
103
+ }
104
+ }
105
+ // get payload if not empty
106
+ isJsonObjectParam := true
107
+ var payloadForJsonObject map [string ]interface {}
108
+ var payloadForJsonArray []interface {}
109
+ if value , ok := input ["value" ].(map [string ]interface {}); ok {
110
+ payloadForJsonObject = value
111
+ } else {
112
+ if value , ok := input ["value" ].([]interface {}); ok {
113
+ payloadForJsonArray = value
114
+ isJsonObjectParam = false
115
+ }
116
+ }
117
+ // process the request
118
+ var result interface {}
119
+ funcMain := reflect .ValueOf (Main )
120
+ if isJsonObjectParam {
121
+ param := []reflect.Value {reflect .ValueOf (payloadForJsonObject )}
122
+ reflectResult := funcMain .Call (param )
123
+ result = reflectResult [0 ].Interface ()
124
+ } else {
125
+ param := []reflect.Value {reflect .ValueOf (payloadForJsonArray )}
126
+ reflectResult := funcMain .Call (param )
127
+ result = reflectResult [0 ].Interface ()
128
+ }
129
+ // encode the answer
130
+ output , err := json .Marshal (& result )
131
+ if err != nil {
132
+ log .Println (err .Error ())
133
+ fmt .Fprintf (out , "{ error: %q}\n " , err .Error ())
134
+ continue
135
+ }
136
+ output = bytes .Replace (output , []byte ("\n " ), []byte ("" ), - 1 )
137
+ if debug {
138
+ log .Printf ("<<<'%s'<<<" , output )
139
+ }
140
+ fmt .Fprintf (out , "%s\n " , output )
141
+ }
142
+ }
0 commit comments