Skip to content

Commit

Permalink
refactor: kcl settings file parse code
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Oct 25, 2023
1 parent f9ea93c commit d636cf5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 153 deletions.
1 change: 0 additions & 1 deletion examples/hello/hello.k

This file was deleted.

43 changes: 0 additions & 43 deletions examples/hello/main.go

This file was deleted.

118 changes: 9 additions & 109 deletions pkg/settings/utils_settings_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type SettingsFile struct {
}

type ConfigStruct struct {
InputFiles []string `yaml:"file"`
InputFile []string `yaml:"file"`
InputFiles []string `yaml:"files"`
Output string `yaml:"output"`

Overrides []string `yaml:"overrides"`
Expand Down Expand Up @@ -75,9 +76,7 @@ func LoadFile(filename string, src interface{}) (f *SettingsFile, err error) {

var settings SettingsFile
if err := yaml.Unmarshal([]byte(code), &settings); err != nil {
if err2 := decodeTestFormatSettingsFile(code, &settings); err2 != nil {
return nil, err
}
return nil, err
}

settings.Filename = filename
Expand Down Expand Up @@ -109,7 +108,12 @@ func (settings *SettingsFile) To_ExecProgram_Args() *gpyrpc.ExecProgram_Args {
pkgroot, _, _ := tools_list.FindPkgInfo(args.WorkDir)

// Input files may be a KCL file folder or a single KCL file.
for _, s := range settings.Config.InputFiles {

var files []string
files = append(files, settings.Config.InputFile...)
files = append(files, settings.Config.InputFiles...)

for _, s := range files {
if strings.Contains(s, "${PWD}") {
s = strings.ReplaceAll(s, "${PWD}", args.WorkDir)
}
Expand Down Expand Up @@ -187,107 +191,3 @@ func (settings *SettingsFile) To_ExecProgram_Args() *gpyrpc.ExecProgram_Args {

return args
}

// kcl_options: -Y config1.yaml config2.yaml
// kcl_options: -D key0=value0 -D key1=value1 -Y temp.yaml
// kcl_options: -O :alice.labels.skin=white
// kcl_options: -S :JohnDoe.* -S :list_data.* -S :dict_data.*
// kcl_options: -d
// kcl_options: -r
// kcl_options: -n
// kcl_options: pkg.k
func decodeTestFormatSettingsFile(src string, settings *SettingsFile) error {
// kcl_options: -Y config1.yaml config2.yaml
var test_settings_yaml struct {
CmdArgs string `yaml:"kcl_options"`
}
err := yaml.Unmarshal([]byte(src), &test_settings_yaml)
if err != nil {
return fmt.Errorf("invalid settings file: %v, %v", err, src)
}

cmdArgs := strings.Fields(test_settings_yaml.CmdArgs)
*settings = SettingsFile{}

for len(cmdArgs) > 0 {
switch cmdArgs[0] {
case "-Y":
cmdArgs = cmdArgs[1:]
for len(cmdArgs) > 0 {
if strings.HasSuffix(cmdArgs[0], ".yaml") || strings.HasSuffix(cmdArgs[0], ".yml") {
settings.Config.InputFiles = append(settings.Config.InputFiles, cmdArgs[0])
cmdArgs = cmdArgs[1:]
continue
}
break
}
case "-D":
cmdArgs = cmdArgs[1:]
if len(cmdArgs) == 0 {
return fmt.Errorf("invalid kcl_options: %v %v", "-D", cmdArgs)
}

_D_arg := cmdArgs[0]
cmdArgs = cmdArgs[1:]

kv := strings.Split(_D_arg, "=")
if len(kv) == 0 {
return fmt.Errorf("invalid kcl_options: %v %v", "-D", cmdArgs)
}
if len(kv) < 2 {
kv = append(kv, "")
}

settings.Options = append(settings.Options, KeyValueStruct{
Key: kv[0],
Value: kv[1],
})

case "-O":
// "--overrides",
cmdArgs = cmdArgs[1:]
if len(cmdArgs) == 0 {
return fmt.Errorf("invalid kcl_options: %v %v", "-O", cmdArgs)
}

_O_arg := cmdArgs[0]
cmdArgs = cmdArgs[1:]

settings.Config.Overrides = append(settings.Config.Overrides, _O_arg)

case "-S":
// "--path-selector",
cmdArgs = cmdArgs[1:]
if len(cmdArgs) == 0 {
return fmt.Errorf("invalid kcl_options: %v %v", "-S", cmdArgs)
}

_S_arg := cmdArgs[0]
cmdArgs = cmdArgs[1:]

settings.Config.PathSelector = append(settings.Config.PathSelector, _S_arg)

case "-n":
// --disable-none
cmdArgs = cmdArgs[1:]
settings.Config.DisableNone = true
case "-r":
// --strict-range-check
cmdArgs = cmdArgs[1:]
settings.Config.StrictRangeCheck = true
case "-d":
// --debug
cmdArgs = cmdArgs[1:]
settings.Config.Debug = true
default:
if strings.HasSuffix(cmdArgs[0], ".k") {
settings.Config.InputFiles = append(settings.Config.InputFiles, cmdArgs[0])
cmdArgs = cmdArgs[1:]
} else {
return fmt.Errorf("invalid kcl_options: %v", test_settings_yaml.CmdArgs)
}
}
}

return nil
}

0 comments on commit d636cf5

Please sign in to comment.