diff --git a/cmd/edge-api/main.go b/cmd/edge-api/main.go index 534ef7ea..711a26f7 100644 --- a/cmd/edge-api/main.go +++ b/cmd/edge-api/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "github.com/TeaOSLab/EdgeAPI/internal/apps" + "github.com/TeaOSLab/EdgeAPI/internal/configs" teaconst "github.com/TeaOSLab/EdgeAPI/internal/const" "github.com/TeaOSLab/EdgeAPI/internal/nodes" "github.com/TeaOSLab/EdgeAPI/internal/setup" @@ -71,6 +72,14 @@ func main() { } fmt.Println("done") }) + app.On("reset", func() { + err := configs.ResetAPIConfig() + if err != nil { + fmt.Println("[ERROR]reset failed: " + err.Error()) + return + } + fmt.Println("done") + }) app.On("goman", func() { var sock = gosock.NewTmpSock(teaconst.ProcessName) reply, err := sock.Send(&gosock.Command{Code: "goman"}) diff --git a/internal/configs/api_config.go b/internal/configs/api_config.go index 34805b17..0712ecc2 100644 --- a/internal/configs/api_config.go +++ b/internal/configs/api_config.go @@ -132,3 +132,47 @@ func (this *APIConfig) WriteFile(path string) error { return os.WriteFile(path, data, 0666) } + +// ResetAPIConfig 重置配置 +func ResetAPIConfig() error { + for _, filename := range []string{"api.yaml", "db.yaml"} { + // 重置 configs/api.yaml + { + var configFile = Tea.ConfigFile(filename) + stat, err := os.Stat(configFile) + if err == nil && !stat.IsDir() { + err = os.Remove(configFile) + if err != nil { + return err + } + } + } + + // 重置 ~/.edge-api/api.yaml + homeDir, homeErr := os.UserHomeDir() + if homeErr == nil { + var configFile = homeDir + "/." + teaconst.ProcessName + "/" + filename + stat, err := os.Stat(configFile) + if err == nil && !stat.IsDir() { + err = os.Remove(configFile) + if err != nil { + return err + } + } + } + + // 重置 /etc/edge-api/api.yaml + { + var configFile = "/etc/" + teaconst.ProcessName + "/" + filename + stat, err := os.Stat(configFile) + if err == nil && !stat.IsDir() { + err = os.Remove(configFile) + if err != nil { + return err + } + } + } + } + + return nil +}