Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
fix: make version a real version string (#425)
Browse files Browse the repository at this point in the history
Closes #261
  • Loading branch information
technosophos authored Nov 15, 2018
1 parent 4311a67 commit d3aacd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
34 changes: 33 additions & 1 deletion pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"sync"
"time"

"github.com/Masterminds/semver"

"github.com/deis/duffle/pkg/bundle"
"github.com/deis/duffle/pkg/duffle/manifest"
)
Expand All @@ -18,6 +20,10 @@ import (
type Builder struct {
ID string
LogsDir string
// If this is true, versions will contain build metadata
// Example:
// 0.1.2+2c3c59e8a5adad62d2245cbb7b2a8685b1a9a717
VersionWithBuildMetadata bool
}

// New returns a new Builder
Expand Down Expand Up @@ -88,7 +94,16 @@ func (b *Builder) PrepareBuild(bldr *Builder, mfst *manifest.Manifest, appDir st
Image: c.URI(),
ImageType: c.Type(),
}}
bf.Version = strings.Split(c.URI(), ":")[1]
//bf.Version = strings.Split(c.URI(), ":")[1]
baseVersion := mfst.Version
if baseVersion == "" {
baseVersion = "0.1.0"
}
newver, err := b.version(baseVersion, strings.Split(c.URI(), ":")[1])
if err != nil {
return nil, nil, err
}
bf.Version = newver
} else {
bf.Images = append(bf.Images, bundle.Image{Name: c.Name(), URI: c.URI()})
}
Expand All @@ -104,6 +119,23 @@ func (b *Builder) PrepareBuild(bldr *Builder, mfst *manifest.Manifest, appDir st
return app, bf, nil
}

func (b *Builder) version(baseVersion, sha string) (string, error) {
sv, err := semver.NewVersion(baseVersion)
if err != nil {
return baseVersion, err
}

if b.VersionWithBuildMetadata {
newsv, err := sv.SetMetadata(sha)
if err != nil {
return baseVersion, err
}
return newsv.String(), nil
}

return sv.String(), nil
}

// Build passes the context of each component to its respective builder
func (b *Builder) Build(ctx context.Context, app *AppContext) error {
if err := buildComponents(ctx, app); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/duffle/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Manifest represents a duffle manifest.
type Manifest struct {
Name string `mapstructure:"name"`
Version string `mapstructure:"version"`
Description string `mapstructure:"description"`
Keywords []string `mapstructure:"keywords"`
Maintainers []bundle.Maintainer `mapstructure:"maintainers"`
Expand Down
14 changes: 8 additions & 6 deletions pkg/duffle/manifest/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package manifest

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
m := New()
expected := "&{manifest [] [] map[] map[] map[]}"
actual := fmt.Sprintf("%v", m)
if expected != actual {
t.Errorf("wanted %s, got %s", expected, actual)
}
// Testing to make sure maps are initialized
is := assert.New(t)
is.Len(m.Components, 0)
is.Len(m.Parameters, 0)
is.Len(m.Credentials, 0)

}

func TestGenerateName(t *testing.T) {
Expand Down

0 comments on commit d3aacd7

Please sign in to comment.