Skip to content

Commit bb05b98

Browse files
author
Tobias Meinhardt
committed
move postrun logic & semver logic to util package
1 parent ea25054 commit bb05b98

File tree

3 files changed

+113
-93
lines changed

3 files changed

+113
-93
lines changed

pkg/cli/cmd/release.go

Lines changed: 13 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package cmd
22

33
import (
4-
"bytes"
5-
"fmt"
6-
"log"
7-
"os/exec"
8-
"path/filepath"
9-
"strings"
10-
114
"github.com/meinto/glow"
125
"github.com/meinto/glow/pkg/cli/cmd/util"
13-
"github.com/meinto/glow/semver"
146
"github.com/spf13/cobra"
157
)
168

@@ -37,25 +29,14 @@ var releaseCmd = &cobra.Command{
3729
Short: "create a release branch",
3830
Args: cobra.MinimumNArgs(1),
3931
Run: func(cmd *cobra.Command, args []string) {
40-
version := args[0]
41-
4232
g, err := util.GetGitClient()
4333
util.CheckForError(err, "GetGitClient")
4434

45-
var s semver.Service
46-
if util.IsSemanticVersion(args[0]) {
47-
pathToRepo, err := g.GitRepoPath()
48-
util.CheckForError(err, "semver GitRepoPath")
49-
s = semver.NewSemverService(
50-
pathToRepo,
51-
"/bin/bash",
52-
releaseCmdOptions.VersionFile,
53-
releaseCmdOptions.VersionFileType,
54-
)
55-
v, err := s.GetNextVersion(args[0])
56-
util.CheckForError(err, "semver GetNextVersion")
57-
version = v
58-
}
35+
version, s := util.ProcessVersion(
36+
args[0],
37+
releaseCmdOptions.VersionFile,
38+
releaseCmdOptions.VersionFileType,
39+
)
5940

6041
release, err := glow.NewRelease(version)
6142
util.CheckForError(err, "NewRelease")
@@ -74,74 +55,13 @@ var releaseCmd = &cobra.Command{
7455
PostRun: func(cmd *cobra.Command, args []string) {
7556
version := args[0]
7657

77-
g, err := util.GetGitClient()
78-
util.CheckForError(err, "GetGitClient")
79-
80-
if util.IsSemanticVersion(args[0]) {
81-
pathToRepo, err := g.GitRepoPath()
82-
util.CheckForError(err, "semver GitRepoPath")
83-
s := semver.NewSemverService(
84-
pathToRepo,
85-
"/bin/bash",
86-
releaseCmdOptions.VersionFile,
87-
releaseCmdOptions.VersionFileType,
88-
)
89-
v, err := s.GetCurrentVersion()
90-
util.CheckForError(err, "semver GetNextVersion")
91-
version = v
92-
}
93-
94-
if releaseCmdOptions.PostReleaseScript != "" {
95-
postRelease(version)
96-
}
97-
if len(releaseCmdOptions.PostReleaseCommand) > 0 {
98-
for _, command := range releaseCmdOptions.PostReleaseCommand {
99-
execute(version, command)
100-
}
101-
}
102-
103-
if releaseCmdOptions.Push {
104-
err = g.AddAll()
105-
util.CheckForError(err, "AddAll")
106-
107-
err = g.Commit("[glow] Add post release changes")
108-
util.CheckForError(err, "Commit")
109-
110-
err = g.Push(true)
111-
util.CheckForError(err, "Push")
112-
}
58+
util.PostRunWithVersion(
59+
version,
60+
releaseCmdOptions.VersionFile,
61+
releaseCmdOptions.VersionFileType,
62+
releaseCmdOptions.PostReleaseScript,
63+
releaseCmdOptions.PostReleaseCommand,
64+
releaseCmdOptions.Push,
65+
)
11366
},
11467
}
115-
116-
func postRelease(version string) {
117-
pathToFile, err := filepath.Abs(releaseCmdOptions.PostReleaseScript)
118-
if err != nil {
119-
log.Println("cannot find post-release script", err)
120-
}
121-
cmd := exec.Command(pathToFile, version)
122-
var out bytes.Buffer
123-
cmd.Stdout = &out
124-
err = cmd.Run()
125-
if err != nil {
126-
log.Println("error while executing post-release script", err)
127-
}
128-
log.Println("post release:")
129-
log.Println(out.String())
130-
}
131-
132-
func execute(version, command string) {
133-
cmdString := string(command)
134-
if strings.Contains(command, "%s") {
135-
cmdString = fmt.Sprintf(command, version)
136-
}
137-
cmd := exec.Command("/bin/bash", "-c", cmdString)
138-
var stdout, stderr bytes.Buffer
139-
cmd.Stderr = &stderr
140-
cmd.Stdout = &stdout
141-
err := cmd.Run()
142-
if err != nil {
143-
log.Println("error while executing post-release script", err.Error(), stderr.String())
144-
}
145-
log.Println("post release:")
146-
log.Println(stdout.String())
147-
}

pkg/cli/cmd/util/postrun.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package util
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"log"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
10+
)
11+
12+
func PostRunWithVersion(
13+
versionArg, versionFile, versionFileType, postReleaseScript string,
14+
postReleaseCommand []string,
15+
push bool,
16+
) {
17+
g, err := GetGitClient()
18+
CheckForError(err, "GetGitClient")
19+
20+
version, _ := ProcessVersion(versionArg, versionFile, versionFileType)
21+
22+
if postReleaseScript != "" {
23+
postRelease(version, postReleaseScript)
24+
}
25+
if len(postReleaseCommand) > 0 {
26+
for _, command := range postReleaseCommand {
27+
execute(version, command)
28+
}
29+
}
30+
31+
if push {
32+
err = g.AddAll()
33+
CheckForError(err, "AddAll")
34+
35+
err = g.Commit("[glow] Add post release changes")
36+
CheckForError(err, "Commit")
37+
38+
err = g.Push(true)
39+
CheckForError(err, "Push")
40+
}
41+
}
42+
43+
func postRelease(version, script string) {
44+
pathToFile, err := filepath.Abs(script)
45+
if err != nil {
46+
log.Println("cannot find post-release script", err)
47+
}
48+
cmd := exec.Command(pathToFile, version)
49+
var out bytes.Buffer
50+
cmd.Stdout = &out
51+
err = cmd.Run()
52+
if err != nil {
53+
log.Println("error while executing post-release script", err)
54+
}
55+
log.Println("post release:")
56+
log.Println(out.String())
57+
}
58+
59+
func execute(version, command string) {
60+
cmdString := string(command)
61+
if strings.Contains(command, "%s") {
62+
cmdString = fmt.Sprintf(command, version)
63+
}
64+
cmd := exec.Command("/bin/bash", "-c", cmdString)
65+
var stdout, stderr bytes.Buffer
66+
cmd.Stderr = &stderr
67+
cmd.Stdout = &stdout
68+
err := cmd.Run()
69+
if err != nil {
70+
log.Println("error while executing post-release script", err.Error(), stderr.String())
71+
}
72+
log.Println("post release:")
73+
log.Println(stdout.String())
74+
}

pkg/cli/cmd/util/semver.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,34 @@ import (
44
"os"
55

66
"github.com/spf13/viper"
7+
8+
"github.com/meinto/glow/semver"
79
)
810

11+
func ProcessVersion(versionArg, versionFile, versionFileType string) (string, semver.Service) {
12+
version := versionArg
13+
14+
g, err := GetGitClient()
15+
CheckForError(err, "GetGitClient")
16+
17+
var s semver.Service
18+
if IsSemanticVersion(version) {
19+
pathToRepo, err := g.GitRepoPath()
20+
CheckForError(err, "semver GitRepoPath")
21+
s = semver.NewSemverService(
22+
pathToRepo,
23+
"/bin/bash",
24+
versionFile,
25+
versionFileType,
26+
)
27+
v, err := s.GetCurrentVersion()
28+
CheckForError(err, "semver GetNextVersion")
29+
version = v
30+
}
31+
32+
return version, s
33+
}
34+
935
func HasSemverConfig() bool {
1036
if _, err := os.Stat(viper.GetString("gitPath") + "/semver.config.json"); os.IsNotExist(err) {
1137
return false

0 commit comments

Comments
 (0)