Skip to content

Commit

Permalink
refactor and cleanup the compile command (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Feb 14, 2023
1 parent d4c20f3 commit 9c39291
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
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

0 comments on commit 9c39291

Please sign in to comment.