Skip to content

Commit 9c24aaf

Browse files
committed
stembuild: simplify version package
1 parent a510edc commit 9c24aaf

File tree

8 files changed

+49
-65
lines changed

8 files changed

+49
-65
lines changed

stembuild/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GOSRC = $(shell find . -name "*.go" ! -name "*test.go" ! -name "*fake*" ! -path "./integration/*")
22
STEMCELL_VERSION = $(shell echo "$${STEMBUILD_VERSION}")
3-
LD_FLAGS = "-w -s -X github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/version.Version=${STEMCELL_VERSION}"
3+
LD_FLAGS = "-w -s -X github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/version.Current=${STEMCELL_VERSION}"
44

55
ifeq ($(OS),Windows_NT)
66
COMMAND = out/stembuild.exe
@@ -31,7 +31,7 @@ out/stembuild.exe : assets $(GOSRC)
3131
GOOS=windows CGO_ENABLED=0 go build -o out/stembuild.exe -ldflags $(LD_FLAGS) .
3232

3333
clean :
34-
rm -rf version/version.go assets/StemcellAutomation.zip out/*
34+
rm -rf assets/StemcellAutomation.zip out/*
3535

3636
units : stubbed-stemcell-automation-zip
3737
go run github.com/onsi/ginkgo/v2/ginkgo run -r --randomize-all --randomize-suites --keep-going --skip-package integration,iaas_cli

stembuild/commandparser/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (h *stembuildHelp) Execute(c context.Context, f *flag.FlagSet, args ...inte
6363

6464
func (h *stembuildHelp) Explain(w io.Writer) {
6565

66-
fmt.Fprintf(w, "%s version %s, Windows Stemcell Building Tool\n\n", path.Base(os.Args[0]), version.Version) //nolint:errcheck
66+
fmt.Fprintf(w, "%s version %s, Windows Stemcell Building Tool\n\n", path.Base(os.Args[0]), version.Current) //nolint:errcheck
6767
fmt.Fprintf(w, "Usage: %s <global options> <command> <command flags>\n\n", path.Base(os.Args[0])) //nolint:errcheck
6868

6969
fmt.Fprint(w, "Commands:\n") //nolint:errcheck

stembuild/commandparser/help_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ var _ = Describe("help", func() {
2121
// struct variables. This adds a bit of protection when renaming flag parameters.
2222
Describe("Explain", func() {
2323
It("shows the correct version", func() {
24-
version.Version = "1.56"
24+
version.Current = "1.56"
2525
buf := bytes.Buffer{}
2626
fs := flag.NewFlagSet(path.Base(os.Args[0]), flag.ExitOnError)
2727
commands := make([]subcommands.Command, 0)
2828
sb := commandparser.NewStembuildHelp(subcommands.DefaultCommander, fs, &commands)
2929

3030
sb.Explain(&buf)
3131

32-
expectedString := fmt.Sprintf("%s version %s, Windows Stemcell Building Tool", path.Base(os.Args[0]), version.Version)
32+
expectedString := fmt.Sprintf("%s version %s, Windows Stemcell Building Tool", path.Base(os.Args[0]), version.Current)
3333
Expect(buf.String()).To(ContainSubstring(expectedString))
3434
})
3535
})

stembuild/construct/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (f *Factory) New(config config.SourceConfig, vCenterManager commandparser.V
4747
GuestManager: guestManager,
4848
Unarchiver: &archive.Zip{},
4949
}
50-
versionGetter := version.NewVersionGetter()
50+
versionGetter := version.New()
5151

5252
winRmClientFactory := remotemanager.NewWinRmClientFactory(config.GuestVmIp, config.GuestVMUsername, config.GuestVMPassword)
5353
remoteManager := remotemanager.NewWinRM(config.GuestVmIp, config.GuestVMUsername, config.GuestVMPassword, winRmClientFactory)

stembuild/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
}
3636

3737
var gf commandparser.GlobalFlags
38-
packageCmd := commandparser.NewPackageCommand(version.NewVersionGetter(), &packager.Factory{}, &commandparser.PackageMessenger{Output: os.Stderr})
38+
packageCmd := commandparser.NewPackageCommand(version.New(), &packager.Factory{}, &commandparser.PackageMessenger{Output: os.Stderr})
3939
packageCmd.GlobalFlags = &gf
4040
constructCmd := commandparser.NewConstructCmd(context.Background(), &construct.Factory{}, &vcenter_manager.ManagerFactory{}, &commandparser.ConstructValidator{}, &commandparser.ConstructCmdMessenger{OutputChannel: os.Stderr})
4141
constructCmd.GlobalFlags = &gf
@@ -65,7 +65,7 @@ func main() {
6565

6666
fs.Parse(os.Args[1:]) //nolint:errcheck
6767
if gf.ShowVersion {
68-
fmt.Fprintf(os.Stdout, "%s version %s, Windows Stemcell Building Tool\n\n", path.Base(os.Args[0]), version.Version) //nolint:errcheck
68+
fmt.Fprintf(os.Stdout, "%s version %s, Windows Stemcell Building Tool\n\n", path.Base(os.Args[0]), version.Current) //nolint:errcheck
6969
os.Remove(s) //nolint:errcheck
7070
os.Exit(0)
7171
}

stembuild/version/get_versions.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

stembuild/version/version.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
var Current = "dev"
9+
10+
func New() *Getter {
11+
return &Getter{
12+
Version: Current,
13+
}
14+
}
15+
16+
type Getter struct {
17+
Version string
18+
}
19+
20+
func (g *Getter) GetVersion() string {
21+
majorMinor := g.versionArray()[0:2]
22+
23+
return strings.Join(majorMinor, ".")
24+
}
25+
26+
func (g *Getter) GetVersionWithPatchNumber(patchNumber string) string {
27+
return fmt.Sprintf("%s.%s", g.GetVersion(), patchNumber)
28+
}
29+
30+
func (g *Getter) GetOs() string {
31+
osIdentifier := g.versionArray()[0]
32+
33+
return osIdentifier
34+
}
35+
36+
func (g *Getter) versionArray() []string {
37+
return strings.Split(g.Version, ".")
38+
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@ import (
77
. "github.com/onsi/gomega"
88
)
99

10-
type VModifier struct {
11-
newVersionNumber string
12-
}
13-
14-
func (m *VModifier) Modify(v *version.VersionGetter) {
15-
v.Version = m.newVersionNumber
16-
}
17-
1810
var _ = Describe("Version Utilities", func() {
1911
Describe("GetVersion", func() {
2012
It("should return a properly formatted version number", func() {
21-
versionGetter := version.NewVersionGetter(&VModifier{"2019.123.13"})
13+
versionGetter := version.Getter{Version: "2019.123.13"}
2214

2315
stemcellVersion := versionGetter.GetVersion()
2416
Expect(stemcellVersion).To(Equal("2019.123"))
@@ -27,7 +19,7 @@ var _ = Describe("Version Utilities", func() {
2719

2820
Describe("GetVersionWithPatchNumber", func() {
2921
It("returns a version number with a patch number when provided", func() {
30-
versionGetter := version.NewVersionGetter(&VModifier{"2019.5.13"})
22+
versionGetter := version.Getter{Version: "2019.5.13"}
3123

3224
stemcellVersion := versionGetter.GetVersionWithPatchNumber("2")
3325
Expect(stemcellVersion).To(Equal("2019.5.2"))
@@ -36,7 +28,7 @@ var _ = Describe("Version Utilities", func() {
3628

3729
Describe("GetOs", func() {
3830
It("should returns the first part of a '.' separated string", func() {
39-
versionGetter := version.NewVersionGetter(&VModifier{"111.222.333"})
31+
versionGetter := version.Getter{Version: "111.222.333"}
4032

4133
os := versionGetter.GetOs()
4234
Expect(os).To(Equal("111"))

0 commit comments

Comments
 (0)