Initial commit of datagraph_launcher

This commit is contained in:
Magnus Åhall 2025-08-20 10:18:35 +02:00
commit bdcc7ff935
3 changed files with 120 additions and 0 deletions

5
launcher/go.mod Normal file
View file

@ -0,0 +1,5 @@
module datagraph_launcher
go 1.24.2
require github.com/goccy/go-yaml v1.18.0

2
launcher/go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=

113
launcher/main.go Normal file
View file

@ -0,0 +1,113 @@
package main
import (
// External
"github.com/goccy/go-yaml"
// Standard
"encoding/json"
"flag"
"fmt"
"io"
"os"
"path"
)
const VERSION = "v1"
type DatagraphNode struct {
ID int
Name string
Children []*DatagraphNode
TypeName string
Data struct {
Exec string
Dynamic string
Inplace bool
}
}
type Entry struct {
Name string
Exec string `yaml:",omitempty"`
Inplace bool `yaml:",omitempty"`
Dynamic string `yaml:",omitempty"`
Commands []*Entry `yaml:",omitempty"`
}
func (node DatagraphNode) toEntry() (e Entry) {
e.Name = node.Name
e.Exec = node.Data.Exec
e.Dynamic = node.Data.Dynamic
e.Inplace = node.Data.Inplace
for _, childNode := range node.Children {
childEntry := childNode.toEntry()
e.Commands = append(e.Commands, &childEntry)
}
return
}
func init() {
var flagVersion bool
flag.BoolVar(&flagVersion, "version", false, "Print version and exit")
flag.Parse()
if flagVersion {
fmt.Println(VERSION)
os.Exit(0)
}
}
func main() {
// Script could be run for different users and have different places where config is supposed to go.
dir := os.Getenv("DATAGRAPH_DIR")
if dir == "" {
fmt.Fprintf(os.Stderr, "Set env variable DATAGRAPH_DIR\n")
os.Exit(1)
}
// Make it easier for admins by creating directories automatically.
err := os.MkdirAll(dir, 0700)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// JSON data from datagraph is read into a data structure
// for further processing into the launcher
datagraphData, _ := io.ReadAll(os.Stdin)
var node DatagraphNode
err = json.Unmarshal(datagraphData, &node)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// Each top node is a separate file.
for _, topNode := range node.Children {
y, err := yaml.Marshal(topNode.toEntry())
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
f, err := os.OpenFile(
path.Join(dir, topNode.Name+".yaml"),
os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
0600,
)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
defer f.Close()
_, err = f.Write(y)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
}