Skip to content

Commit

Permalink
download svgs to replace remote images from shields with local images
Browse files Browse the repository at this point in the history
  • Loading branch information
gernotstarke committed Dec 8, 2023
1 parent d7b15cb commit fa36349
Show file tree
Hide file tree
Showing 44 changed files with 191 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

<h1>Welcome to shields.io badges for arc42 status-page</h1>

<img src="https://img.shields.io/badge/issues-5-BDB76B"/>
<img src="https://img.shields.io/badge/open_issues-CEA41E"/>
<img src="https://img.shields.io/badge/20-open_issues-CEA41E"/>
<img src="https://img.shields.io/badge/20+-open_issues-CEA41E"/>


<img src="https://img.shields.io/badge/issues-6-BDB76B"/>
<img src="https://img.shields.io/badge/issues-7-BDB76B"/>
<img src="https://img.shields.io/badge/issues-8-BDB76B"/>
Expand All @@ -23,16 +27,6 @@ <h1>Welcome to shields.io badges for arc42 status-page</h1>
<img src="https://img.shields.io/badge/issues-12-BDB76B"/>
<img src="https://img.shields.io/badge/issues-13-BDB76B"/>
<img src="https://img.shields.io/badge/issues-14-BDB76B"/>
<img src="https://img.shields.io/badge/issues-15-BDB76B"/>
<img src="https://img.shields.io/badge/issues-16-BDB76B"/>
<img src="https://img.shields.io/badge/issues-17-BDB76B"/>
<img src="https://img.shields.io/badge/issues-18-BDB76B"/>
<img src="https://img.shields.io/badge/issues-19-BDB76B"/>
<img src="https://img.shields.io/badge/issues-20-BDB76B"/>
<img src="https://img.shields.io/badge/issues-21-BDB76B"/>
<img src="https://img.shields.io/badge/issues-22-BDB76B"/>
<img src="https://img.shields.io/badge/issues-23-BDB76B"/>
<img src="https://img.shields.io/badge/issues-24-BDB76B"/>

<h2>Bugs</h2>
<img src="https://img.shields.io/badge/bugs-1-DC143C"/>
Expand Down
144 changes: 144 additions & 0 deletions cmd/downloadBadgeSVGs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

// Saves svg-badges from shields.io to local storage.
//
// These svg files follow this naming schema:
// <nr>-issues.svg (e.g. `13-issues.svg)
// <nr>-bugs.svg (`3-bugs.svg`)
// If there are more issues than `badge.LocalBadgeSvgThreshold`,
// the naming is `20+issues.svg`.

// we download these badges from shields.io, from URLs like this:
// https://img.shields.io/badge/open_issues-19-BDB76B
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io"
"net/http"
"os"
"site-usage-statistics/internal/badge"
"strconv"
)

// SVGBadgePath is the constant path to the directory where SVG files for badges are stored.
const SVGBadgePath = "./svgs/"

const IssueBadgeFileNameSuffix = "-issues.svg"
const BugBadgeFileNameSuffix = "-bugs.svg"

const issuesColor = "CEA41E"
const bugsColor = "DC143C"

const issueName = "issue"
const bugName = "bug"

const badgeDownloadURLPrefix = "https://img.shields.io/badge/"

// svgFileNameForKindOf creates the filename for the downloaded issue-svg files.
func svgFileNameForKindOf(kindOf string, count int) string {
switch kindOf {
case issueName:
return strconv.Itoa(count) + IssueBadgeFileNameSuffix
case bugName:
return strconv.Itoa(count) + BugBadgeFileNameSuffix
default:
log.Error().Msgf("error creating filename for count %d and kindOf %s", count, kindOf)
return "_error-" + strconv.Itoa(count)
}
}

func badgeColorForKindOf(kindOf string) string {
switch kindOf {
case issueName:
return issuesColor
case bugName:
return bugsColor
default:
return issuesColor
}
}

// nrOfBugsIssuesShown returns the infix needed to create the shields.io URL
func nrOfBugsIssuesShown(count int, kindOf string) string {
if count == 1 {
// note: Singular
return "open_" + kindOf + "-1-"
} else if (count == 0) || (count <= badge.LocalBadgeSvgThreshold) {
// count + Plural
return "open_" + kindOf + "s-" + strconv.Itoa(count) + "-"
} else {
// 20+
return "open_" + kindOf + "s-" + strconv.Itoa(badge.LocalBadgeSvgThreshold) + "+-"
}
}

func openIssueSVGBadgeDownloadURL(count int, kindOf string) string {
var infix = nrOfBugsIssuesShown(count, kindOf)
return badgeDownloadURLPrefix + infix + badgeColorForKindOf(kindOf)
}

func createSVGBadgeDirIfNotPresent(dirName string) {

if _, err := os.Stat(dirName); os.IsNotExist(err) {
errDir := os.MkdirAll(dirName, 0755)
if errDir != nil {
log.Fatal().Msg(errDir.Error())
} else {
log.Info().Msgf("directory %s created", dirName)
}

}
}

// Function to download and save SVG file
func downloadSVG(url string, count int, kindOf string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

fileName := svgFileNameForKindOf(kindOf, count)
log.Info().Msgf("filename is %s", fileName)

file, err := os.Create(SVGBadgePath + fileName)
if err != nil {
return err
}
defer file.Close()

_, err = io.Copy(file, resp.Body)
return err
}

func init() {
// Configure the global logger to write to console/stdout and add file and line number
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: zerolog.TimeFormatUnix}
log.Logger = zerolog.New(output).With().Timestamp().Caller().Logger()
}

func downloadSVGBadgesForKindof(kindOf string) {
log.Info().Msgf("starting to download %ss", kindOf)

// Download and save each SVG file
for count := 1; count <= badge.LocalBadgeSvgThreshold+1; count++ {

// first, load badge for issue
url := openIssueSVGBadgeDownloadURL(count, kindOf)
log.Info().Msgf("loading badge-SVG for %d %ss from %s", count, kindOf, url)
err := downloadSVG(url, count, kindOf)
if err != nil {
panic(err)
}
}
}

func main() {
// create SVGBadge directory, if it does not exist,
// so we have a filesystem location to store SVG files
createSVGBadgeDirIfNotPresent(SVGBadgePath)

downloadSVGBadgesForKindof(issueName)
downloadSVGBadgesForKindof(bugName)

}
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/1-bugs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/1-issues.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/10-bugs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/10-issues.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/11-bugs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/11-issues.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/12-bugs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/12-issues.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/13-bugs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/13-issues.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/14-bugs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions cmd/downloadBadgeSVGs/svgs/14-issues.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fa36349

Please sign in to comment.