Skip to content

Commit

Permalink
Enhance scripts/include.go
Browse files Browse the repository at this point in the history
Write tmp data to a buffer rather than a temp file. This is faster and robuster.
  • Loading branch information
apocelipes committed Sep 27, 2024
1 parent 7730148 commit c8f09b4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
114 changes: 57 additions & 57 deletions SCC-OUTPUT-REPORT.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<tbody><tr>
<th>Go</th>
<th>30</th>
<th>9515</th>
<th>1460</th>
<th>456</th>
<th>7599</th>
<th>1516</th>
<th>254099</th>
<th>4050</th>
<th>9627</th>
<th>1481</th>
<th>468</th>
<th>7678</th>
<th>1536</th>
<th>256617</th>
<th>4092</th>
</tr><tr>
<td>processor/formatters.go</td>
<td></td>
Expand Down Expand Up @@ -93,13 +93,13 @@
</tr><tr>
<td>cmd/badges/main.go</td>
<td></td>
<td>342</td>
<td>345</td>
<td>59</td>
<td>14</td>
<td>269</td>
<td>47</td>
<td>7991</td>
<td>202</td>
<td>272</td>
<td>50</td>
<td>8129</td>
<td>204</td>
</tr><tr>
<td>processor/workers_tokei_test.go</td>
<td></td>
Expand All @@ -113,13 +113,13 @@
</tr><tr>
<td>processor/detector.go</td>
<td></td>
<td>236</td>
<td>237</td>
<td>44</td>
<td>33</td>
<td>159</td>
<td>160</td>
<td>57</td>
<td>6232</td>
<td>151</td>
<td>6243</td>
<td>152</td>
</tr><tr>
<td>processor/file_test.go</td>
<td></td>
Expand Down Expand Up @@ -160,6 +160,16 @@
<td>50</td>
<td>3766</td>
<td>99</td>
</tr><tr>
<td>cmd/badges/simplecache.go</td>
<td></td>
<td>161</td>
<td>28</td>
<td>13</td>
<td>120</td>
<td>20</td>
<td>3070</td>
<td>94</td>
</tr><tr>
<td>processor/processor_test.go</td>
<td></td>
Expand All @@ -181,15 +191,15 @@
<td>1911</td>
<td>60</td>
</tr><tr>
<td>cmd/badges/simplecache.go</td>
<td>cmd/badges/simplecache_test.go</td>
<td></td>
<td>109</td>
<td>102</td>
<td>21</td>
<td>3</td>
<td>78</td>
<td>17</td>
<td>4</td>
<td>88</td>
<td>14</td>
<td>1931</td>
<td>75</td>
<td>2024</td>
<td>52</td>
</tr><tr>
<td>processor/structs_test.go</td>
<td></td>
Expand All @@ -203,13 +213,13 @@
</tr><tr>
<td>scripts/include.go</td>
<td></td>
<td>76</td>
<td>15</td>
<td>8</td>
<td>53</td>
<td>82</td>
<td>16</td>
<td>1796</td>
<td>56</td>
<td>8</td>
<td>58</td>
<td>19</td>
<td>2043</td>
<td>60</td>
</tr><tr>
<td>processor/processor_unix.go</td>
<td></td>
Expand All @@ -230,16 +240,6 @@
<td>6</td>
<td>1316</td>
<td>37</td>
</tr><tr>
<td>cmd/badges/simplecache_test.go</td>
<td></td>
<td>52</td>
<td>12</td>
<td>0</td>
<td>40</td>
<td>9</td>
<td>1041</td>
<td>30</td>
</tr><tr>
<td>processor/cocomo.go</td>
<td></td>
Expand All @@ -250,16 +250,6 @@
<td>0</td>
<td>2209</td>
<td>35</td>
</tr><tr>
<td>processor/bloom.go</td>
<td></td>
<td>37</td>
<td>7</td>
<td>12</td>
<td>18</td>
<td>2</td>
<td>1062</td>
<td>29</td>
</tr><tr>
<td>processor/cocomo_test.go</td>
<td></td>
Expand All @@ -270,6 +260,16 @@
<td>6</td>
<td>686</td>
<td>23</td>
</tr><tr>
<td>processor/bloom.go</td>
<td></td>
<td>37</td>
<td>7</td>
<td>12</td>
<td>18</td>
<td>2</td>
<td>1062</td>
<td>29</td>
</tr><tr>
<td>processor/helpers_test.go</td>
<td></td>
Expand Down Expand Up @@ -324,15 +324,15 @@
<tfoot><tr>
<th>Total</th>
<th>30</th>
<th>9515</th>
<th>1460</th>
<th>456</th>
<th>7599</th>
<th>1516</th>
<th>254099</th>
<th>4050</th>
<th>9627</th>
<th>1481</th>
<th>468</th>
<th>7678</th>
<th>1536</th>
<th>256617</th>
<th>4092</th>
</tr>
<tr>
<th colspan="9">Estimated Cost to Develop (organic) $227,190<br>Estimated Schedule Effort (organic) 7.83 months<br>Estimated People Required (organic) 2.58<br></th>
<th colspan="9">Estimated Cost to Develop (organic) $229,670<br>Estimated Schedule Effort (organic) 7.86 months<br>Estimated People Required (organic) 2.59<br></th>
</tr></tfoot>
</table></body></html>
5 changes: 4 additions & 1 deletion cmd/badges/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ func main() {

addr := ":8080"
log.Info().Str(uniqueCode, "1876ce1e").Str("addr", addr).Msg("serving")
http.ListenAndServe(addr, nil).Error()
if err := http.ListenAndServe(addr, nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error().Str(uniqueCode, "c28556e8").Err(err).Send()
os.Exit(1)
}
}

