feat: add foreground mode, pause/resume commands, API docs

This commit is contained in:
2026-03-25 00:12:14 +08:00
parent b00f95b7fb
commit 8ce1e6531a
8 changed files with 502 additions and 76 deletions

View File

@@ -43,7 +43,14 @@ func GetLogDir() string {
func LoadConfig() (*Config, error) {
configDir := GetConfigDir()
configPath := filepath.Join(configDir, "config.json")
return loadConfigFile(configPath)
}
func LoadConfigFrom(path string) (*Config, error) {
return loadConfigFile(path)
}
func loadConfigFile(configPath string) (*Config, error) {
data, err := os.ReadFile(configPath)
if err != nil {
if os.IsNotExist(err) {

52
pkg/config/pause.go Normal file
View File

@@ -0,0 +1,52 @@
package config
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// SetPause writes a timestamp into pause_until.txt
func SetPause(until time.Time) error {
dir := GetConfigDir()
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
path := filepath.Join(dir, "pause_until.txt")
ts := strconv.FormatInt(until.Unix(), 10)
return os.WriteFile(path, []byte(ts), 0644)
}
// ClearPause removes the pause_until.txt file
func ClearPause() error {
path := filepath.Join(GetConfigDir(), "pause_until.txt")
return os.Remove(path)
}
// IsPaused checks if the service is currently paused via IPC file
func IsPaused() (bool, int64) {
path := filepath.Join(GetConfigDir(), "pause_until.txt")
data, err := os.ReadFile(path)
if err != nil {
return false, 0
}
tsStr := strings.TrimSpace(string(data))
untilUnix, err := strconv.ParseInt(tsStr, 10, 64)
if err != nil {
// Invalid file, clean it up
os.Remove(path)
return false, 0
}
now := time.Now().Unix()
if now < untilUnix {
return true, untilUnix - now
}
// Expired
os.Remove(path)
return false, 0
}