Skip to content

Commit 6093358

Browse files
committed
Get build info from goreleaser
- Remove old version package - Add build info to cmd var - Pass build info to printer, which is used in the header - Use build info in version cmd
1 parent af600d2 commit 6093358

File tree

7 files changed

+37
-16
lines changed

7 files changed

+37
-16
lines changed

cmd/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func init() {
2020
}
2121

2222
func http(cmd *cobra.Command, args []string) {
23-
renderer := &renderer.Printer{Port: Port, Addr: Address}
23+
renderer := &renderer.Printer{Port: Port, Addr: Address, BuildInfo: BuildInfo}
2424
httpServer := server.Http{Addr: Address, Port: Port, ResponseCode: ResponseCode, Output: renderer}
2525
httpServer.Start()
2626
}

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var (
77
Address string
88
Port int
99
ResponseCode int
10+
BuildInfo map[string]string
1011
)
1112

1213
var rootCmd = &cobra.Command{
@@ -16,7 +17,8 @@ var rootCmd = &cobra.Command{
1617
This CLI tool will let you create a temporary API endpoint for testing purposes.`,
1718
}
1819

19-
func Execute() error {
20+
func Execute(buildInfo map[string]string) error {
21+
BuildInfo = buildInfo
2022
return rootCmd.Execute()
2123
}
2224

cmd/version.go

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

33
import (
44
"fmt"
5+
"time"
56

6-
"github.com/aaronvb/request_hole/pkg/version"
77
"github.com/spf13/cobra"
88
)
99

@@ -15,6 +15,15 @@ var versionCmd = &cobra.Command{
1515
Use: "version",
1616
Short: "Print version number of Request Hole",
1717
Run: func(cmd *cobra.Command, args []string) {
18-
fmt.Printf("Request Hole %s\n%s\n", version.BuildVersion, version.BuildRepo)
18+
var date string
19+
20+
buildDate, err := time.Parse(time.RFC3339, BuildInfo["date"])
21+
if err != nil {
22+
date = BuildInfo["date"]
23+
} else {
24+
date = buildDate.Format("2006-01-02 15:04:05")
25+
}
26+
27+
fmt.Printf("Request Hole %s\n%s\n\nBuild date: %s\nCommit: %s\nBuild by: %s", BuildInfo["version"], BuildInfo["repo"], date, BuildInfo["commit"], BuildInfo["builtBy"])
1928
},
2029
}

main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ package main
22

33
import "github.com/aaronvb/request_hole/cmd"
44

5+
var (
6+
version = "dev"
7+
commit = "none"
8+
date = "unknown"
9+
repo = "github.com/aaronvb/request_hole"
10+
builtBy = "dev"
11+
)
12+
513
func main() {
6-
cmd.Execute()
14+
buildInfo := map[string]string{
15+
"version": version,
16+
"commit": commit,
17+
"date": date,
18+
"repo": repo,
19+
"builtBy": builtBy,
20+
}
21+
cmd.Execute(buildInfo)
722
}

pkg/renderer/printer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77

88
"github.com/aaronvb/logrequest"
9-
"github.com/aaronvb/request_hole/pkg/version"
109
"github.com/pterm/pterm"
1110
)
1211

@@ -19,6 +18,9 @@ type Printer struct {
1918
// Fields for startText
2019
Port int
2120
Addr string
21+
22+
// Contains build info
23+
BuildInfo map[string]string
2224
}
2325

2426
// Start renders the initial header and the spinner. The Spinner should be consistent during
@@ -41,7 +43,7 @@ func (p *Printer) startText() string {
4143
Sprintf("Request Hole")
4244
version := pterm.DefaultBasicText.
4345
WithStyle(pterm.NewStyle(pterm.Fuzzy)).
44-
Sprintf(version.BuildVersion)
46+
Sprintf(p.BuildInfo["version"])
4547

4648
text := fmt.Sprintf("%s %s\nListening on http://%s:%d", primary, version, p.Addr, p.Port)
4749
return text

pkg/renderer/printer_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
"testing"
66

77
"github.com/aaronvb/logrequest"
8-
"github.com/aaronvb/request_hole/pkg/version"
98
"github.com/pterm/pterm"
109
)
1110

1211
func TestStartText(t *testing.T) {
1312
pterm.DisableColor()
14-
printer := Printer{Addr: "localhost", Port: 8080}
13+
printer := Printer{Addr: "localhost", Port: 8080, BuildInfo: map[string]string{"version": "dev"}}
1514
result := printer.startText()
16-
expected := fmt.Sprintf("Request Hole %s\nListening on http://%s:%d", version.BuildVersion, printer.Addr, printer.Port)
15+
expected := fmt.Sprintf("Request Hole %s\nListening on http://%s:%d", "dev", printer.Addr, printer.Port)
1716

1817
if result != expected {
1918
t.Errorf("Expected %s, got %s", expected, result)

pkg/version/version.go

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

0 commit comments

Comments
 (0)