Brush up the go code
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"covergen/pkg/covergen"
|
||||
)
|
||||
|
||||
// List of colors used for picking a (random) color in the cli
|
||||
var colors = []string{"red", "blue", "yellow", "green"}
|
||||
var colorMap = map[string]covergen.Color{
|
||||
"red": {255, 85, 0},
|
||||
@@ -69,6 +70,7 @@ func main() {
|
||||
chosenColor = colorMap[*color]
|
||||
}
|
||||
|
||||
// Newlines in CLI arguments are given escaped. Unescape them to proper newlines
|
||||
*customer = strings.ReplaceAll(*customer, "\\n", "\n")
|
||||
settings := covergen.CoverSettings{
|
||||
Number: *number,
|
||||
@@ -77,6 +79,7 @@ func main() {
|
||||
HLColor: chosenColor,
|
||||
}
|
||||
|
||||
// If output2 (back page) is not set, generate a combined PDF
|
||||
if *output2 == "" {
|
||||
pdf, err := covergen.GenerateInvoice(settings)
|
||||
|
||||
@@ -88,6 +91,7 @@ func main() {
|
||||
log.Fatal().Err(err).Msg("Failed to write invoice to disk")
|
||||
}
|
||||
} else {
|
||||
// Otherwise, generate the front and back separately
|
||||
front, err := covergen.GenerateFrontCover(settings)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to render front")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Super simple development webserver
|
||||
func main() {
|
||||
err := http.ListenAndServe(":9090", http.FileServer(http.Dir("assets")))
|
||||
if err != nil {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"covergen/pkg/covergen"
|
||||
)
|
||||
|
||||
// jsmap is just a type alias that corresponds to the ES5 Record<string, any>, or just a plain object {}
|
||||
type jsmap = map[string]interface{}
|
||||
|
||||
type covergenArgs struct {
|
||||
@@ -21,6 +22,8 @@ type covergenArgs struct {
|
||||
HLColor string `jsref:"hlColor"`
|
||||
}
|
||||
|
||||
// parseHexColor takes a CSS hex color (#RRGGBB or the shorthand #RGB) and parses out the red, green, and blue components
|
||||
// into a covergen.Color
|
||||
func parseHexColor(s string) (c covergen.Color, err error) {
|
||||
switch len(s) {
|
||||
case 7:
|
||||
@@ -33,11 +36,12 @@ func parseHexColor(s string) (c covergen.Color, err error) {
|
||||
c.B *= 17
|
||||
default:
|
||||
err = fmt.Errorf("invalid length, must be 7 or 4")
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// settingsFromValue tries to parse the WASM boundary-crossing arguments blob into a golang-native settings object
|
||||
func settingsFromValue(arg js.Value) (*covergen.CoverSettings, error) {
|
||||
settings := covergenArgs{
|
||||
NumberPrefix: "offerte",
|
||||
@@ -71,6 +75,7 @@ func settingsFromValue(arg js.Value) (*covergen.CoverSettings, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// pdfToJs takes the pdf object and outputs it into a Uint8Array and passes that back to JS.
|
||||
func pdfToJs(pdf *fpdf.Fpdf) (js.Value, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := pdf.Output(&buf); err != nil {
|
||||
@@ -83,10 +88,11 @@ func pdfToJs(pdf *fpdf.Fpdf) (js.Value, error) {
|
||||
return ta, nil
|
||||
}
|
||||
|
||||
// generateCover is the JS exposed entrypoint to generate a 2 page pdf with both the front and back cover
|
||||
func generateCover(this js.Value, args []js.Value) interface{} {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("recovered", r)
|
||||
fmt.Printf("recovered from critical error: %+v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -119,7 +125,7 @@ func generateCover(this js.Value, args []js.Value) interface{} {
|
||||
func generateSplitCover(this js.Value, args []js.Value) interface{} {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("recovered", r)
|
||||
fmt.Printf("recovered from critical error: %+v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user