func calculate(category string, wage int, res []processor.LanguageSummary) (string, int64) {
Expand Down
3 changes: 2 additions & 1 deletion processor/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func scanForSheBang(content []byte) (string, error) {
candidate1 := ""
candidate2 := ""

loop:
for i := range content {
switch state {
case 0: // Deals with whitespace after #! and before first /
Expand Down Expand Up @@ -128,7 +129,7 @@ func scanForSheBang(content []byte) (string, error) {
state = 4
}
case 4:
break
break loop
}
}

Expand Down
4 changes: 2 additions & 2 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ func fileSummarizeMulti(input chan *FileJob) string {
}
close(i)

var val = ""
var val string

switch strings.ToLower(t[0]) {
case "tabular":
Expand All @@ -861,8 +861,8 @@ func fileSummarizeMulti(input chan *FileJob) string {
case "csv":
val = toCSV(i)
case "csv-stream":
val = toCSVStream(i)
// special case where we want to ignore writing to stdout to disk as it's already done
_ = toCSVStream(i)
continue
case "html":
val = toHtml(i)
Expand Down
34 changes: 20 additions & 14 deletions scripts/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
Expand All @@ -18,51 +19,56 @@ const constantsFile = "./processor/constants.go"
// and encodes them as strings literals in constants.go
func generateConstants() error {
files, _ := os.ReadDir(".")
out, err := os.CreateTemp(".", "temp_constants")
if err != nil {
return fmt.Errorf("failed to open temp file: %v", err)
}
defer os.Remove(out.Name())
buf := &bytes.Buffer{}

// Open constants
out.WriteString("package processor \n\nconst (\n")
_, _ = buf.WriteString("package processor\n\nconst (\n")

for _, f := range files {
if strings.HasPrefix(f.Name(), "languages") && strings.HasSuffix(f.Name(), ".json") {
f, err := os.Open(f.Name())
if err != nil {
return fmt.Errorf("failed to open file '%s': %v", f.Name(), err)
}
defer f.Close()

// validate the json by decoding into an empty struct
if err := json.NewDecoder(f).Decode(&struct{}{}); err != nil {
return fmt.Errorf("failed to validate json in file '%s': %v", f.Name(), err)
}

// Reset position
f.Seek(0, io.SeekStart)
if _, err := f.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("failed to reset file position '%s': %v", f.Name(), err)
}

// The constant variable name
out.WriteString(strings.TrimSuffix(f.Name(), ".json") + " = `")
_ = buf.WriteByte('\t')
_, _ = buf.WriteString(strings.TrimSuffix(f.Name(), ".json") + " = `")

enc := base64.NewEncoder(base64.StdEncoding, out)
enc := base64.NewEncoder(base64.StdEncoding, buf)
gz, _ := gzip.NewWriterLevel(enc, gzip.BestSpeed)
if _, err := io.Copy(gz, f); err != nil {
return fmt.Errorf("failed to encode file '%s': %v", f.Name(), err)
}
gz.Close()
enc.Close()

out.WriteString("`\n")
_, _ = buf.WriteString("`\n")
}
}

// Close out constants
out.WriteString(")\n")
out.Close()
_, _ = buf.WriteString(")\n")

out, err := os.OpenFile(constantsFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open constants file: %v", err)
}
defer out.Close()

if err := os.Rename(out.Name(), constantsFile); err != nil {
return fmt.Errorf("%v", err)
if _, err := io.Copy(out, buf); err != nil {
return fmt.Errorf("failed to write constants file %v", err)
}

return nil
Expand Down

0 comments on commit c8f09b4

Please sign in to comment.