pixelweaver/config.go

48 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
type Config struct {
Method string `json:method`
OutputDir string `json:outputdir`
InputDir string `json:inputdir`
BaseImage string `json:baseimage`
Dimensions string `json:dimensions`
OutputWidth int `json:output_width`
OutputHeight int `json:output_height`
Grayscale bool
Amount int
}
// loads configuration values from json file
func (c *Config) LoadConfigFromFile(filePath string) {
file, _ := os.Open("conf.json")
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Config{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(configuration.Method)
fmt.Println(configuration.OutputDir)
}
// Set output file dimensions using string symbol (<width>x<height>)
func (c *Config) SetOutputDimensions() {
split := strings.Split(c.Dimensions, "x")
width, _ := strconv.Atoi(split[0])
height, _ := strconv.Atoi(split[1])
c.OutputWidth = width
c.OutputHeight = height
}
// global register to acccess configuration values
var ConfigRegister Config