mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-03 06:31:18 +08:00
feat: add cache cleaning functionality and configuration option
This commit is contained in:
31
cmd/run.go
31
cmd/run.go
@@ -3,9 +3,11 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/krau/SaveAny-Bot/bootstrap"
|
"github.com/krau/SaveAny-Bot/bootstrap"
|
||||||
|
"github.com/krau/SaveAny-Bot/config"
|
||||||
"github.com/krau/SaveAny-Bot/core"
|
"github.com/krau/SaveAny-Bot/core"
|
||||||
"github.com/krau/SaveAny-Bot/logger"
|
"github.com/krau/SaveAny-Bot/logger"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -18,5 +20,32 @@ func Run(_ *cobra.Command, _ []string) {
|
|||||||
quit := make(chan os.Signal, 1)
|
quit := make(chan os.Signal, 1)
|
||||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||||
sig := <-quit
|
sig := <-quit
|
||||||
logger.L.Info(sig, ", exit")
|
logger.L.Info(sig, ", exitting...")
|
||||||
|
defer logger.L.Info("Bye!")
|
||||||
|
if config.Cfg.NoCleanCache {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if config.Cfg.Temp.BasePath != "" {
|
||||||
|
for _, path := range []string{"/", ".", "\\", ".."} {
|
||||||
|
if filepath.Clean(config.Cfg.Temp.BasePath) == path {
|
||||||
|
logger.L.Error("Invalid cache dir: ", config.Cfg.Temp.BasePath)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentDir, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
logger.L.Error("Failed to get current dir: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cachePath := filepath.Join(currentDir, config.Cfg.Temp.BasePath)
|
||||||
|
cachePath, err = filepath.Abs(cachePath)
|
||||||
|
if err != nil {
|
||||||
|
logger.L.Error("Failed to get absolute path: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.L.Info("Cleaning cache dir: ", cachePath)
|
||||||
|
if err := os.RemoveAll(cachePath); err != nil {
|
||||||
|
logger.L.Error("Failed to clean cache dir: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Workers int `toml:"workers" mapstructure:"workers"`
|
Workers int `toml:"workers" mapstructure:"workers"`
|
||||||
Retry int `toml:"retry" mapstructure:"retry"`
|
Retry int `toml:"retry" mapstructure:"retry"`
|
||||||
|
NoCleanCache bool `toml:"no_clean_cache" mapstructure:"no_clean_cache"`
|
||||||
|
|
||||||
Temp tempConfig `toml:"temp" mapstructure:"temp"`
|
Temp tempConfig `toml:"temp" mapstructure:"temp"`
|
||||||
Log logConfig `toml:"log" mapstructure:"log"`
|
Log logConfig `toml:"log" mapstructure:"log"`
|
||||||
|
|||||||
Reference in New Issue
Block a user