Skip to content

Commit

Permalink
Merge pull request #8 from MisterMX/feat/first-implementation
Browse files Browse the repository at this point in the history
feat!: Rework config
  • Loading branch information
MisterMX authored Feb 12, 2024
2 parents c12623f + 495e53e commit 3430b50
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 39 deletions.
18 changes: 11 additions & 7 deletions cmd/crossplanereleaser/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,24 @@ func (c *buildCmd) Run(fsys afero.Fs) error {
}

func (c *buildCmd) buildPackages(ctx context.Context, fsys afero.Fs, cfg *v1.Config) error {
for _, pkgCfg := range cfg.XPackages {
for _, b := range cfg.Builds {
buildCfg := &build.PackageBuildConfig{
PackageDir: pkgCfg.Dir,
ExamplesDir: pkgCfg.Examples,
OutputPath: getPackageOutputPath(cfg, &pkgCfg),
PackageDir: b.Dir,
ExamplesDir: b.Examples,
OutputPath: getPackageOutputPath(cfg, &b),
}
// We need to call mkdir here because crank doesn't do it for us
if err := fsys.MkdirAll(filepath.Dir(buildCfg.OutputPath), 0755); err != nil {
return err
}
err := c.builder.BuildPackage(ctx, buildCfg)
if err != nil {
return errors.Wrapf(err, "cannot build package %q", pkgCfg.ID)
return errors.Wrapf(err, "cannot build package %q", b.ID)
}
}
return nil
}

func getPackageOutputPath(cfg *v1.Config, pkgCfg *v1.XPackageConfig) string {
return filepath.Join(cfg.Dist, pkgCfg.ID, pkgCfg.NameTemplate)
func getPackageOutputPath(cfg *v1.Config, build *v1.BuildConfig) string {
return filepath.Join(cfg.Dist, build.ID, build.NameTemplate)
}
31 changes: 23 additions & 8 deletions cmd/crossplanereleaser/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,33 @@ func (c *releaseCmd) Run(fsys afero.Fs) error {
return errors.Wrap(err, "build failed")
}
}
return c.publishPackages(ctx, cfg)
return errors.Wrap(c.publishPackages(ctx, cfg), "cannot push images")
}

