init
This commit is contained in:
82
out.go
Normal file
82
out.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const FILEPERM = 0644
|
||||
const INIPREFIX = `[SETTINGS]
|
||||
Sensitivity=6
|
||||
Splash1len=2
|
||||
Splash2len=2
|
||||
`
|
||||
|
||||
func CopyOut(roomFolders []RoomFolder, outpath string) error {
|
||||
inistr := INIPREFIX
|
||||
|
||||
for _, roomFolder := range roomFolders {
|
||||
err := copyFolder(roomFolder, outpath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error copying folder %s: %+v", roomFolder.path, err)
|
||||
}
|
||||
inistr += roomFolder.cfg.ToIni()
|
||||
}
|
||||
|
||||
outini := filepath.Join(outpath, "CF.ini")
|
||||
fmt.Printf("Writing %s\n", outini)
|
||||
return os.WriteFile(outini, []byte(inistr), FILEPERM)
|
||||
}
|
||||
|
||||
func copyFolder(roomFolder RoomFolder, outpath string) error {
|
||||
nightdir := fmt.Sprintf("NIGHT_%d", roomFolder.cfg.night)
|
||||
roomdir := fmt.Sprintf("ROOM_%d", roomFolder.cfg.room)
|
||||
|
||||
roomoutpath := filepath.Join(outpath, "DATA", nightdir, roomdir)
|
||||
|
||||
os.MkdirAll(roomoutpath, FILEPERM)
|
||||
fmt.Printf(`Copying:
|
||||
%s
|
||||
to %s
|
||||
`, roomFolder.path, roomoutpath)
|
||||
|
||||
entries, err := os.ReadDir(roomFolder.path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
// TODO whitelist files?
|
||||
entrypath := path.Join(roomFolder.path, entry.Name())
|
||||
|
||||
if entry.IsDir() {
|
||||
fmt.Printf("WARN: unexpected folder found in room folder: %s\n", entrypath)
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.ToLower(entry.Name()) == "room.ini" {
|
||||
continue
|
||||
}
|
||||
|
||||
topath := path.Join(roomoutpath, entry.Name())
|
||||
|
||||
err := copyFile(entrypath, topath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyFile(from, to string) error {
|
||||
fromdata, err := os.ReadFile(from)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(to, fromdata, FILEPERM)
|
||||
}
|
||||
Reference in New Issue
Block a user