continue to improve badges · boyter/scc@3d993f7

@@ -4,9 +4,10 @@ import (

44

"context"

55

"errors"

66

"fmt"

7-

"log"

7+

"github.com/rs/zerolog/log"

88

"math"

99

"net/http"

10+

"net/url"

1011

"os"

1112

"os/exec"

1213

"regexp"

@@ -15,22 +16,31 @@ import (

1516

"time"

1617

)

171819+

var uniqueCode = "unique_code"

20+

var cache = NewSimpleCache(1000)

21+1822

func main() {

1923

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

20-

_, err := processUrlPath(r.URL.Path)

24+

loc, err := processUrlPath(r.URL.Path)

2125

if err != nil {

2226

w.WriteHeader(http.StatusBadRequest)

2327

_, _ = w.Write([]byte("you be invalid"))

2428

return

2529

}

263031+

process(1, loc)

32+2733

category := strings.TrimSpace(strings.ToLower(r.URL.Query().Get("category")))

283429-

title := "Total lines"

35+

title := ""

30363137

switch category {

38+

case "codes":

39+

fallthrough

3240

case "code":

3341

title = "Code lines"

42+

case "blank":

43+

fallthrough

3444

case "blanks":

3545

title = "Blank lines"

3646

case "comment":

@@ -41,6 +51,8 @@ func main() {

4151

title = "COCOMO $"

4252

case "lines": // lines is the default

4353

fallthrough

54+

case "line": // lines is the default

55+

fallthrough

4456

default:

4557

//

4658

title = "Total lines"

@@ -53,6 +65,7 @@ func main() {

5365

textLength = "200"

5466

}

556768+

log.Info().Str(uniqueCode, "42c5269c").Str("loc", loc.String()).Str("category", category).Send()

5669

w.Header().Set("Content-Type", "image/svg+xml;charset=utf-8")

5770

_, _ = w.Write([]byte(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="100" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h69v20H0z"/><path fill="#4c1" d="M69 0h31v20H69z"/><path fill="url(#b)" d="M0 0h100v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="355" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="590">` + title + `</text><text x="355" y="140" transform="scale(.1)" textLength="590">` + title + `</text><text x="835" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="` + textLength + `">` + s + `</text><text x="835" y="140" transform="scale(.1)" textLength="` + textLength + `">` + s + `</text></g> </svg>`))

5871

})

@@ -61,14 +74,14 @@ func main() {

6174

}

62756376

type location struct {

64-

Location string

77+

Provider string

6578

User string

6679

Repo string

6780

}

68816982

func (l *location) String() string {

70-

//url.Parse("https://"+l.Location)

71-

return ""

83+

parse, _ := url.Parse("https://" + l.Provider + ".com/" + l.User + "/" + l.Repo + ".git")

84+

return parse.String()

7285

}

73867487

// processUrlPath takes in a string path and returns a struct

@@ -81,11 +94,11 @@ func processUrlPath(path string) (location, error) {

8194

path = strings.TrimSuffix(path, "/")

8295

s := strings.Split(path, "/")

8396

if len(s) != 3 {

84-

return location{}, errors.New("")

97+

return location{}, errors.New("invalid path part")

8598

}

869987100

return location{

88-

Location: s[0],

101+

Provider: s[0],

89102

User: s[1],

90103

Repo: s[2],

91104

}, nil

@@ -123,8 +136,12 @@ func formatCount(count float64) string {

123136

return fmt.Sprintf("%v", math.Round(count))

124137

}

125138126-

func process(id int, s string) {

127-

fmt.Println("processing", s)

139+

func process(id int, s location) {

140+

_, ok := cache.Get(s.String())

141+

if ok {

142+

// TODO real thing here

143+

return

144+

}

128145129146

// Clean target just to be sure

130147

cmdArgs := []string{

@@ -136,7 +153,7 @@ func process(id int, s string) {

136153

err := cmd.Run()

137154138155

if err != nil {

139-

fmt.Println("rm start", err.Error())

156+

log.Error().Err(err).Str(uniqueCode, "41b5460d").Str("loc", s.String()).Send()

140157

return

141158

}

142159

@@ -145,23 +162,23 @@ func process(id int, s string) {

145162

ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)

146163

defer cancel()

147164148-

cmd = exec.CommandContext(ctx, "git", "clone", "--depth=1", s+".git", "/tmp/scc-tmp-path-"+strconv.Itoa(id))

165+

cmd = exec.CommandContext(ctx, "git", "clone", "--depth=1", s.String(), "/tmp/scc-tmp-path-"+strconv.Itoa(id))

149166150167

cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")

151168

resp, err := cmd.Output()

152169153170

if ctx.Err() == context.DeadlineExceeded {

154-

fmt.Println("git clone timed out")

171+

log.Error().Str(uniqueCode, "8f8ccc64").Str("loc", s.String()).Msg("git clone timed out")

155172

return

156173

}

157174158175

if err != nil {

159-

fmt.Println("git clone non-zero exit code", string(resp))

176+

log.Error().Err(err).Str(uniqueCode, "f28fb388").Str("loc", s.String()).Str("resp", string(resp)).Msg("git clone non-zero exit code")

160177

return

161178

}

162179163180

// Run scc against what we just cloned

164-

fileName := processPath(s)

181+

fileName := processPath(s.String())

165182166183

if fileName == "" {

167184

return

@@ -179,14 +196,17 @@ func process(id int, s string) {

179196

err = cmd.Run()

180197181198

if err != nil {

182-

fmt.Println("scc", err.Error())

199+

log.Error().Err(err).Str(uniqueCode, "3a74fde3").Str("loc", s.String()).Str("resp", string(resp)).Send()

200+

return

183201

}

184202185-

//err = uploadS3File("sloccloccode", fileName, "/tmp/"+fileName)

186-

//if err != nil {

187-

// fmt.Println("s3 upload", err.Error())

188-

//}

189-

//fmt.Println("uploaded now cleaning up")

203+

file, err := os.ReadFile("/tmp/" + fileName)

204+

if err != nil {

205+

log.Error().Err(err).Str(uniqueCode, "b16570de").Str("loc", s.String()).Str("resp", string(resp)).Send()

206+

return

207+

}

208+

cache.Add(s.String(), file)

209+

//map[string]processor.LanguageSummary{}

190210191211

// Cleanup

192212

cmdArgs = []string{

@@ -198,7 +218,7 @@ func process(id int, s string) {

198218

err = cmd.Run()

199219200220

if err != nil {

201-

fmt.Println("rm cleanup filename", err.Error())

221+

log.Error().Err(err).Str(uniqueCode, "ed40409c").Str("loc", s.String()).Str("resp", string(resp)).Send()

202222

return

203223

}

204224

@@ -211,7 +231,7 @@ func process(id int, s string) {

211231

err = cmd.Run()

212232213233

if err != nil {

214-

fmt.Println("rm cleanup", err.Error())

234+

log.Error().Err(err).Str(uniqueCode, "2bca46a1").Str("loc", s.String()).Str("resp", string(resp)).Send()

215235

return

216236

}

217237

}

@@ -241,7 +261,7 @@ func processPath(s string) string {

241261

func cleanString(s string) string {

242262

reg, err := regexp.Compile("[^a-z0-9-._]+")

243263

if err != nil {

244-

log.Fatal(err)

264+

log.Fatal().Err(err).Send()

245265

}

246266247267

processedString := reg.ReplaceAllString(s, "")