forked from riba2534/feishu-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_file.go
More file actions
97 lines (78 loc) · 2.7 KB
/
export_file.go
File metadata and controls
97 lines (78 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cmd
import (
"fmt"
"github.com/riba2534/feishu-cli/internal/client"
"github.com/riba2534/feishu-cli/internal/config"
"github.com/spf13/cobra"
)
var exportFileCmd = &cobra.Command{
Use: "export-file <doc_token>",
Short: "导出文档为文件",
Long: `将飞书云文档导出为指定格式的文件(PDF、DOCX、XLSX 等)。
这是一个异步操作:创建导出任务 → 轮询任务状态 → 下载导出文件。
参数:
doc_token 文档的 Token
选项:
--type 导出格式(pdf/docx/xlsx,必填)
--doc-type 文档类型(默认 docx)
-o, --output 输出文件路径
支持的导出格式:
pdf PDF 格式
docx Word 格式
xlsx Excel 格式
文档类型:
doc 旧版文档
docx 新版文档
sheet 电子表格
bitable 多维表格
示例:
# 导出文档为 PDF
feishu-cli doc export-file doccnXXX --type pdf -o output.pdf
# 导出电子表格为 Excel
feishu-cli doc export-file shtcnXXX --type xlsx --doc-type sheet -o report.xlsx`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := config.Validate(); err != nil {
return err
}
docToken := args[0]
fileType, _ := cmd.Flags().GetString("type")
docType, _ := cmd.Flags().GetString("doc-type")
outputPath, _ := cmd.Flags().GetString("output")
if outputPath == "" {
outputPath = fmt.Sprintf("%s.%s", docToken, fileType)
}
// 获取可选的 User Access Token
userAccessToken := resolveOptionalUserTokenWithFallback(cmd)
// 创建导出任务
fmt.Printf("正在创建导出任务...\n")
ticket, err := client.CreateExportTask(docToken, docType, fileType, userAccessToken)
if err != nil {
return err
}
fmt.Printf(" 任务 ID: %s\n", ticket)
// 轮询等待任务完成
fmt.Printf("正在等待导出完成...\n")
fileToken, err := client.WaitExportTask(ticket, docToken, userAccessToken, 60)
if err != nil {
return err
}
fmt.Printf(" 导出文件 Token: %s\n", fileToken)
// 下载导出文件
fmt.Printf("正在下载文件...\n")
if err := client.DownloadExportFile(fileToken, outputPath, userAccessToken); err != nil {
return err
}
fmt.Printf("导出成功!\n")
fmt.Printf(" 保存路径: %s\n", outputPath)
return nil
},
}
func init() {
docCmd.AddCommand(exportFileCmd)
exportFileCmd.Flags().String("type", "", "导出格式(pdf/docx/xlsx,必填)")
exportFileCmd.Flags().String("doc-type", "docx", "文档类型")
exportFileCmd.Flags().StringP("output", "o", "", "输出文件路径")
exportFileCmd.Flags().String("user-access-token", "", "User Access Token(用于导出无 App 权限的文档)")
mustMarkFlagRequired(exportFileCmd, "type")
}