fix
This commit is contained in:
@@ -26,12 +26,12 @@ type Controller struct {
|
|||||||
|
|
||||||
// Config represents the entire tellstick.conf structure
|
// Config represents the entire tellstick.conf structure
|
||||||
type Config struct {
|
type Config struct {
|
||||||
User string `json:"user,omitempty"`
|
User string `json:"user,omitempty"`
|
||||||
Group string `json:"group,omitempty"`
|
Group string `json:"group,omitempty"`
|
||||||
DevicePath string `json:"devicePath,omitempty"`
|
DevicePath string `json:"devicePath,omitempty"`
|
||||||
IgnoreControllerConfirmation int `json:"ignoreControllerConfirmation,omitempty"`
|
IgnoreControllerConfirmation int `json:"ignoreControllerConfirmation,omitempty"`
|
||||||
Controllers []Controller `json:"controllers"`
|
Controllers []Controller `json:"controllers"`
|
||||||
Devices []Device `json:"devices"`
|
Devices []Device `json:"devices"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parser handles parsing and writing tellstick.conf files
|
// Parser handles parsing and writing tellstick.conf files
|
||||||
@@ -64,7 +64,7 @@ func (p *Parser) Parse() (*Config, error) {
|
|||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
|
||||||
// Skip empty lines and comments
|
// Skip empty lines and comments
|
||||||
if line == "" || strings.HasPrefix(line, "#") {
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
continue
|
continue
|
||||||
@@ -101,12 +101,12 @@ func (p *Parser) Parse() (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Section detection
|
// Section detection
|
||||||
if strings.HasPrefix(line, "controller") {
|
if strings.HasPrefix(line, "controller {") || strings.HasPrefix(line, "controller{") {
|
||||||
currentSection = "controller"
|
currentSection = "controller"
|
||||||
currentController = &Controller{}
|
currentController = &Controller{}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(line, "device") {
|
if strings.HasPrefix(line, "device {") || strings.HasPrefix(line, "device{") {
|
||||||
currentSection = "device"
|
currentSection = "device"
|
||||||
currentDevice = &Device{
|
currentDevice = &Device{
|
||||||
Parameters: make(map[string]string),
|
Parameters: make(map[string]string),
|
||||||
@@ -134,7 +134,7 @@ func (p *Parser) Parse() (*Config, error) {
|
|||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
key := strings.TrimSpace(parts[0])
|
key := strings.TrimSpace(parts[0])
|
||||||
value := strings.Trim(strings.TrimSpace(parts[1]), "\"")
|
value := strings.Trim(strings.TrimSpace(parts[1]), "\"")
|
||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case "id":
|
case "id":
|
||||||
fmt.Sscanf(value, "%d", ¤tDevice.ID)
|
fmt.Sscanf(value, "%d", ¤tDevice.ID)
|
||||||
@@ -149,13 +149,13 @@ func (p *Parser) Parse() (*Config, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentSection == "controller" && currentController != nil {
|
if currentSection == "controller" && currentController != nil {
|
||||||
parts := strings.SplitN(line, "=", 2)
|
parts := strings.SplitN(line, "=", 2)
|
||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
key := strings.TrimSpace(parts[0])
|
key := strings.TrimSpace(parts[0])
|
||||||
value := strings.Trim(strings.TrimSpace(parts[1]), "\"")
|
value := strings.Trim(strings.TrimSpace(parts[1]), "\"")
|
||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case "id":
|
case "id":
|
||||||
fmt.Sscanf(value, "%d", ¤tController.ID)
|
fmt.Sscanf(value, "%d", ¤tController.ID)
|
||||||
@@ -200,7 +200,7 @@ func (p *Parser) Write(config *Config) error {
|
|||||||
if config.IgnoreControllerConfirmation > 0 {
|
if config.IgnoreControllerConfirmation > 0 {
|
||||||
fmt.Fprintf(w, "ignoreControllerConfirmation = %d\n", config.IgnoreControllerConfirmation)
|
fmt.Fprintf(w, "ignoreControllerConfirmation = %d\n", config.IgnoreControllerConfirmation)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteString("\n")
|
w.WriteString("\n")
|
||||||
|
|
||||||
// Write controllers
|
// Write controllers
|
||||||
@@ -228,12 +228,12 @@ func (p *Parser) Write(config *Config) error {
|
|||||||
if dev.Model != "" {
|
if dev.Model != "" {
|
||||||
fmt.Fprintf(w, " model = \"%s\"\n", dev.Model)
|
fmt.Fprintf(w, " model = \"%s\"\n", dev.Model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write parameters
|
// Write parameters
|
||||||
for key, value := range dev.Parameters {
|
for key, value := range dev.Parameters {
|
||||||
fmt.Fprintf(w, " %s = \"%s\"\n", key, value)
|
fmt.Fprintf(w, " %s = \"%s\"\n", key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteString("}\n\n")
|
w.WriteString("}\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
60
pkg/config/parser_test.go
Normal file
60
pkg/config/parser_test.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParserControllerDetection(t *testing.T) {
|
||||||
|
sampleConfig := `user = "root"
|
||||||
|
group = "plugdev"
|
||||||
|
device {
|
||||||
|
id = 5
|
||||||
|
name = "TF_OFFICE_DESK"
|
||||||
|
controller = 0
|
||||||
|
protocol = "arctech"
|
||||||
|
model = "selflearning-switch"
|
||||||
|
parameters {
|
||||||
|
house = "12168864"
|
||||||
|
unit = "11"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
controller {
|
||||||
|
id = 1
|
||||||
|
type = 2
|
||||||
|
serial = "AE01DHRN"
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
dir := t.TempDir()
|
||||||
|
cfgPath := filepath.Join(dir, "tellstick.conf")
|
||||||
|
if err := os.WriteFile(cfgPath, []byte(sampleConfig), 0o600); err != nil {
|
||||||
|
t.Fatalf("failed writing sample config: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
parser := NewParser(cfgPath)
|
||||||
|
cfg, err := parser.Parse()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got, want := len(cfg.Controllers), 1; got != want {
|
||||||
|
t.Fatalf("expected %d controller, got %d", want, got)
|
||||||
|
}
|
||||||
|
ctrl := cfg.Controllers[0]
|
||||||
|
if ctrl.ID != 1 || ctrl.Serial != "AE01DHRN" {
|
||||||
|
t.Fatalf("unexpected controller parsed: %+v", ctrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got, want := len(cfg.Devices), 1; got != want {
|
||||||
|
t.Fatalf("expected %d device, got %d", want, got)
|
||||||
|
}
|
||||||
|
dev := cfg.Devices[0]
|
||||||
|
if dev.Name != "TF_OFFICE_DESK" {
|
||||||
|
t.Fatalf("unexpected device parsed: %+v", dev)
|
||||||
|
}
|
||||||
|
if val := dev.Parameters["controller"]; val != "0" {
|
||||||
|
t.Fatalf("expected controller parameter stored, got %q", val)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user