chore: add icons.json file (#10085)

This commit is contained in:
Kayla Washburn
2023-10-06 13:46:37 -06:00
committed by GitHub
parent 36f3151b71
commit 71ad5909f2
4 changed files with 143 additions and 1 deletions
+60
View File
@@ -0,0 +1,60 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
func generateIconList(path string) int {
if path == "" {
return 0 // skip
}
files, err := os.ReadDir("site/static/icon/")
if err != nil {
_, _ = fmt.Println("failed to read site/static/icon/ directory")
_, _ = fmt.Println("err:", err.Error())
return 71 // OSERR
}
icons := make([]string, len(files))
i := 0
for _, file := range files {
if !file.Type().IsRegular() {
continue
}
icons[i] = file.Name()
i++
}
icons = icons[:i]
outputFile, err := os.Create(path)
if err != nil {
_, _ = fmt.Println("failed to create file")
_, _ = fmt.Println("err:", err.Error())
return 73 // CANTCREAT
}
defer outputFile.Close()
iconsJSON, err := json.Marshal(icons)
if err != nil {
_, _ = fmt.Println("failed to serialize JSON")
_, _ = fmt.Println("err:", err.Error())
return 70 // SOFTWARE
}
written, err := outputFile.Write(iconsJSON)
if err != nil || written != len(iconsJSON) {
_, _ = fmt.Println("failed to write JSON")
if err != nil {
_, _ = fmt.Println("err:", err.Error())
}
return 74 // IOERR
}
_, _ = fmt.Println(green.Sprintf("==>"), path)
return 0
}
+19
View File
@@ -0,0 +1,19 @@
package main
import (
"flag"
"os"
"github.com/fatih/color"
)
var green = color.New(color.FgGreen).Add(color.Bold)
func main() {
var iconsPath string
flag.StringVar(&iconsPath, "icons", "", "the path to place icons.json at")
flag.Parse()
status := generateIconList(iconsPath)
os.Exit(status)
}