isle/go/garage/garagesrv/tpl.go

76 lines
1.6 KiB
Go
Raw Normal View History

package garagesrv
import (
2025-01-01 12:07:35 +00:00
"bytes"
"context"
2025-01-01 12:07:35 +00:00
"fmt"
"io"
2025-01-01 12:07:35 +00:00
"os"
"strconv"
"text/template"
"isle/garage"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
)
// GarageTomlData describes all fields needed for rendering a garage.toml
// file via this package's template.
type GarageTomlData struct {
MetaPath string
DataPath string
2022-10-16 19:22:58 +00:00
RPCSecret string
AdminToken string
DBEngine DBEngine
garage.LocalNode
BootstrapPeers []garage.RemoteNode
}
var garageTomlTpl = template.Must(template.New("").Parse(`
metadata_dir = "{{ .MetaPath }}"
data_dir = "{{ .DataPath }}"
db_engine = "{{ .DBEngine }}"
replication_mode = "` + strconv.Itoa(garage.ReplicationFactor) + `"
rpc_secret = "{{ .RPCSecret }}"
rpc_bind_addr = "{{ .RPCAddr }}"
rpc_public_addr = "{{ .RPCAddr }}"
bootstrap_peers = [{{- range .BootstrapPeers }}
"{{ .RPCNodeAddr }}",
{{ end -}}]
[s3_api]
api_bind_addr = "{{ .S3APIAddr }}"
s3_region = "garage"
2022-10-16 19:22:58 +00:00
[admin]
api_bind_addr = "{{ .AdminAddr }}"
admin_token = "{{ .AdminToken }}"
`))
// RenderGarageToml renders a garage.toml using the given data into the writer.
func RenderGarageToml(into io.Writer, data GarageTomlData) error {
return garageTomlTpl.Execute(into, data)
}
// WriteGarageTomlFile renders a garage.toml using the given data to a new file
2025-01-01 12:07:35 +00:00
// at the given path.
func WriteGarageTomlFile(
2025-01-01 12:07:35 +00:00
ctx context.Context, logger *mlog.Logger, path string, data GarageTomlData,
) error {
buf := new(bytes.Buffer)
if err := garageTomlTpl.Execute(buf, data); err != nil {
return fmt.Errorf("executing template: %w", err)
}
if err := os.WriteFile(path, buf.Bytes(), 0600); err != nil {
return fmt.Errorf("writing file: %w", err)
}
return nil
}