refactor: migrate remaining os calls to internal/vfs

Replace direct os.Stat/Open/MkdirAll/OpenFile/Remove/ReadDir/UserHomeDir
with vfs equivalents in shortcuts/minutes, shortcuts/drive, and
internal/keychain. Add ReadDir to the vfs interface and OsFs implementation.

Change-Id: I8f97e5fb3e1731b4684d276644fcb10fae823067
This commit is contained in:
liushiyao
2026-04-03 20:22:23 +08:00
parent d6251805c1
commit 2dec7e24c4
7 changed files with 22 additions and 14 deletions

View File

@@ -8,6 +8,8 @@ import (
"strings"
"sync"
"time"
"github.com/larksuite/cli/internal/vfs"
)
var (
@@ -23,7 +25,7 @@ func authLogDir() string {
return filepath.Join(dir, "logs")
}
home, err := os.UserHomeDir()
home, err := vfs.UserHomeDir()
if err != nil || home == "" {
fmt.Fprintf(os.Stderr, "warning: unable to determine home directory: %v\n", err)
}
@@ -39,13 +41,13 @@ func initAuthLogger() {
dir := authLogDir()
now := authResponseLogNow()
if err := os.MkdirAll(dir, 0700); err != nil {
if err := vfs.MkdirAll(dir, 0700); err != nil {
return
}
logName := fmt.Sprintf("auth-%s.log", now.Format("2006-01-02"))
logPath := filepath.Join(dir, logName)
if f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600); err == nil {
if f, err := vfs.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600); err == nil {
authResponseLogger = log.New(f, "", 0)
cleanupOldLogs(dir, now)
}
@@ -131,7 +133,7 @@ func cleanupOldLogs(dir string, now time.Time) {
}
}()
entries, err := os.ReadDir(dir)
entries, err := vfs.ReadDir(dir)
if err != nil {
return
}
@@ -153,7 +155,7 @@ func cleanupOldLogs(dir string, now time.Time) {
logDate = time.Date(logDate.Year(), logDate.Month(), logDate.Day(), 0, 0, 0, 0, now.Location())
if logDate.Before(cutoff) {
_ = os.Remove(filepath.Join(dir, entry.Name()))
_ = vfs.Remove(filepath.Join(dir, entry.Name()))
}
}
}

View File

@@ -25,5 +25,6 @@ func OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
}
func CreateTemp(dir, pattern string) (*os.File, error) { return DefaultFS.CreateTemp(dir, pattern) }
func MkdirAll(path string, perm fs.FileMode) error { return DefaultFS.MkdirAll(path, perm) }
func ReadDir(name string) ([]os.DirEntry, error) { return DefaultFS.ReadDir(name) }
func Remove(name string) error { return DefaultFS.Remove(name) }
func Rename(oldpath, newpath string) error { return DefaultFS.Rename(oldpath, newpath) }

View File

@@ -23,6 +23,7 @@ type FS interface {
// Directory/File management
MkdirAll(path string, perm fs.FileMode) error
ReadDir(name string) ([]os.DirEntry, error)
Remove(name string) error
Rename(oldpath, newpath string) error
}

View File

@@ -27,5 +27,6 @@ func (OsFs) CreateTemp(dir, pattern string) (*os.File, error) { return os.Create
// Directory/File management
func (OsFs) MkdirAll(path string, perm fs.FileMode) error { return os.MkdirAll(path, perm) }
func (OsFs) ReadDir(name string) ([]os.DirEntry, error) { return os.ReadDir(name) }
func (OsFs) Remove(name string) error { return os.Remove(name) }
func (OsFs) Rename(oldpath, newpath string) error { return os.Rename(oldpath, newpath) }

View File

@@ -6,8 +6,9 @@ package drive
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/larksuite/cli/internal/vfs"
"strings"
"github.com/larksuite/cli/internal/output"
@@ -147,7 +148,7 @@ func preflightDriveImportFile(spec *driveImportSpec) (int64, error) {
}
spec.FilePath = safeFilePath
info, err := os.Stat(spec.FilePath)
info, err := vfs.Stat(spec.FilePath)
if err != nil {
return 0, output.ErrValidation("cannot read file: %s", err)
}

View File

@@ -11,11 +11,12 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/larksuite/cli/internal/vfs"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/cli/internal/output"
@@ -96,7 +97,7 @@ func (s driveImportSpec) CreateTaskBody(fileToken string) map[string]interface{}
// uploadMediaForImport uploads the source file to the temporary import media
// endpoint and returns the file token consumed by import_tasks.
func uploadMediaForImport(ctx context.Context, runtime *common.RuntimeContext, filePath, fileName, docType string) (string, error) {
importInfo, err := os.Stat(filePath)
importInfo, err := vfs.Stat(filePath)
if err != nil {
return "", output.ErrValidation("cannot read file: %s", err)
}
@@ -125,7 +126,7 @@ func uploadMediaForImport(ctx context.Context, runtime *common.RuntimeContext, f
}
func uploadMediaForImportAll(runtime *common.RuntimeContext, filePath, fileName string, fileSize int, extra string) (string, error) {
f, err := os.Open(filePath)
f, err := vfs.Open(filePath)
if err != nil {
return "", output.ErrValidation("cannot read file: %s", err)
}
@@ -164,7 +165,7 @@ func uploadMediaForImportMultipart(runtime *common.RuntimeContext, filePath, fil
totalBlocks := session.BlockNum
fmt.Fprintf(runtime.IO().ErrOut, "Multipart upload initialized: %d chunks x %s\n", totalBlocks, common.FormatSize(int64(session.BlockSize)))
f, err := os.Open(filePath)
f, err := vfs.Open(filePath)
if err != nil {
return "", output.ErrValidation("cannot read file: %s", err)
}

View File

@@ -9,12 +9,13 @@ import (
"io"
"mime"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/larksuite/cli/internal/vfs"
"github.com/larksuite/cli/internal/output"
"github.com/larksuite/cli/internal/validate"
"github.com/larksuite/cli/shortcuts/common"
@@ -78,7 +79,7 @@ var MinutesDownload = common.Shortcut{
// Batch mode: --output must be a directory, not an existing file.
if !single && outputPath != "" {
if fi, err := os.Stat(outputPath); err == nil && !fi.IsDir() {
if fi, err := vfs.Stat(outputPath); err == nil && !fi.IsDir() {
return output.ErrValidation("--output %q is a file; batch mode expects a directory path", outputPath)
}
}
@@ -281,7 +282,7 @@ func downloadMediaFile(ctx context.Context, client *http.Client, downloadURL, mi
if err := common.EnsureWritableFile(safePath, opts.overwrite); err != nil {
return nil, err
}
if err := os.MkdirAll(filepath.Dir(safePath), 0700); err != nil {
if err := vfs.MkdirAll(filepath.Dir(safePath), 0700); err != nil {
return nil, output.Errorf(output.ExitInternal, "api_error", "cannot create parent directory: %s", err)
}