isle/go/garage/tpl.go

76 lines
1.5 KiB
Go

package garage
import (
"fmt"
"io"
"os"
"text/template"
)
// GarageTomlData describes all fields needed for rendering a garage.toml
// file via this package's template.
type GarageTomlData struct {
MetaPath string
DataPath string
RPCSecret string
AdminToken string
RPCAddr string
S3APIAddr string
AdminAddr string
BootstrapPeers []string
}
var garageTomlTpl = template.Must(template.New("").Parse(`
metadata_dir = "{{ .MetaPath }}"
data_dir = "{{ .DataPath }}"
replication_mode = "3"
rpc_secret = "{{ .RPCSecret }}"
rpc_bind_addr = "{{ .RPCAddr }}"
rpc_public_addr = "{{ .RPCAddr }}"
bootstrap_peers = [{{- range .BootstrapPeers }}
"{{ . }}",
{{ end -}}]
[s3_api]
api_bind_addr = "{{ .S3APIAddr }}"
s3_region = "garage"
[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
// at the given path.
func WriteGarageTomlFile(path string, data GarageTomlData) error {
file, err := os.OpenFile(
path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0640,
)
if err != nil {
return fmt.Errorf("creating file: %w", err)
}
defer file.Close()
if err := garageTomlTpl.Execute(file, data); err != nil {
return fmt.Errorf("rendering template to file: %w", err)
}
return nil
}