50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
)
|
|
|
|
type InstanceState string
|
|
|
|
const (
|
|
StateStopped InstanceState = "STOPPED"
|
|
StateRunning InstanceState = "RUNNING"
|
|
)
|
|
|
|
type ManagedInstance struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
Port int `json:"port"`
|
|
Status InstanceState `json:"status"`
|
|
|
|
Cmd *exec.Cmd `json:"-"`
|
|
Stdin interface{} `json:"-"`
|
|
}
|
|
|
|
func CreateNewInstance(name string, version string, options VsServerConfigOptions, cfg *AppConfig) error {
|
|
instanceDir := filepath.Join(cfg.Storage.InstancesDir, name)
|
|
|
|
dirs := []string{
|
|
instanceDir,
|
|
filepath.Join(instanceDir, "Saves"),
|
|
filepath.Join(instanceDir, "Mods"),
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
return fmt.Errorf("Failed creating core instance directory layout: %w", err)
|
|
}
|
|
}
|
|
|
|
instanceConfigPath := filepath.Join(instanceDir, "serverconfig.json")
|
|
if err := PrepareInstanceConfig(version, instanceConfigPath, options, cfg); err != nil {
|
|
return fmt.Errorf("Failed provisioning server baseline configuration: %w", err)
|
|
}
|
|
|
|
fmt.Printf("Instance '%s' (v%s) successfully initialized\n", name, version)
|
|
return nil
|
|
}
|