/
get.go
98 lines (85 loc) · 1.99 KB
/
get.go
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
98
package cmd
import (
"os"
"path/filepath"
"strconv"
"strings"
"github.com/levinion/letc/timer"
"github.com/levinion/letc/config"
"github.com/levinion/letc/query"
"github.com/levinion/letc/model"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
_ "embed"
)
var getCmd = &cobra.Command{
Short: "通过题号拉取具体题目",
Use: "get",
Run: func(cmd *cobra.Command, args []string) {
index, err := strconv.Atoi(args[0])
checkErr(err)
Get(index)
},
}
func Get(index int) {
t := timer.NewTimer()
t.Start()
config.ReadConfig()
codeType := viper.GetString("codeType")
slug, diff := query.GetSlug(index)
codeR, needR, titleR := query.GetInter(slug, codeType)
data := &model.Data{Index: index, Diff: diff, CodeR: codeR, NeedR: needR, TitleR: titleR}
codeDir := viper.GetString("codeDir")
var dir string
if viper.GetBool("moduled") {
diff := strings.ToLower(data.Diff)
dir = filepath.Join(codeDir, diff, strconv.Itoa(data.Index)+"."+data.TitleR)
} else {
dir = filepath.Join(codeDir, strconv.Itoa(data.Index)+"."+data.TitleR)
}
os.MkdirAll(dir, os.ModePerm)
codeFilename := "main." + codeType
codeFilePath := filepath.Join(dir, codeFilename)
needFilePath := filepath.Join(dir, "need.md")
if !isExist(codeFilePath) {
color.Cyan("请勿重复拉取文件")
return
} else {
cfile, err := os.Create(codeFilePath)
checkErr(err)
defer cfile.Close()
info := viper.GetString("append.prefix") + data.CodeR
cfile.WriteString(info)
}
if viper.GetBool("function.useNeed") {
if !isExist(needFilePath) {
color.Cyan("请勿重复拉取文件")
return
} else {
nfile, err := os.Create(needFilePath)
checkErr(err)
defer nfile.Close()
nfile.WriteString(needR)
}
}
t.Stop()
}
func isExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
return true
}
if os.IsExist(err) {
return true
}
return false
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func init() {
rootCmd.AddCommand(getCmd)
}