1 Star 2 Fork 1

孤爺仔/BaiduPCS-Go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ls_search.go 3.98 KB
一键复制 编辑 原始数据 按行查看 历史
package pcscommand
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/baidupcs"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"github.com/olekukonko/tablewriter"
"os"
"strconv"
)
type (
// LsOptions 列目录可选项
LsOptions struct {
Total bool
}
// SearchOptions 搜索可选项
SearchOptions struct {
Total bool
Recurse bool
}
)
const (
opLs int = iota
opSearch
)
// RunLs 执行列目录
func RunLs(path string, lsOptions *LsOptions, orderOptions *baidupcs.OrderOptions) {
path, err := getAbsPath(path)
if err != nil {
fmt.Println(err)
return
}
files, err := GetBaiduPCS().FilesDirectoriesList(path, orderOptions)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("\n当前目录: %s\n----\n", path)
if lsOptions == nil {
lsOptions = &LsOptions{}
}
renderTable(opLs, lsOptions.Total, path, files)
return
}
// RunSearch 执行搜索
func RunSearch(targetPath, keyword string, opt *SearchOptions) {
targetPath, err := getAbsPath(targetPath)
if err != nil {
fmt.Println(err)
return
}
if opt == nil {
opt = &SearchOptions{}
}
files, err := GetBaiduPCS().Search(targetPath, keyword, opt.Recurse)
if err != nil {
fmt.Println(err)
return
}
renderTable(opSearch, opt.Total, targetPath, files)
return
}
func renderTable(op int, isTotal bool, path string, files baidupcs.FileDirectoryList) {
tb := pcstable.NewTable(os.Stdout)
var (
fN, dN int64
showPath string
)
switch op {
case opLs:
showPath = "文件(目录)"
case opSearch:
showPath = "路径"
}
if isTotal {
tb.SetHeader([]string{"#", "fs_id", "app_id", "文件大小", "创建日期", "修改日期", "md5(截图请打码)", showPath})
tb.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
for k, file := range files {
if file.Isdir {
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), "-", pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), file.MD5, file.Filename + "/"})
continue
}
var md5 string
if len(file.BlockList) > 1 {
md5 = "(可能不正确)" + file.MD5
} else {
md5 = file.MD5
}
switch op {
case opLs:
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Filename})
case opSearch:
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(file.FsID, 10), strconv.FormatInt(file.AppID, 10), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Ctime), pcstime.FormatTime(file.Mtime), md5, file.Path})
}
}
fN, dN = files.Count()
tb.Append([]string{"", "", "总: " + converter.ConvertFileSize(files.TotalSize(), 2), "", "", "", fmt.Sprintf("文件总数: %d, 目录总数: %d", fN, dN)})
} else {
tb.SetHeader([]string{"#", "文件大小", "修改日期", showPath})
tb.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT, tablewriter.ALIGN_LEFT})
for k, file := range files {
if file.Isdir {
tb.Append([]string{strconv.Itoa(k), "-", pcstime.FormatTime(file.Mtime), file.Filename + "/"})
continue
}
switch op {
case opLs:
tb.Append([]string{strconv.Itoa(k), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Mtime), file.Filename})
case opSearch:
tb.Append([]string{strconv.Itoa(k), converter.ConvertFileSize(file.Size, 2), pcstime.FormatTime(file.Mtime), file.Path})
}
}
fN, dN = files.Count()
tb.Append([]string{"", "总: " + converter.ConvertFileSize(files.TotalSize(), 2), "", fmt.Sprintf("文件总数: %d, 目录总数: %d", fN, dN)})
}
tb.Render()
if fN+dN >= 50 {
fmt.Printf("\n当前目录: %s\n", path)
}
fmt.Printf("----\n")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guyezi/BaiduPCS-Go.git
git@gitee.com:guyezi/BaiduPCS-Go.git
guyezi
BaiduPCS-Go
BaiduPCS-Go
v3.5.6

搜索帮助