goherence/examples/external-module-hello/main.go
2026-09-11 10:17:25 +03:00

51 lines
1.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Пример внешнего модуля для goherence — демонстрирует контракт
// stdin/stdout JSON, тот же, что использует и сам goherence для builtin.
// Собери его через build.sh и положи результат в modules.d/hello/
// рядом с manifest.json — goherence подхватит его как модуль `hello`.
package main
import (
"encoding/json"
"fmt"
"os"
)
// Input и Result дублируют контракт из internal/module/module.go —
// внешний модуль не импортирует goherence как зависимость (он может
// быть написан вообще не на Go), поэтому просто повторяет форму JSON.
type Input struct {
SchemaVersion int `json:"schema_version"`
Args map[string]interface{} `json:"args"`
Host string `json:"host"`
Become bool `json:"become"`
CheckMode bool `json:"check_mode"`
}
type Result struct {
SchemaVersion int `json:"schema_version"`
Changed bool `json:"changed"`
Failed bool `json:"failed"`
Msg string `json:"msg"`
Diff interface{} `json:"diff,omitempty"`
}
func main() {
var in Input
if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil {
fmt.Fprintln(os.Stderr, "hello: невалидный input JSON:", err)
os.Exit(1)
}
name, _ := in.Args["name"].(string)
if name == "" {
name = "world"
}
result := Result{
SchemaVersion: 1,
Changed: false, // модуль ничего не меняет на хосте — просто демонстрационный
Msg: fmt.Sprintf("hello, %s (host=%s, check_mode=%v)", name, in.Host, in.CheckMode),
}
json.NewEncoder(os.Stdout).Encode(result)
}