Skip to content

Commit 3cafb85

Browse files
msciabarrafrancescotimperi
authored andcommitted
go support
1 parent 3603e8d commit 3cafb85

File tree

7 files changed

+222
-5
lines changed

7 files changed

+222
-5
lines changed

ide/deploy/deploy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
MAINS = ["__main__.py", "index.js"]
18+
MAINS = ["__main__.py", "index.js", "index.php", "main.go"]
1919

2020
import os
2121
from os.path import exists, isdir

ide/go/exec.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nuvolaris/runtime-golang-v1.22

ide/go/launcher.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

ide/go/nuvfile.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
version: '3'
19+
20+
tasks:
21+
22+
clean:
23+
silent: true
24+
desc: clean action DIR
25+
requires: {var: [DIR]}
26+
cmds:
27+
- test -d "{{.DIR}}" || die "{{.DIR}} not found or not a directory"
28+
- /usr/bin/rm -v -f {{.DIR}}/main_.go {{.DIR}}.zip {{.DIR}}/exec
29+
30+
zip:
31+
silent: true
32+
desc: prepare zip for action in DIR
33+
requires: {var: [DIR]}
34+
cmds:
35+
- test -d "{{.DIR}}" || die "{{.DIR}} not found or not a directory"
36+
- /usr/bin/zip -v "{{.DIR}}.zip" exec.env
37+
- /bin/cp -v launcher.go "{{.DIR}}/main_.go"
38+
generates:
39+
- "{{.DIR}}/main_.go"
40+
41+
action:
42+
deps: [zip]
43+
requires: {var: [DIR]}
44+
silent: true
45+
desc: compile action in DIR
46+
dir: "{{.DIR}}"
47+
cmds:
48+
- test -d "{{.DIR}}" || die "{{.DIR}} not found or not a directory"
49+
- go build -o exec
50+
- /usr/bin/zip -u "{{.DIR}}.zip" exec
51+
52+

ide/nuvfile.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tasks:
3737
echo "*** 'deploy' (alias for 'nuv ide deploy') to deploy your app to https://$NUVDEV_USERNAME.nuvolaris.dev"
3838
echo "*** 'devel' (alias for 'nuv ide devel') to start incremental development mode"
3939
echo "*** 'login' (alias for 'nuv ide login') to login as a different user"
40-
echo "*** 'nuv ide' for more informations
40+
echo "*** 'nuv ide' for more informations"
4141
echo "*** https://nuvolaris.github.io for online documentation"
4242
echo "**************************************************************************************"
4343
@@ -244,6 +244,8 @@ tasks:
244244
/bin/rm -rvf "$NUV_PWD"/packages/*/*/virtualenv/
245245
echo "*** removing node_modules"
246246
/bin/rm -rvf "$NUV_PWD"/packages/*/*/node_modules/
247+
echo "*** removing vendor"
248+
/bin/rm -rvf "$NUV_PWD"/packages/*/*/vendor/
247249
echo "*** removing .zip"
248250
/bin/rm -vf "$NUV_PWD"/packages/*/*.zip
249251
else die "no packages in current directory"
@@ -261,3 +263,9 @@ tasks:
261263

262264
nodejs:
263265
desc: nodejs subcommand
266+
267+
php:
268+
desc: php subcommand
269+
270+
golang:
271+
desc: go subcommand

ide/nuvopts.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Usage:
1313
ide kill
1414
ide python
1515
ide nodejs
16+
ide php
1617

1718
Commands:
1819
ide login login in nuvolaris.dev
@@ -27,3 +28,5 @@ Commands:
2728
ide shell start a shell with current env
2829
ide python python subcommands
2930
ide nodejs nodejs subcommands
31+
ide php php subcommands
32+
ide go go subcommand

ide/util/nuvfile.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ tasks:
3030
then nuv ide python zip DIR="$(realpath "packages/{{.A}}")"
3131
elif test -e "packages/{{.A}}/package.json"
3232
then nuv ide nodejs zip DIR="$(realpath "packages/{{.A}}")"
33-
else echo "no zip environemnt"
33+
elif test -e "packages/{{.A}}/composer.json"
34+
then nuv ide php zip DIR="$(realpath "packages/{{.A}}")"
35+
elif test -e "packages/{{.A}}/go.mod"
36+
then nuv ide go zip DIR="$(realpath "packages/{{.A}}")"
37+
else echo "no zip environment"
3438
fi
3539
3640
action:
@@ -44,6 +48,10 @@ tasks:
4448
then nuv ide python action DIR="$(realpath "packages/{{.A}}")"
4549
elif test -e "packages/{{.A}}/index.js"
4650
then nuv ide nodejs action DIR="$(realpath "packages/{{.A}}")"
51+
elif test -e "packages/{{.A}}/index.php"
52+
then nuv ide php action DIR="$(realpath "packages/{{.A}}")"
53+
elif test -e "packages/{{.A}}/main.go"
54+
then nuv ide go action DIR="$(realpath "packages/{{.A}}")"
4755
else die "*** unknow action type"
4856
fi
4957
@@ -58,9 +66,12 @@ tasks:
5866
then nuv ide python clean DIR="$(realpath "packages/{{.A}}")"
5967
elif test -d "packages/{{.A}}/node_modules"
6068
then nuv ide nodejs clean DIR="$(realpath "packages/{{.A}}")"
69+
elif test -d "packages/{{.A}}/vendor"
70+
then nuv ide php clean DIR="$(realpath "packages/{{.A}}")"
71+
elif test -e "packages/{{.A}}/_main.go"
72+
then nuv ide go clean DIR="$(realpath "packages/{{.A}}")"
6173
else echo "nothing to clean"
6274
fi
6375
if test -e "packages/{{.A}}.zip"
6476
then rm "packages/{{.A}}.zip"
65-
fi
66-
77+
fi

0 commit comments

Comments
 (0)