Add -root flag to serviceman

This commit is contained in:
2025-10-28 10:33:16 +01:00
parent 8966aa484c
commit c4e0ec1add

View File

@@ -49,6 +49,7 @@ var (
force = flag.Bool("force", false, "Force apply changes")
delete = flag.Bool("d", false, "Delete files not in TOML config")
tomlFile = flag.String("config", "services.toml", "Path to TOML configuration file")
rootDir = flag.String("root", ".", "Root directory to run in (services.toml read from here; .caddy files written here)")
)
func main() {
@@ -135,11 +136,15 @@ func validateConfig(config *Config) error {
}
func syncConfigs(config *Config) error {
// Get current directory
currentDir, err := os.Getwd()
// Determine directory for .caddy files
currentDir := *rootDir
if currentDir == "" || currentDir == "." {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
currentDir = wd
}
logger := cylogger.Default
// Get existing .caddy files
@@ -180,7 +185,7 @@ func syncConfigs(config *Config) error {
generateAndLogDiff(fileLogger, caddyFilename, "", expectedContent)
if !*dryRun {
if err := writeFile(caddyFilename, expectedContent); err != nil {
if err := writeFile(filepath.Join(currentDir, caddyFilename), expectedContent); err != nil {
return fmt.Errorf("failed to create file %s: %w", caddyFilename, err)
}
}
@@ -193,7 +198,7 @@ func syncConfigs(config *Config) error {
generateAndLogDiff(fileLogger, caddyFilename, existingContent, expectedContent)
if !*dryRun {
if err := writeFile(caddyFilename, expectedContent); err != nil {
if err := writeFile(filepath.Join(currentDir, caddyFilename), expectedContent); err != nil {
return fmt.Errorf("failed to update file %s: %w", caddyFilename, err)
}
}
@@ -213,7 +218,7 @@ func syncConfigs(config *Config) error {
if *delete {
orphanLogger.Info("Deleting orphaned file")
if !*dryRun {
if err := os.Remove(filename); err != nil {
if err := os.Remove(filepath.Join(currentDir, filename)); err != nil {
return fmt.Errorf("failed to delete file %s: %w", filename, err)
}
}