Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor and cleanup the compile command #246

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions pkg/cmd/compile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,32 @@ func (o *CompileOptions) Run() error {
DisableNone: o.DisableNone,
OverrideAST: o.OverrideAST,
}, project, stack)

if o.IsCheck {
if err != nil {
fmt.Print(err)
if err != nil {
// only print err in the check command
if o.IsCheck {
fmt.Println(err)
return nil
} else {
return err
}
}

yaml, err := yamlv3.Marshal(sp.Resources)
if err != nil {
return err
}
if o.Output == Stdout {
fmt.Print(string(yaml))
} else {
if err != nil {
return err
if o.WorkDir != "" {
o.Output = filepath.Join(o.WorkDir, o.Output)
}

yaml, err := yamlv3.Marshal(sp.Resources)
err = os.WriteFile(o.Output, yaml, 0o666)
if err != nil {
return err
}
if o.Output == Stdout {
fmt.Print(string(yaml))
} else {
if o.WorkDir != "" {
o.Output = filepath.Join(o.WorkDir, o.Output)
}

err := os.WriteFile(o.Output, yaml, 0o666)
if err != nil {
return err
}
}
}

return nil
}

Expand Down
26 changes: 17 additions & 9 deletions pkg/generator/kcl/kcl_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ func EnableRPC() bool {
}

func (g *Generator) GenerateSpec(o *generator.Options, stack *projectstack.Stack) (*models.Spec, error) {
optList, err := buildOptions(o.WorkDir, o.Settings, o.Arguments, o.Overrides, o.DisableNone, o.OverrideAST)
compileResult, err := Run(o, stack)
if err != nil {
return nil, err
}

// convert Run result to spec
spec, err := engine.ResourcesYAML2Spec(compileResult.Documents)
if err != nil {
return nil, err
}
return spec, nil
}

func Run(o *generator.Options, stack *projectstack.Stack) (*CompileResult, error) {
optList, err := BuildOptions(o.WorkDir, o.Settings, o.Arguments, o.Overrides, o.DisableNone, o.OverrideAST)
if err != nil {
return nil, err
}
Expand All @@ -66,13 +80,7 @@ func (g *Generator) GenerateSpec(o *generator.Options, stack *projectstack.Stack
if err != nil {
return nil, err
}

// convert compile result to spec
spec, err := engine.ResourcesYAML2Spec(compileResult.Documents)
if err != nil {
return nil, err
}
return spec, nil
return compileResult, err
}

func appendCRDs(workDir string, r *CompileResult) error {
Expand Down Expand Up @@ -122,7 +130,7 @@ func readCRDs(workDir string) ([]interface{}, error) {
return visitor.Visit()
}

func buildOptions(workDir string, settings, arguments, overrides []string, disableNone, overrideAST bool) ([]kclvm.Option, error) {
func BuildOptions(workDir string, settings, arguments, overrides []string, disableNone, overrideAST bool) ([]kclvm.Option, error) {
optList := []kclvm.Option{}
// build settings option
for _, setting := range settings {
Expand Down