@@ -51,31 +51,34 @@ func getCachePath(f string) string {
51
51
if err == nil && u .HomeDir != "" {
52
52
dir = path .Join (u .HomeDir , ".leetcode" )
53
53
}
54
- filename := getFilePath (path .Join (dir , f ))
55
- return filename
54
+ return path .Join (dir , f )
56
55
}
57
56
58
- func getFilePath (f string ) string {
59
- dir := path .Dir (f )
60
- if dir != "" {
61
- err := os . MkdirAll ( dir , 0755 )
62
- checkErr ( err )
57
+ func getFilePath (filename string ) string {
58
+ if dir := path .Dir (filename ); dir != "" {
59
+ if err := os . MkdirAll ( dir , 0755 ); err != nil {
60
+ checkErr ( err )
61
+ }
63
62
}
64
- return f
63
+ return filename
64
+ }
65
+
66
+ func filePutContents (filename string , data []byte ) {
67
+ filename = getFilePath (filename )
68
+ err = ioutil .WriteFile (filename , data , 0644 )
69
+ checkErr (err )
65
70
}
66
71
67
- func filePutContents ( filename string , v interface {}) {
72
+ func jsonEncode ( v interface {}) [] byte {
68
73
data , err := json .Marshal (v )
69
74
checkErr (err )
70
75
dst := bytes.Buffer {}
71
76
err = json .Indent (& dst , data , "" , "\t " )
72
77
checkErr (err )
73
- err = ioutil .WriteFile (getCachePath (filename ), dst .Bytes (), 0644 )
74
- checkErr (err )
78
+ return dst .Bytes ()
75
79
}
76
-
77
80
func saveCookies (cookies []* http.Cookie ) {
78
- filePutContents (cookiesFile , cookies )
81
+ filePutContents (getCachePath ( cookiesFile ), jsonEncode ( cookies ) )
79
82
}
80
83
81
84
func getCookies () (cookies []* http.Cookie ) {
@@ -87,7 +90,7 @@ func getCookies() (cookies []*http.Cookie) {
87
90
}
88
91
89
92
func saveCredential (data url.Values ) {
90
- filePutContents (credentialsFile , data )
93
+ filePutContents (getCachePath ( credentialsFile ), jsonEncode ( data ) )
91
94
}
92
95
93
96
func getCredential () (data url.Values ) {
0 commit comments