diff --git a/cmd/commands/env.go b/cmd/commands/env.go index 84c8fe61..b13b5f6c 100644 --- a/cmd/commands/env.go +++ b/cmd/commands/env.go @@ -18,10 +18,8 @@ package commands import ( "fmt" - "github.com/version-fox/vfox/internal" - "os" - "github.com/urfave/cli/v2" + "github.com/version-fox/vfox/internal" "github.com/version-fox/vfox/internal/shell" ) @@ -47,12 +45,8 @@ func envCmd(ctx *cli.Context) error { if ctx.IsSet("cleanup") { manager := internal.NewSdkManager() defer manager.Close() - temp, err := internal.NewTemp(manager.PathMeta.TempPath, os.Getppid()) - if err != nil { - return err - } // Clean up the old temp files, before today. - temp.Remove() + manager.CleanTmp() return nil } else { shellName := ctx.String("shell") diff --git a/internal/manager.go b/internal/manager.go index 2e86b905..5a09560c 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -25,6 +25,7 @@ import ( "net/url" "os" "path/filepath" + "strconv" "strings" "github.com/pterm/pterm" @@ -345,6 +346,30 @@ func (m *Manager) Available() ([]*Category, error) { } } +func (m *Manager) CleanTmp() { + dir, err := os.ReadDir(m.PathMeta.TempPath) + if err == nil { + _ = os.RemoveAll(m.PathMeta.CurTmpPath) + for _, file := range dir { + if !file.IsDir() { + continue + } + names := strings.SplitN(file.Name(), "-", 2) + if len(names) != 2 { + continue + } + timestamp := names[0] + i, err := strconv.ParseInt(timestamp, 10, 64) + if err != nil { + continue + } + if util.IsBeforeToday(i) { + _ = os.Remove(filepath.Join(m.PathMeta.TempPath, file.Name())) + } + } + } +} + func NewSdkManagerWithSource(sources ...RecordSource) *Manager { if env.IsHookEnv() { return newSdkManagerWithSource(sources...) @@ -370,11 +395,7 @@ func newSdkManagerWithSource(sources ...RecordSource) *Manager { } paths = append(paths, curDir) case SessionRecordSource: - temp, err := NewTemp(meta.TempPath, os.Getppid()) - if err != nil { - panic("Init temp error") - } - paths = append(paths, temp.CurProcessPath) + paths = append(paths, meta.CurTmpPath) } } var record env.Record diff --git a/internal/path.go b/internal/path.go index 1b6e802d..ad937e23 100644 --- a/internal/path.go +++ b/internal/path.go @@ -18,6 +18,7 @@ package internal import ( "fmt" + "github.com/version-fox/vfox/internal/util" "os" "path/filepath" ) @@ -31,7 +32,9 @@ const ( ) type PathMeta struct { - TempPath string + TempPath string + // Temporary directory for the current process + CurTmpPath string ConfigPath string SdkCachePath string PluginPath string @@ -52,10 +55,21 @@ func newPathMeta() (*PathMeta, error) { _ = os.MkdirAll(tmpPath, 0755) exePath, err := os.Executable() if err != nil { - panic("Get executable path error") + return nil, err + } + pid := os.Getppid() + timestamp := util.GetBeginOfToday() + name := fmt.Sprintf("%d-%d", timestamp, pid) + curTmpPath := filepath.Join(tmpPath, name) + if !util.FileExists(curTmpPath) { + err = os.Mkdir(curTmpPath, 0755) + if err != nil { + return nil, fmt.Errorf("create temp dir failed: %w", err) + } } return &PathMeta{ TempPath: tmpPath, + CurTmpPath: curTmpPath, ConfigPath: configPath, SdkCachePath: sdkCachePath, PluginPath: pluginPath, diff --git a/internal/tmp.go b/internal/tmp.go deleted file mode 100644 index c4cfb5bb..00000000 --- a/internal/tmp.go +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2024 Han Li and contributors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package internal - -import ( - "fmt" - "os" - "path/filepath" - "strconv" - "strings" - - "github.com/version-fox/vfox/internal/util" -) - -type Temp struct { - dirPath string - CurProcessPath string -} - -func (t *Temp) Remove() { - dir, err := os.ReadDir(t.dirPath) - if err == nil { - _ = os.RemoveAll(t.CurProcessPath) - for _, file := range dir { - if !file.IsDir() { - continue - } - names := strings.SplitN(file.Name(), "-", 2) - if len(names) != 2 { - continue - } - timestamp := names[0] - i, err := strconv.ParseInt(timestamp, 10, 64) - if err != nil { - continue - } - if util.IsBeforeToday(i) { - _ = os.Remove(filepath.Join(t.dirPath, file.Name())) - } - } - } -} - -func NewTemp(dirPath string, pid int) (*Temp, error) { - timestamp := util.GetBeginOfToday() - name := fmt.Sprintf("%d-%d", timestamp, pid) - path := filepath.Join(dirPath, name) - if !util.FileExists(path) { - err := os.Mkdir(path, 0755) - if err != nil { - return nil, fmt.Errorf("create temp dir failed: %w", err) - } - } - t := &Temp{ - dirPath: dirPath, - CurProcessPath: path, - } - return t, nil -}