func (c *releaseCmd) publishPackages(ctx context.Context, cfg *v1.Config) error {
for _, pkgCfg := range cfg.XPackages {
filename := getPackageOutputPath(cfg, &pkgCfg)
ref, err := name.ParseReference(pkgCfg.NameTemplate)
if err != nil {
return errors.Wrap(err, "cannot parse image name")
for _, push := range cfg.Pushes {
build := getBuildConfigByID(cfg, push.Build)
if build == nil {
return errors.Errorf("no build with ID %q", push.Build)
}
if err := c.publisher.PublishPackage(ctx, filename, ref); err != nil {
return errors.Wrapf(err, "cannot publish package %q", pkgCfg.ID)
filename := getPackageOutputPath(cfg, build)
for _, img := range push.ImageTemplates {
ref, err := name.ParseReference(img)
if err != nil {
return errors.Wrap(err, "cannot parse image name")
}
if err := c.publisher.PublishPackage(ctx, filename, ref); err != nil {
return errors.Wrapf(err, "cannot publish package %q", build.ID)
}
}
}
return nil
}

func getBuildConfigByID(cfg *v1.Config, buildID string) *v1.BuildConfig {
for _, b := range cfg.Builds {
if b.ID == buildID {
return &b
}
}
return nil
Expand Down
14 changes: 7 additions & 7 deletions config/v1/config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package v1

type Config struct {
ProjectName string `json:"project_name"`
XPackages []XPackageConfig `json:"xpackages"`
Dist string `json:"dist"`
Dockers []DockerConfig `json:"dockers"`
ProjectName string `json:"project_name"`
Dist string `json:"dist"`
Builds []BuildConfig `json:"builds"`
Pushes []PushConfig `json:"pushes"`
}

type XPackageConfig struct {
type BuildConfig struct {
ID string `json:"id"`
Dir string `json:"dir"`
Examples string `json:"examples"`
NameTemplate string `json:"name_template"`
}

type DockerConfig struct {
IDs []string `json:"ids"`
type PushConfig struct {
Build string `json:"build"`
ImageTemplates []string `json:"image_templates"`
}
3 changes: 2 additions & 1 deletion internal/build/build_crank.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func (c CrankCLIBackend) exec(ctx context.Context, args ...string) error {

func (c *CrankCLIBackend) BuildPackage(ctx context.Context, cfg *PackageBuildConfig) error {
args := []string{
"xpkg", "build",
fmt.Sprintf("--package-root=%s", cfg.PackageDir),
fmt.Sprintf("--output=%s", cfg.OutputPath),
fmt.Sprintf("--package-file=%s", cfg.OutputPath),
}
if cfg.ExamplesDir != "" {
args = append(args, fmt.Sprintf("--examples-root=%s", cfg.ExamplesDir))
Expand Down
18 changes: 9 additions & 9 deletions internal/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Parse(fsys afero.Fs, filename string) (*v1.Config, error) {
return nil, err
}
cfg := &v1.Config{}
if err := yaml.Unmarshal(raw, cfg); err != nil {
if err := yaml.UnmarshalStrict(raw, cfg, yaml.DisallowUnknownFields); err != nil {
return nil, err
}
err = fillDefaults(filename, cfg)
Expand All @@ -44,18 +44,18 @@ func fillDefaults(filename string, cfg *v1.Config) error {
cfg.ProjectName = valueOrFallback(cfg.ProjectName, filepath.Base(filepath.Dir(filename)))
cfg.Dist = valueOrFallback(cfg.Dist, "dist")

for i := range cfg.XPackages {
cfg.XPackages[i].Examples = valueOrFallback(cfg.XPackages[i].Examples, "examples")
for i := range cfg.Builds {
cfg.Builds[i].Examples = valueOrFallback(cfg.Builds[i].Examples, "examples")

if cfg.XPackages[i].ID == "" {
if len(cfg.XPackages) > 1 {
return errors.New("package ID is required if there is more than one package")
if cfg.Builds[i].ID == "" {
if len(cfg.Builds) > 1 {
return errors.New("build ID is required if there is more than one build")
}
// If there is only one package use the project name as ID
cfg.XPackages[i].ID = cfg.ProjectName
// If there is only one build use the project name as ID
cfg.Builds[i].ID = cfg.ProjectName
}

cfg.XPackages[i].NameTemplate = valueOrFallback(cfg.XPackages[i].NameTemplate, cfg.XPackages[i].ID)
cfg.Builds[i].NameTemplate = valueOrFallback(cfg.Builds[i].NameTemplate, cfg.Builds[i].ID+".xpkg")
}
return nil
}
Expand Down
2 changes: 0 additions & 2 deletions internal/config/project_properties.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -67,7 +66,6 @@ func getEnvMap() map[string]string {
m := make(map[string]string, len(environ))
for _, s := range os.Environ() {
split := strings.SplitN(s, "=", 2)
fmt.Printf("%v\n", split)
m[split[0]] = split[1]
}
return m
Expand Down
10 changes: 5 additions & 5 deletions internal/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
)

func RenderConfigTemplates(cfg *v1.Config, props *ProjectProperties) error {
for di, docker := range cfg.Dockers {
for ii, imgTmpl := range docker.ImageTemplates {
for pi, push := range cfg.Pushes {
for ii, imgTmpl := range push.ImageTemplates {
var err error
cfg.Dockers[di].ImageTemplates[ii], err = renderTemplate(imgTmpl, props)
cfg.Pushes[pi].ImageTemplates[ii], err = renderTemplate(imgTmpl, props)
if err != nil {
return err
}
}
}
for pi, pkgCfg := range cfg.XPackages {
for pi, build := range cfg.Builds {
var err error
cfg.XPackages[pi].NameTemplate, err = renderTemplate(pkgCfg.NameTemplate, props)
cfg.Builds[pi].NameTemplate, err = renderTemplate(build.NameTemplate, props)
if err != nil {
return err
}
Expand Down

0 comments on commit 3430b50

Please sign in to comment.