Made thos errors better
This commit is contained in:
parent
05ddfef584
commit
863e415310
106
config/config.go
106
config/config.go
@ -1,8 +1,8 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
|
import "io"
|
||||||
import "os"
|
import "os"
|
||||||
import "bufio"
|
import "bufio"
|
||||||
import "errors"
|
|
||||||
import "strings"
|
import "strings"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
@ -20,12 +20,52 @@ const (
|
|||||||
// ErrorIllegalName is thrown when an application name contains illegal
|
// ErrorIllegalName is thrown when an application name contains illegal
|
||||||
// characters such as a slash.
|
// characters such as a slash.
|
||||||
ErrorIllegalName Error = iota
|
ErrorIllegalName Error = iota
|
||||||
|
|
||||||
|
// ErrorNoSeparator is thrown when a configuration file has an
|
||||||
|
// incorrectly formatted key-value pair.
|
||||||
|
ErrorNoSeparator
|
||||||
|
|
||||||
|
// ErrorUnknownParameter is thrown when an unknown key is encountered in
|
||||||
|
// a configuration file.
|
||||||
|
ErrorUnknownParameter
|
||||||
|
|
||||||
|
// ErrorWrongColorLength is thrown when a configuration file has a color
|
||||||
|
// literal with a total length unequal to 7.
|
||||||
|
ErrorWrongColorLength
|
||||||
|
|
||||||
|
// ErrorMalformedColorLiteral is thrown when a configuration file has an
|
||||||
|
// improperly formatted color literal, or a color literal was expected
|
||||||
|
// and something else was encountered.
|
||||||
|
ErrorMalformedColorLiteral
|
||||||
|
|
||||||
|
// ErrorMalformedIntegerLiteral is thrown when a configuration file has
|
||||||
|
// an improperly formatted integer literal, or an integer literal was
|
||||||
|
// expected and something else was encountered.
|
||||||
|
ErrorMalformedIntegerLiteral
|
||||||
|
|
||||||
|
// ErrorMalformedFloatLiteral is thrown when a configuration file has
|
||||||
|
// an improperly formatted float literal, or a float literal was
|
||||||
|
// expected and something else was encountered.
|
||||||
|
ErrorMalformedFloatLiteral
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Error returns a description of the error.
|
||||||
func (err Error) Error () (description string) {
|
func (err Error) Error () (description string) {
|
||||||
switch err {
|
switch err {
|
||||||
case ErrorIllegalName:
|
case ErrorIllegalName:
|
||||||
description = "name contains illegal characters"
|
description = "name contains illegal characters"
|
||||||
|
case ErrorNoSeparator:
|
||||||
|
description = "key:value pair has no separator"
|
||||||
|
case ErrorUnknownParameter:
|
||||||
|
description = "unknown parameter"
|
||||||
|
case ErrorWrongColorLength:
|
||||||
|
description = "color literal has the wrong length"
|
||||||
|
case ErrorMalformedColorLiteral:
|
||||||
|
description = "malformed color literal"
|
||||||
|
case ErrorMalformedIntegerLiteral:
|
||||||
|
description = "malformed integer literal"
|
||||||
|
case ErrorMalformedFloatLiteral:
|
||||||
|
description = "malformed float literal"
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@ -36,10 +76,12 @@ type Type int
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// string
|
// string
|
||||||
|
// It is just a basic string with inner whitespace preserved. No quotes
|
||||||
|
// should be used in the file.
|
||||||
TypeString Type = iota
|
TypeString Type = iota
|
||||||
|
|
||||||
// Type: image/color.RGBA
|
// Type: image/color.RGBA
|
||||||
// Represented as a 24 bit hexidecimal number (case insensitive)
|
// Represented as a 24 bit hexadecimal number (case insensitive)
|
||||||
// preceded with a # sign where the first two digits represent the red
|
// preceded with a # sign where the first two digits represent the red
|
||||||
// channel, the middle two digits represent the green channel, and the
|
// channel, the middle two digits represent the green channel, and the
|
||||||
// last two digits represent the blue channel.
|
// last two digits represent the blue channel.
|
||||||
@ -112,16 +154,24 @@ func (config *Config) Load (name string) (err error) {
|
|||||||
configDirs = append(configDirs, configHome)
|
configDirs = append(configDirs, configHome)
|
||||||
|
|
||||||
for _, directory := range configDirs {
|
for _, directory := range configDirs {
|
||||||
config.loadFile(filepath.Join(directory, name, name + ".conf"))
|
path := filepath.Join(directory, name, name + ".conf")
|
||||||
|
|
||||||
|
file, fileErr := os.Open(path)
|
||||||
|
if fileErr != nil { continue }
|
||||||
|
parseErr := config.loadFile(file)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if parseErr != nil {
|
||||||
|
println (
|
||||||
|
"config: error in file", path +
|
||||||
|
":", parseErr.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) loadFile (path string) {
|
func (config *Config) loadFile (file io.Reader) (err error) {
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil { return }
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
@ -136,22 +186,16 @@ func (config *Config) loadFile (path string) {
|
|||||||
|
|
||||||
key, value, found := strings.Cut(scanner.Text(), ":")
|
key, value, found := strings.Cut(scanner.Text(), ":")
|
||||||
if !found {
|
if !found {
|
||||||
println (
|
err = ErrorNoSeparator
|
||||||
"config: error in file", path +
|
return
|
||||||
": key-value separator missing")
|
|
||||||
println(scanner.Text())
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
key = strings.TrimSpace(key)
|
key = strings.TrimSpace(key)
|
||||||
value = strings.TrimSpace(value)
|
value = strings.TrimSpace(value)
|
||||||
|
|
||||||
what, isKnown := config.LegalParameters[key]
|
what, isKnown := config.LegalParameters[key]
|
||||||
if !isKnown {
|
if !isKnown {
|
||||||
println (
|
err = ErrorUnknownParameter
|
||||||
"config: error in file", path +
|
return
|
||||||
": unknown parameter")
|
|
||||||
println(scanner.Text())
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch what {
|
switch what {
|
||||||
@ -161,24 +205,15 @@ func (config *Config) loadFile (path string) {
|
|||||||
case TypeColor:
|
case TypeColor:
|
||||||
var valueColor color.Color
|
var valueColor color.Color
|
||||||
valueColor, err = parseColor(value)
|
valueColor, err = parseColor(value)
|
||||||
if err != nil {
|
if err != nil { return }
|
||||||
println (
|
|
||||||
"config: error in file", path +
|
|
||||||
":", err)
|
|
||||||
println(scanner.Text())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
config.Parameters[key] = valueColor
|
config.Parameters[key] = valueColor
|
||||||
|
|
||||||
case TypeInteger:
|
case TypeInteger:
|
||||||
var valueInt int
|
var valueInt int
|
||||||
valueInt, err = strconv.Atoi(value)
|
valueInt, err = strconv.Atoi(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println (
|
err = ErrorMalformedIntegerLiteral
|
||||||
"config: error in file", path +
|
return
|
||||||
": malformed integer literal")
|
|
||||||
println(scanner.Text())
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
config.Parameters[key] = valueInt
|
config.Parameters[key] = valueInt
|
||||||
|
|
||||||
@ -186,11 +221,8 @@ func (config *Config) loadFile (path string) {
|
|||||||
var valueFloat float64
|
var valueFloat float64
|
||||||
valueFloat, err = strconv.ParseFloat(value, 64)
|
valueFloat, err = strconv.ParseFloat(value, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println (
|
err = ErrorMalformedFloatLiteral
|
||||||
"config: error in file", path +
|
return
|
||||||
": malformed float literal")
|
|
||||||
println(scanner.Text())
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
config.Parameters[key] = valueFloat
|
config.Parameters[key] = valueFloat
|
||||||
|
|
||||||
@ -211,14 +243,14 @@ func (config *Config) loadFile (path string) {
|
|||||||
func parseColor (value string) (valueColor color.Color, err error) {
|
func parseColor (value string) (valueColor color.Color, err error) {
|
||||||
if value[0] == '#' {
|
if value[0] == '#' {
|
||||||
if len(value) != 7 {
|
if len(value) != 7 {
|
||||||
err = errors.New("wrong length color literal")
|
err = ErrorWrongColorLength
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var colorInt uint64
|
var colorInt uint64
|
||||||
colorInt, err = strconv.ParseUint(value[1:7], 16, 24)
|
colorInt, err = strconv.ParseUint(value[1:7], 16, 24)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.New("malformed color literal")
|
err = ErrorMalformedColorLiteral
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,7 +261,7 @@ func parseColor (value string) (valueColor color.Color, err error) {
|
|||||||
A: 0xFF,
|
A: 0xFF,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = errors.New("malformed color literal")
|
err = ErrorMalformedColorLiteral
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user