Extract daemon logic from cmd/daemon/ into internal/daemon/ package and create a new unified CLI entry point at cmd/multica/ using cobra. The CLI supports `daemon` as a long-running subcommand plus ctrl subcommands for agent/runtime management, config, status, and version. Server, migrate, and seed binaries remain unchanged. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
46 lines
1 KiB
Go
46 lines
1 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// EnvOrDefault returns the trimmed value of the environment variable key,
|
|
// falling back to fallback if empty.
|
|
func EnvOrDefault(key, fallback string) string {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|
|
|
|
// DurationFromEnv parses a duration from an environment variable,
|
|
// returning fallback if the variable is empty.
|
|
func DurationFromEnv(key string, fallback time.Duration) (time.Duration, error) {
|
|
value := strings.TrimSpace(os.Getenv(key))
|
|
if value == "" {
|
|
return fallback, nil
|
|
}
|
|
d, err := time.ParseDuration(value)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("%s: invalid duration %q: %w", key, value, err)
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// SleepWithContext blocks for the given duration or until the context is cancelled.
|
|
func SleepWithContext(ctx context.Context, d time.Duration) error {
|
|
timer := time.NewTimer(d)
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
return nil
|
|
}
|
|
}
|