77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package workspace
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
|
)
|
|
|
|
type Settings struct {
|
|
RootDir string
|
|
DiagnosticsRoot string
|
|
CheckpointsRoot string
|
|
DebugRoot string
|
|
DiagnosticsEnabled bool
|
|
ResumeEnabled bool
|
|
DebugEnabled bool
|
|
}
|
|
|
|
func FromConfig(cfg config.Config) Settings {
|
|
root := cleanPath(cfg.Workspace.Directory)
|
|
settings := Settings{
|
|
RootDir: root,
|
|
DiagnosticsEnabled: cfg.DiagnosticsEnabled(),
|
|
}
|
|
if settings.DiagnosticsEnabled {
|
|
settings.DiagnosticsRoot = cleanPath(cfg.Diagnostics.WorkDir)
|
|
}
|
|
if root == "" {
|
|
return settings
|
|
}
|
|
|
|
settings.CheckpointsRoot = filepath.Join(root, "checkpoints")
|
|
settings.DebugRoot = filepath.Join(root, "debug")
|
|
settings.ResumeEnabled = cfg.Workspace.Resume.Enabled
|
|
settings.DebugEnabled = cfg.Workspace.Debug.Enabled
|
|
return settings
|
|
}
|
|
|
|
func (s Settings) DiagnosticsRunDirectory(runID string) (string, error) {
|
|
if !s.DiagnosticsEnabled || strings.TrimSpace(s.DiagnosticsRoot) == "" {
|
|
return "", nil
|
|
}
|
|
return safeSingleDirectory(s.DiagnosticsRoot, runID, "diagnostics run ID")
|
|
}
|
|
|
|
func (s Settings) CheckpointIdentityDirectory(identity string) (string, error) {
|
|
if !s.ResumeEnabled || strings.TrimSpace(s.CheckpointsRoot) == "" {
|
|
return "", nil
|
|
}
|
|
return SafePath(s.CheckpointsRoot, identity)
|
|
}
|
|
|
|
func (s Settings) DebugRunDirectory(runID string) (string, error) {
|
|
if !s.DebugEnabled || strings.TrimSpace(s.DebugRoot) == "" {
|
|
return "", nil
|
|
}
|
|
return safeSingleDirectory(s.DebugRoot, runID, "debug run ID")
|
|
}
|
|
|
|
func cleanPath(path string) string {
|
|
path = strings.TrimSpace(path)
|
|
if path == "" {
|
|
return ""
|
|
}
|
|
return filepath.Clean(path)
|
|
}
|
|
|
|
func safeSingleDirectory(root string, name string, label string) (string, error) {
|
|
name = strings.TrimSpace(name)
|
|
if strings.Contains(name, "/") || strings.Contains(name, `\`) {
|
|
return "", fmt.Errorf("%s %q must be a single directory name", label, name)
|
|
}
|
|
return SafePath(root, name)
|
|
}
|