diff --git a/internal/keychain/auth_log.go b/internal/keychain/auth_log.go index 64558bd93..079f8cd90 100644 --- a/internal/keychain/auth_log.go +++ b/internal/keychain/auth_log.go @@ -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())) } } } diff --git a/internal/vfs/default.go b/internal/vfs/default.go index 7a9603d76..5b0148c21 100644 --- a/internal/vfs/default.go +++ b/internal/vfs/default.go @@ -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) } diff --git a/internal/vfs/fs.go b/internal/vfs/fs.go index be9aaa26f..6ac5acd9b 100644 --- a/internal/vfs/fs.go +++ b/internal/vfs/fs.go @@ -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 } diff --git a/internal/vfs/osfs.go b/internal/vfs/osfs.go index ac0e4ef20..09a2d6737 100644 --- a/internal/vfs/osfs.go +++ b/internal/vfs/osfs.go @@ -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) } diff --git a/shortcuts/drive/drive_import.go b/shortcuts/drive/drive_import.go index 528e075c3..f152d68cd 100644 --- a/shortcuts/drive/drive_import.go +++ b/shortcuts/drive/drive_import.go @@ -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) } diff --git a/shortcuts/drive/drive_import_common.go b/shortcuts/drive/drive_import_common.go index 370da55b9..f3b61c478 100644 --- a/shortcuts/drive/drive_import_common.go +++ b/shortcuts/drive/drive_import_common.go @@ -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) } diff --git a/shortcuts/minutes/minutes_download.go b/shortcuts/minutes/minutes_download.go index 9a8c55453..1c8a423f1 100644 --- a/shortcuts/minutes/minutes_download.go +++ b/shortcuts/minutes/minutes_download.go @@ -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) }