-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reorg generators packages and simplify namings (#459)
- Loading branch information
Showing
12 changed files
with
243 additions
and
237 deletions.
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
34 changes: 0 additions & 34 deletions
34
pkg/generator/appconfiguration/app_configurations_generator.go
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
pkg/generator/appconfiguration/generator/app_configurations_generator.go
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,92 @@ | ||
package generator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"kusionstack.io/kusion/pkg/generator" | ||
"kusionstack.io/kusion/pkg/generator/appconfiguration" | ||
"kusionstack.io/kusion/pkg/generator/appconfiguration/generator/workload" | ||
"kusionstack.io/kusion/pkg/models" | ||
appmodel "kusionstack.io/kusion/pkg/models/appconfiguration" | ||
"kusionstack.io/kusion/pkg/projectstack" | ||
) | ||
|
||
type AppsGenerator struct { | ||
Apps map[string]appmodel.AppConfiguration | ||
} | ||
|
||
func (acg *AppsGenerator) GenerateSpec( | ||
o *generator.Options, | ||
project *projectstack.Project, | ||
stack *projectstack.Stack, | ||
) (*models.Spec, error) { | ||
spec := &models.Spec{ | ||
Resources: []models.Resource{}, | ||
} | ||
|
||
gfs := []appconfiguration.NewGeneratorFunc{} | ||
appconfiguration.ForeachOrdered(acg.Apps, func(appName string, app appmodel.AppConfiguration) error { | ||
gfs = append(gfs, NewAppConfigurationGeneratorFunc(project.Name, appName, &app)) | ||
return nil | ||
}) | ||
if err := appconfiguration.CallGenerators(spec, gfs...); err != nil { | ||
return nil, err | ||
} | ||
|
||
return spec, nil | ||
} | ||
|
||
type appConfigurationGenerator struct { | ||
projectName string | ||
appName string | ||
app *appmodel.AppConfiguration | ||
} | ||
|
||
func NewAppConfigurationGenerator( | ||
projectName, appName string, | ||
app *appmodel.AppConfiguration, | ||
) (appconfiguration.Generator, error) { | ||
if len(projectName) == 0 { | ||
return nil, fmt.Errorf("project name must not be empty") | ||
} | ||
|
||
if len(appName) == 0 { | ||
return nil, fmt.Errorf("app name must not be empty") | ||
} | ||
|
||
if app == nil { | ||
return nil, fmt.Errorf("can not find app configuration when generating the Spec") | ||
} | ||
|
||
return &appConfigurationGenerator{ | ||
projectName: projectName, | ||
appName: appName, | ||
app: app, | ||
}, nil | ||
} | ||
|
||
func NewAppConfigurationGeneratorFunc( | ||
projectName, appName string, | ||
app *appmodel.AppConfiguration, | ||
) appconfiguration.NewGeneratorFunc { | ||
return func() (appconfiguration.Generator, error) { | ||
return NewAppConfigurationGenerator(projectName, appName, app) | ||
} | ||
} | ||
|
||
func (g *appConfigurationGenerator) Generate(spec *models.Spec) error { | ||
if spec.Resources == nil { | ||
spec.Resources = make(models.Resources, 0) | ||
} | ||
|
||
gfs := []appconfiguration.NewGeneratorFunc{ | ||
NewNamespaceGeneratorFunc(g.projectName), | ||
workload.NewWorkloadGeneratorFunc(g.projectName, g.appName, g.app.Workload), | ||
} | ||
|
||
if err := appconfiguration.CallGenerators(spec, gfs...); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...on/generators/namespace_generator_test.go → ...ion/generator/namespace_generator_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package generators | ||
package generator | ||
|
||
import ( | ||
"testing" | ||
|
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
Oops, something went wrong.