11package fetcher
22
33import (
4- "log"
5- "net/url"
6- "strings"
7- "net/http"
4+ "crypto/md5"
5+ "encoding/hex"
86 "io/ioutil"
9- "os"
10- "time"
11- "crypto/md5"
12- "encoding/hex"
7+ "log"
8+ "net/http"
9+ "net/url"
10+ "os"
11+ "strings"
12+ "time"
1313)
1414
15- const AWESOMEREPOURL = "https://github.com/sindresorhus/awesome/"
16- const AWESOMECACHEFOLDER = ".awesomecache"
15+ const AWESOMEREPOURL = "https://github.com/sindresorhus/awesome/"
16+ const AWESOMECACHEFOLDER = ".awesomecache"
1717const RAWGITHUBUSERCONTENT = "https://raw.githubusercontent.com"
1818
1919func FetchAwsomeRootRepo () (string , error ) {
@@ -25,163 +25,162 @@ func FetchAwsomeRepo(repourl string) (string, error) {
2525 CreateCacheFolder ()
2626 }
2727
28- cacheFile := GetCachePath (repourl )
28+ cacheFile := GetCachePath (repourl )
29+
30+ if CacheFileExists (cacheFile ) && CacheFileUptoDate (cacheFile ) {
31+ content , err := ioutil .ReadFile (cacheFile )
2932
30- if CacheFileExists (cacheFile ) && CacheFileUptoDate (cacheFile ) {
31- content , err := ioutil .ReadFile (cacheFile )
32-
33- if err != nil {
34- log .Println (err )
35- return "" , err
36- } else {
37- return string (content ), nil
38- }
39- }
33+ if err != nil {
34+ log .Println (err )
35+ return "" , err
36+ } else {
37+ return string (content ), nil
38+ }
39+ }
4040
41- readmes := GetPossibleReadmeFileURLs (repourl )
41+ readmes := GetPossibleReadmeFileURLs (repourl )
4242
43- for _ ,rurl := range readmes {
44- response , err := http .Get (rurl )
43+ for _ , rurl := range readmes {
44+ response , err := http .Get (rurl )
4545
46- if err != nil {
47- log .Println (err )
48- continue
49- }
46+ if err != nil {
47+ log .Println (err )
48+ continue
49+ }
5050
51- if response .StatusCode == http .StatusNotFound {
52- //log.Println(rurl, "gives 404.")
53- continue
54- }
51+ if response .StatusCode == http .StatusNotFound {
52+ //log.Println(rurl, "gives 404.")
53+ continue
54+ }
5555
56- defer response .Body .Close ()
56+ defer response .Body .Close ()
5757
58- responseData , err := ioutil .ReadAll (response .Body )
58+ responseData , err := ioutil .ReadAll (response .Body )
5959
60- if err != nil {
61- log .Println (err )
62- return "" , err
63- }
60+ if err != nil {
61+ log .Println (err )
62+ return "" , err
63+ }
6464
65- responseString := string (responseData )
65+ responseString := string (responseData )
6666
67- return SaveCache (cacheFile , responseString ), nil
68- }
67+ return SaveCache (cacheFile , responseString ), nil
68+ }
6969
70- return "" , nil
70+ return "" , nil
7171}
7272
7373func GetPossibleReadmeFileURLs (repourl string ) []string {
74- // Parse the URL and ensure there are no errors.
75- u , err := url .Parse (repourl )
76- if err != nil {
77- log .Println (err )
78- }
79-
80- if strings .Count (u .Host , "github.com" ) == 0 {
81- return []string {}
82- }
83-
84- prefix := RAWGITHUBUSERCONTENT + u .Path + "/master/"
85-
86- return []string {
87- prefix + "README" ,
88- prefix + "README.MD" ,
89- prefix + "README.md" ,
90- prefix + "readme" ,
91- prefix + "readme.md" ,
92- prefix + "readme.MD" ,
93- }
74+ // Parse the URL and ensure there are no errors.
75+ u , err := url .Parse (repourl )
76+ if err != nil {
77+ log .Println (err )
78+ }
79+
80+ if strings .Count (u .Host , "github.com" ) == 0 {
81+ return []string {}
82+ }
83+
84+ prefix := RAWGITHUBUSERCONTENT + u .Path + "/master/"
85+
86+ return []string {
87+ prefix + "README" ,
88+ prefix + "README.MD" ,
89+ prefix + "README.md" ,
90+ prefix + "readme" ,
91+ prefix + "readme.md" ,
92+ prefix + "readme.MD" ,
93+ }
9494}
9595
9696func GetCachePath (url string ) string {
97- return GetCacheFolderPath () + string (os .PathSeparator ) + CacheFileName (url )
97+ return GetCacheFolderPath () + string (os .PathSeparator ) + CacheFileName (url )
9898}
9999
100100func GetCacheFolderPath () string {
101101 home , err := os .UserHomeDir ()
102102
103- if err != nil {
104- log .Println (err )
105- return ""
106- }
103+ if err != nil {
104+ log .Println (err )
105+ return ""
106+ }
107107
108- return home + string (os .PathSeparator ) + AWESOMECACHEFOLDER
108+ return home + string (os .PathSeparator ) + AWESOMECACHEFOLDER
109109}
110110
111111func SaveCache (filename string , text string ) string {
112112 file , err := os .Create (filename )
113113
114- if err != nil {
115- log .Println (err )
116- return ""
117- }
114+ if err != nil {
115+ log .Println (err )
116+ return ""
117+ }
118118
119- file .WriteString (text )
119+ file .WriteString (text )
120120
121- return text
121+ return text
122122}
123123
124124func CacheFileName (url string ) string {
125125 hasher := md5 .New ()
126-
127- hasher .Write ([]byte (url ))
128126
129- return hex .EncodeToString (hasher .Sum (nil ))
127+ hasher .Write ([]byte (url ))
128+
129+ return hex .EncodeToString (hasher .Sum (nil ))
130130}
131131
132132func CacheFolderExists () bool {
133133 info , err := os .Stat (GetCacheFolderPath ())
134-
135- if os .IsNotExist (err ) {
136- return false
137- }
138134
139- return info .IsDir ()
135+ if os .IsNotExist (err ) {
136+ return false
137+ }
138+
139+ return info .IsDir ()
140140}
141141
142142func CreateCacheFolder () bool {
143143 err := os .MkdirAll (GetCacheFolderPath (), 0755 )
144144
145145 if err != nil {
146- return false
146+ return false
147147 }
148148
149149 return true
150150}
151151
152152func CacheFileExists (filename string ) bool {
153- info , err := os .Stat (filename )
153+ info , err := os .Stat (filename )
154+
155+ if os .IsNotExist (err ) {
156+ return false
157+ }
154158
155- if os .IsNotExist (err ) {
156- return false
157- }
158-
159- return ! info .IsDir ()
159+ return ! info .IsDir ()
160160}
161161
162162func CacheFileUptoDate (filename string ) bool {
163- info , _ := os .Stat (filename )
164- modifiedtime := info .ModTime ()
163+ info , _ := os .Stat (filename )
164+ modifiedtime := info .ModTime ()
165165
166- return ! IsOlderThanOneDay (modifiedtime )
166+ return ! IsOlderThanOneDay (modifiedtime )
167167}
168168
169169func IsOlderThanOneDay (t time.Time ) bool {
170- return time .Now ().Sub (t ) > 24 * time .Hour
170+ return time .Now ().Sub (t ) > 24 * time .Hour
171171}
172172
173173func PadLeft (str , pad string , lenght int ) string {
174- for {
175- str = pad + str
176- if len (str ) >= lenght {
177- return str [0 :lenght ]
178- }
179- }
174+ for {
175+ str = pad + str
176+ if len (str ) >= lenght {
177+ return str [0 :lenght ]
178+ }
179+ }
180180}
181181
182-
183182func IsUrl (str string ) bool {
184- u , err := url .Parse (str )
185-
186- return err == nil && u .Scheme != "" && u .Host != ""
183+ u , err := url .Parse (str )
184+
185+ return err == nil && u .Scheme != "" && u .Host != ""
187186}
0 commit comments