Skip to content

Commit

Permalink
mod: streamline code
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed Feb 1, 2024
1 parent 1d4063d commit b02b441
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 88 deletions.
10 changes: 2 additions & 8 deletions cmd/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
Expand Down
31 changes: 26 additions & 5 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/pterm/pterm"
Expand Down Expand Up @@ -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...)
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions internal/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package internal

import (
"fmt"
"github.com/version-fox/vfox/internal/util"
"os"
"path/filepath"
)
Expand All @@ -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
Expand All @@ -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,
Expand Down
73 changes: 0 additions & 73 deletions internal/tmp.go

This file was deleted.

0 comments on commit b02b441

Please sign in to comment.