mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: init cmd-hook and move terminal-service to services directory
feat: put cmd-hook in container feat: cmd-hook request type, auto restart setup feat: handle ls
This commit is contained in:
1 parent
58b6448c3d
commit
0b08a9a5d9
16 files changed
+343
-21
No files matched your search
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type CommandPayload struct {
|
||||
Command string `json:"command"`
|
||||
Output string `json:"output"`
|
||||
Error string `json:"error"`
|
||||
ExitCode string `json:"exit_code"`
|
||||
PWD string `json:"pwd"`
|
||||
Params CommandParams `json:"params"`
|
||||
}
|
||||
|
||||
func readOrNone(file string) string {
|
||||
data, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func main() {
|
||||
cmdName := os.Getenv("CMD_NAME")
|
||||
if cmdName == "" {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
payload := CommandPayload{
|
||||
Command: cmdName,
|
||||
Output: readOrNone(os.Getenv("CMD_OUTPUT_FILE")),
|
||||
Error: readOrNone(os.Getenv("CMD_ERROR_FILE")),
|
||||
ExitCode: os.Getenv("CMD_EXIT_CODE"),
|
||||
PWD: os.Getenv("PWD"),
|
||||
}
|
||||
|
||||
params, err := handleCommand(cmdName)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error handling command: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
payload.Params = params
|
||||
|
||||
apiEndpoint := os.Getenv("API_ENDPOINT")
|
||||
token := os.Getenv("TOKEN")
|
||||
|
||||
jsonData, err := json.MarshalIndent(payload, "", " ")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error marshaling payload: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(string(jsonData))
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/commands", apiEndpoint), bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating request: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error sending request: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
fmt.Fprintf(os.Stderr, "Failed to send command: %s\nResponse Body: %s\n", resp.Status, string(body))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user