create config and metadata if none exists

This commit is contained in:
0ceanSlim 2024-07-31 12:27:56 -04:00
parent f4dd7d2f54
commit cbd4f62466
5 changed files with 51 additions and 0 deletions

View File

@ -6,7 +6,9 @@ import (
configTypes "grain/config/types"
"grain/relay"
"grain/relay/db"
"grain/relay/utils"
"grain/web"
"log"
"net/http"
@ -14,6 +16,9 @@ import (
)
func main() {
utils.EnsureFileExists("config.yml", "config/config.example.yml")
utils.EnsureFileExists("relay_metadata.json", "web/relay_metadata.example.json")
cfg, err := config.LoadConfig("config.yml")
if err != nil {
log.Fatal("Error loading config: ", err)

31
relay/utils/copyFile.go Normal file
View File

@ -0,0 +1,31 @@
package utils
import (
"io"
"os"
"path/filepath"
)
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
// Create the directory for the destination file if it doesn't exist
destDir := filepath.Dir(dst)
err = os.MkdirAll(destDir, os.ModePerm)
if err != nil {
return err
}
destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
return err
}

View File

@ -0,0 +1,15 @@
package utils
import (
"log"
"os"
)
func EnsureFileExists(filePath, examplePath string) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
err = copyFile(examplePath, filePath)
if err != nil {
log.Fatalf("Failed to copy %s to %s: %v", examplePath, filePath, err)
}
}
}