Skip to content

Commit

Permalink
feat: add version pkg for Vela (#112)
Browse files Browse the repository at this point in the history
* feat: add version type for Vela

* feat: add metadata struct

* refactor: change full to canonical

* feat: add tests for version pkg
  • Loading branch information
jbrockopp committed Oct 19, 2020
1 parent 6a11d23 commit 226d0cc
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
10 changes: 10 additions & 0 deletions version/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package version provides the defined version types for Vela.
//
// Usage:
//
// import "github.com/go-vela/types/version"
package version
45 changes: 45 additions & 0 deletions version/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package version

import "fmt"

const metaFormat = `{
Architecture: %s,
BuildDate: %s,
Compiler: %s,
GitCommit: %s,
GoVersion: %s,
OperatingSystem: %s,
}`

// Metadata represents extra information surrounding the application version.
type Metadata struct {
// Architecture represents the architecture information for the application.
Architecture string `json:"architecture,omitempty"`
// BuildDate represents the build date information for the application.
BuildDate string `json:"build_date,omitempty"`
// Compiler represents the compiler information for the application.
Compiler string `json:"compiler,omitempty"`
// GitCommit represents the git commit information for the application.
GitCommit string `json:"git_commit,omitempty"`
// GoVersion represents the golang version information for the application.
GoVersion string `json:"go_version,omitempty"`
// OperatingSystem represents the operating system information for the application.
OperatingSystem string `json:"operating_system,omitempty"`
}

// String implements the Stringer interface for the Metadata type.
func (m *Metadata) String() string {
return fmt.Sprintf(
metaFormat,
m.Architecture,
m.BuildDate,
m.Compiler,
m.GitCommit,
m.GoVersion,
m.OperatingSystem,
)
}
40 changes: 40 additions & 0 deletions version/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package version

import (
"fmt"
"reflect"
"testing"
)

func TestVersion_Metadata_String(t *testing.T) {
// setup types
m := &Metadata{
Architecture: "amd64",
BuildDate: "1970-1-1T00:00:00Z",
Compiler: "gc",
GitCommit: "abcdef123456789",
GoVersion: "1.15.0",
OperatingSystem: "llinux",
}

want := fmt.Sprintf(
metaFormat,
m.Architecture,
m.BuildDate,
m.Compiler,
m.GitCommit,
m.GoVersion,
m.OperatingSystem,
)

// run test
got := m.String()

if !reflect.DeepEqual(got, want) {
t.Errorf("String is %v, want %v", got, want)
}
}
71 changes: 71 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package version

import (
"fmt"
)

const versionFormat = `{
Canonical: %s,
Major: %d,
Minor: %d,
Patch: %d,
PreRelease: %s,
Metadata: {
Architecture: %s,
BuildDate: %s,
Compiler: %s,
GitCommit: %s,
GoVersion: %s,
OperatingSystem: %s,
}
}`

// Version represents application information that
// follows semantic version guidelines from
// https://semver.org/.
type Version struct {
// Canonical represents a canonical semantic version for the application.
Canonical string `json:"canonical"`
// Major represents incompatible API changes.
Major int64 `json:"major"`
// Minor represents added functionality in a backwards compatible manner.
Minor int64 `json:"minor"`
// Patch represents backwards compatible bug fixes.
Patch int64 `json:"patch"`
// PreRelease represents unstable changes that might not be compatible.
PreRelease string `json:"pre_release,omitempty"`
// Metadata represents extra information surrounding the application version.
Metadata Metadata `json:"metadata,omitempty"`
}

// Meta implements a formatted string containing only metadata for the Version type.
func (v *Version) Meta() string {
return v.Metadata.String()
}

// Semantic implements a formatted string containing a formal semantic version for the Version type.
func (v *Version) Semantic() string {
return v.Canonical
}

// String implements the Stringer interface for the Version type.
func (v *Version) String() string {
return fmt.Sprintf(
versionFormat,
v.Canonical,
v.Major,
v.Minor,
v.Patch,
v.PreRelease,
v.Metadata.Architecture,
v.Metadata.BuildDate,
v.Metadata.Compiler,
v.Metadata.GitCommit,
v.Metadata.GoVersion,
v.Metadata.OperatingSystem,
)
}
108 changes: 108 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package version

import (
"fmt"
"reflect"
"testing"
)

func TestVersion_Version_Meta(t *testing.T) {
// setup types
v := &Version{
Canonical: "v1.2.3",
Major: 1,
Minor: 2,
Patch: 3,
PreRelease: "",
Metadata: Metadata{
Architecture: "amd64",
BuildDate: "1970-1-1T00:00:00Z",
Compiler: "gc",
GitCommit: "abcdef123456789",
GoVersion: "1.15.0",
OperatingSystem: "llinux",
},
}

want := v.Metadata.String()

// run test
got := v.Meta()

if !reflect.DeepEqual(got, want) {
t.Errorf("String is %v, want %v", got, want)
}
}

func TestVersion_Version_Semantic(t *testing.T) {
// setup types
v := &Version{
Canonical: "v1.2.3",
Major: 1,
Minor: 2,
Patch: 3,
PreRelease: "",
Metadata: Metadata{
Architecture: "amd64",
BuildDate: "1970-1-1T00:00:00Z",
Compiler: "gc",
GitCommit: "abcdef123456789",
GoVersion: "1.15.0",
OperatingSystem: "llinux",
},
}

want := v.Canonical

// run test
got := v.Semantic()

if !reflect.DeepEqual(got, want) {
t.Errorf("String is %v, want %v", got, want)
}
}

func TestVersion_Version_String(t *testing.T) {
// setup types
v := &Version{
Canonical: "v1.2.3",
Major: 1,
Minor: 2,
Patch: 3,
PreRelease: "",
Metadata: Metadata{
Architecture: "amd64",
BuildDate: "1970-1-1T00:00:00Z",
Compiler: "gc",
GitCommit: "abcdef123456789",
GoVersion: "1.15.0",
OperatingSystem: "llinux",
},
}

want := fmt.Sprintf(
versionFormat,
v.Canonical,
v.Major,
v.Minor,
v.Patch,
v.PreRelease,
v.Metadata.Architecture,
v.Metadata.BuildDate,
v.Metadata.Compiler,
v.Metadata.GitCommit,
v.Metadata.GoVersion,
v.Metadata.OperatingSystem,
)

// run test
got := v.String()

if !reflect.DeepEqual(got, want) {
t.Errorf("String is %v, want %v", got, want)
}
}

0 comments on commit 226d0cc

Please sign in to comment.