package garagesrv import ( "fmt" "io" "os" "strconv" "text/template" "isle/garage" ) // 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 garage.LocalPeer BootstrapPeers []garage.RemotePeer } var garageTomlTpl = template.Must(template.New("").Parse(` metadata_dir = "{{ .MetaPath }}" data_dir = "{{ .DataPath }}" replication_mode = "` + strconv.Itoa(garage.ReplicationFactor) + `" rpc_secret = "{{ .RPCSecret }}" rpc_bind_addr = "{{ .RPCAddr }}" rpc_public_addr = "{{ .RPCAddr }}" bootstrap_peers = [{{- range .BootstrapPeers }} "{{ .RPCPeerAddr }}", {{ 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 }