This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (91 loc) · 2.37 KB
/
main.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
96
97
98
99
100
101
102
103
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
// Verify structure for JSON output
type Verify struct {
Alias string `json:"alias"`
Description string `json:"description"`
IsCGI bool `json:"is_cgi"`
}
// CGIExtensionRequest
type CGIExtensionRequest struct {
Arguments []string `json:"arguments"`
TopicId string `json:"topicid"`
}
func main() {
// Define and parse command-line flags
extVerify := flag.Bool("ext_verify", false, "")
flag.Parse()
// Create the Verify struct
verify := Verify{
Alias: "cgi-allora-infer",
Description: "allora cgi extension for blockless runtime",
IsCGI: true,
}
// Check if the --ext_verify flag is set
if *extVerify {
jsonData, err := json.Marshal(verify)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
fmt.Println(string(jsonData))
os.Exit(0)
}
// Read and parse JSON from stdin
scanner := bufio.NewScanner(os.Stdin)
var request CGIExtensionRequest
var inputProcessed bool
// Read the first line for the length of the data
if scanner.Scan() {
lengthStr := strings.TrimSpace(scanner.Text())
if lengthStr != "" {
length, err := strconv.Atoi(lengthStr)
if err != nil {
fmt.Println("Invalid length value:", err)
} else if scanner.Scan() {
jsonData := scanner.Text()
if len(jsonData) == length {
if err := json.Unmarshal([]byte(jsonData), &request); err != nil {
fmt.Println("Error parsing JSON:", err)
} else {
inputProcessed = true
}
} else {
fmt.Println("Data length mismatch")
}
}
}
}
if !inputProcessed {
fmt.Println("No valid input provided, continuing with the main loop.")
}
scriptPath := "/tmp/runtime/extensions/main.py"
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
scriptPath = "/app/main.py"
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
scriptPath = "/app/runtime/extensions/main.py"
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
scriptPath = "./main.py"
}
}
}
// Execute the Python script with arguments
cmdArgs := append([]string{scriptPath}, request.TopicId)
cmdArgs = append(cmdArgs, request.Arguments...)
cmd := exec.Command("python3", cmdArgs...)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error running script:", err)
os.Exit(1)
}
fmt.Printf("%s", stdoutStderr)
}