-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package modeline | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Config structure for storing settings from modeline | ||
type Config struct { | ||
Nodes []string | ||
Endpoints []string | ||
Templates []string | ||
} | ||
|
||
// ParseModeline parses a modeline string and populates the Config structure | ||
func ParseModeline(line string) (*Config, error) { | ||
config := &Config{} | ||
trimLine := strings.TrimSpace(line) | ||
prefix := "# talm: " | ||
if strings.HasPrefix(trimLine, prefix) { | ||
content := strings.TrimPrefix(trimLine, prefix) | ||
parts := strings.Split(content, ", ") | ||
for _, part := range parts { | ||
keyVal := strings.SplitN(strings.TrimSpace(part), "=", 2) | ||
if len(keyVal) != 2 { | ||
return nil, fmt.Errorf("invalid format of modeline part: %s", part) | ||
} | ||
key := keyVal[0] | ||
val := keyVal[1] | ||
var arr []string | ||
if err := json.Unmarshal([]byte(val), &arr); err != nil { | ||
return nil, fmt.Errorf("error parsing JSON array for key %s, value %s, error: %v", key, val, err) | ||
} | ||
// Assign values to Config fields based on known keys | ||
switch key { | ||
case "nodes": | ||
config.Nodes = arr | ||
case "endpoints": | ||
config.Endpoints = arr | ||
case "templates": | ||
config.Templates = arr | ||
// Ignore unknown keys | ||
} | ||
} | ||
return config, nil | ||
} | ||
return nil, fmt.Errorf("modeline prefix not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package modeline | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParseModeline(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
line string | ||
want *Config | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid modeline with all known keys", | ||
line: `# talm: nodes=["192.168.100.2"], endpoints=["1.2.3.4","127.0.0.1","192.168.100.2"], templates=["templates/controlplane.yaml","templates/worker.yaml"]`, | ||
want: &Config{ | ||
Nodes: []string{"192.168.100.2"}, | ||
Endpoints: []string{"1.2.3.4", "127.0.0.1", "192.168.100.2"}, | ||
Templates: []string{"templates/controlplane.yaml", "templates/worker.yaml"}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "modeline with unknown key", | ||
line: `# talm: nodes=["192.168.100.2"], endpoints=["1.2.3.4","127.0.0.1","192.168.100.2"], unknown=["value"]`, | ||
want: &Config{ | ||
Nodes: []string{"192.168.100.2"}, | ||
Endpoints: []string{"1.2.3.4", "127.0.0.1", "192.168.100.2"}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got, err := ParseModeline(tc.line) | ||
if (err != nil) != tc.wantErr { | ||
t.Errorf("parseModeline() error = %v, wantErr %v", err, tc.wantErr) | ||
return | ||
} | ||
if !tc.wantErr && !reflect.DeepEqual(got, tc.want) { | ||
t.Errorf("parseModeline() got = %v, want %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} |