hugo-medialog/utils/utils.go

61 lines
1.3 KiB
Go

package utils
import (
"bytes"
"encoding/json"
"fmt"
"os"
"text/template"
)
func CreateDirIfNotExists(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.MkdirAll(dir, 0755)
}
return nil
}
func GenerateMarkdown(templatePath string, outputPath string, data map[string]interface{}) error {
tmpl, err := template.ParseFiles(templatePath)
if err != nil {
return err
}
var output bytes.Buffer
err = tmpl.Execute(&output, data)
if err != nil {
return err
}
return os.WriteFile(outputPath, output.Bytes(), 0644)
}
func Debug(v interface{}, args ...string) {
// utils.Debug(variable)
debug, err := json.MarshalIndent(v, "", " ")
if err != nil {
fmt.Println("Error marshaling JSON:", err)
return
}
// Print a title if there is one
if len(args) > 0 {
fmt.Printf("%s\n", args[0])
}
fmt.Printf("DEBUG:\n%s\n", string(debug))
}
func DebugBody(body []byte) {
// utils.DebugBody(body)
var jsonResponse interface{}
err := json.Unmarshal(body, &jsonResponse)
if err != nil {
fmt.Println("Error unmarshaling JSON:", err)
}
Debug(jsonResponse, "--API RESPONSE--")
}
func Sep() {
separator := "════════════════════════════════════════════════"
fmt.Println(separator)
}