mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-24 09:23:23 +00:00
create config and metadata if none exists
This commit is contained in:
parent
f4dd7d2f54
commit
cbd4f62466
5
main.go
5
main.go
@ -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
31
relay/utils/copyFile.go
Normal 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
|
||||
}
|
15
relay/utils/ensureFileExists.go
Normal file
15
relay/utils/ensureFileExists.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user