Skip to content

Commit 12a0572

Browse files
authored
Feature/setup versioning (#35)
2 parents 52389ac + 2deca15 commit 12a0572

File tree

6 files changed

+20
-100
lines changed

6 files changed

+20
-100
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.idea
1+
.idea
2+
dist

.goreleaser.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ builds:
1313
- linux
1414
- windows
1515
- darwin
16+
ldflags:
17+
- -s -w -X github.com/laureanray/clibgen/cmd.version={{.Version}}
1618
checksum:
1719
name_template: 'checksums.txt'
1820
snapshot:

cmd/root.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
56

67
"github.com/spf13/cobra"
78
)
89

10+
var version string
11+
912
var rootCmd = &cobra.Command{
1013
Use: "clibgen",
1114
Short: "Library Genesis command line / terminal client",
@@ -14,6 +17,15 @@ Clibgen is a CLI application to search and download epubs, pdfs, from library ge
1417
Useful if you are lazy to open up a browser to download e-books/resources.`,
1518
}
1619

20+
var versionCmd = &cobra.Command{
21+
Use: "version",
22+
Short: "Print the version number of Hugo",
23+
Long: `All software has versions. This is Hugo's`,
24+
Run: func(cmd *cobra.Command, args []string) {
25+
fmt.Println(version)
26+
},
27+
}
28+
1729
func Execute() {
1830
err := rootCmd.Execute()
1931
if err != nil {
@@ -22,5 +34,5 @@ func Execute() {
2234
}
2335

2436
func init() {
25-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
37+
rootCmd.AddCommand(versionCmd)
2638
}

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

3-
import "github.com/laureanray/clibgen/cmd"
3+
import (
4+
"github.com/laureanray/clibgen/cmd"
5+
)
46

57
func main() {
68
cmd.Execute()

test/integration_test.go

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10-
"reflect"
1110
"strings"
1211
"testing"
1312
"time"
1413
)
1514

16-
var (
17-
update = flag.Bool("update", false, "update .golden files")
18-
)
19-
2015
var binaryName = "clibgen"
2116
var binaryPath = ""
2217

@@ -42,12 +37,6 @@ func TestMain(m *testing.M) {
4237
os.Exit(m.Run())
4338
}
4439

45-
func runBinary(args []string) ([]byte, error) {
46-
cmd := exec.Command(binaryPath, args...)
47-
cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata")
48-
return cmd.CombinedOutput()
49-
}
50-
5140
func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error) {
5241
fmt.Println("Running binary with file input", bytesToWrite)
5342
fmt.Printf("Executing command: %s", binaryPath)
@@ -99,38 +88,6 @@ func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error)
9988
return out, err
10089
}
10190

102-
func TestStaticCliArgs(t *testing.T) {
103-
tests := []struct {
104-
name string
105-
args []string
106-
fixture string
107-
}{
108-
{"help args", []string{"--help"}, "help.golden"},
109-
}
110-
111-
for _, tt := range tests {
112-
t.Run(tt.name, func(t *testing.T) {
113-
output, err := runBinary(tt.args)
114-
115-
if err != nil {
116-
t.Fatal(err)
117-
}
118-
119-
if *update {
120-
writeFixture(t, tt.fixture, output)
121-
}
122-
123-
actual := string(output)
124-
125-
expected := loadFixture(t, tt.fixture)
126-
127-
if !reflect.DeepEqual(actual, expected) {
128-
t.Fatalf("actual = %s, expected = %s", actual, expected)
129-
}
130-
})
131-
}
132-
}
133-
13491
func TestSearch(t *testing.T) {
13592
tests := []struct {
13693
name string
@@ -158,40 +115,3 @@ func TestSearch(t *testing.T) {
158115
})
159116
}
160117
}
161-
162-
func removeLastLine(str string, num int) string {
163-
lines := strings.Split(str, "\n")
164-
165-
if len(lines) > 0 {
166-
lines = lines[:len(lines)-num]
167-
}
168-
169-
return strings.Join(lines, "\n")
170-
}
171-
172-
func writeFixture(t *testing.T, goldenFile string, actual []byte) {
173-
t.Helper()
174-
goldenPath := "testdata/" + goldenFile
175-
176-
f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644)
177-
defer f.Close()
178-
179-
_, err = f.WriteString(string(actual))
180-
181-
if err != nil {
182-
t.Fatalf("Error writing to file %s: %s", goldenPath, err)
183-
}
184-
}
185-
186-
func loadFixture(t *testing.T, goldenFile string) string {
187-
goldenPath := "testdata/" + goldenFile
188-
189-
f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644)
190-
191-
content, err := ioutil.ReadAll(f)
192-
if err != nil {
193-
t.Fatalf("Error opening file %s: %s", goldenPath, err)
194-
}
195-
196-
return string(content)
197-
}

testdata/help.golden

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

0 commit comments

Comments
 (0)