From 18028c096cfb2f91427fe9c277fd4bf818b3ec1b Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 04:07:35 +0800 Subject: [PATCH 1/6] add version subcmd --- cmd/root.go | 14 +++++++++++++- main.go | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index ce495a2..7244949 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,11 +1,14 @@ package cmd import ( + "fmt" "os" "github.com/spf13/cobra" ) +var version string + var rootCmd = &cobra.Command{ Use: "clibgen", 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 Useful if you are lazy to open up a browser to download e-books/resources.`, } +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of Hugo", + Long: `All software has versions. This is Hugo's`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(version) + }, +} + func Execute() { err := rootCmd.Execute() if err != nil { @@ -22,5 +34,5 @@ func Execute() { } func init() { - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + rootCmd.AddCommand(versionCmd) } diff --git a/main.go b/main.go index 3f71bf5..a98920f 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main -import "github.com/laureanray/clibgen/cmd" +import ( + "github.com/laureanray/clibgen/cmd" +) func main() { cmd.Execute() From fd3f29dcd6ededd22871a56342d2892eac4a23ef Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 04:09:33 +0800 Subject: [PATCH 2/6] update gorelaser --- .goreleaser.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index cf08383..a590d80 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -13,6 +13,8 @@ builds: - linux - windows - darwin + ldflags: + - -X github.com/laureanray/clibgen/cmd.version=`git tag --sort=-version:refname | head -n ` checksum: name_template: 'checksums.txt' snapshot: From eab7803d01653b01db881331cb3f715ac923767d Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 04:15:08 +0800 Subject: [PATCH 3/6] update ldflags --- .goreleaser.yaml | 2 +- dist/config.yaml | 126 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 dist/config.yaml diff --git a/.goreleaser.yaml b/.goreleaser.yaml index a590d80..0438710 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -14,7 +14,7 @@ builds: - windows - darwin ldflags: - - -X github.com/laureanray/clibgen/cmd.version=`git tag --sort=-version:refname | head -n ` + - -s -w -X github.com/laureanray/clibgen/cmd.version={{.Version}} checksum: name_template: 'checksums.txt' snapshot: diff --git a/dist/config.yaml b/dist/config.yaml new file mode 100644 index 0000000..ada0366 --- /dev/null +++ b/dist/config.yaml @@ -0,0 +1,126 @@ +project_name: clibgen +release: + github: + owner: laureanray + name: clibgen + name_template: '{{.Tag}}' +builds: + - id: clibgen + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 + - "386" + goarm: + - "6" + gomips: + - hardfloat + goamd64: + - v1 + targets: + - linux_amd64_v1 + - linux_arm64 + - linux_386 + - windows_amd64_v1 + - windows_arm64 + - windows_386 + - darwin_amd64_v1 + - darwin_arm64 + dir: . + main: . + binary: clibgen + builder: go + gobinary: go + command: build + ldflags: + - -X github.com/laureanray/clibgen/cmd.version=`git tag --sort=-version:refname | head -n ` + env: + - CGO_ENABLED=0 +archives: + - id: default + name_template: '{{.ProjectName}}_{{.Os}}-{{.Arch}}' + format: tar.gz + format_overrides: + - goos: windows + format: zip + files: + - src: license* + - src: LICENSE* + - src: readme* + - src: README* + - src: changelog* + - src: CHANGELOG* +snapshot: + name_template: clibgen +checksum: + name_template: checksums.txt + algorithm: sha256 +changelog: + filters: + exclude: + - '^docs:' + - '^test:' + sort: asc +dist: dist +env_files: + github_token: ~/.config/goreleaser/github_token + gitlab_token: ~/.config/goreleaser/gitlab_token + gitea_token: ~/.config/goreleaser/gitea_token +before: + hooks: + - go mod tidy + - go generate ./... +source: + name_template: '{{ .ProjectName }}-{{ .Version }}' + format: tar.gz +gomod: + gobinary: go +announce: + twitter: + message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' + mastodon: + message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' + server: "" + reddit: + title_template: '{{ .ProjectName }} {{ .Tag }} is out!' + url_template: '{{ .ReleaseURL }}' + slack: + message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' + username: GoReleaser + discord: + message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' + author: GoReleaser + color: "3888754" + icon_url: https://goreleaser.com/static/avatar.png + teams: + title_template: '{{ .ProjectName }} {{ .Tag }} is out!' + message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' + color: '#2D313E' + icon_url: https://goreleaser.com/static/avatar.png + smtp: + subject_template: '{{ .ProjectName }} {{ .Tag }} is out!' + body_template: 'You can view details from: {{ .ReleaseURL }}' + mattermost: + message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' + title_template: '{{ .ProjectName }} {{ .Tag }} is out!' + username: GoReleaser + linkedin: + message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' + telegram: + message_template: '{{ .ProjectName }} {{ mdv2escape .Tag }} is out! Check it out at {{ mdv2escape .ReleaseURL }}' + parse_mode: MarkdownV2 + webhook: + message_template: '{ "message": "{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}"}' + content_type: application/json; charset=utf-8 + opencollective: + title_template: '{{ .Tag }}' + message_template: '{{ .ProjectName }} {{ .Tag }} is out!
Check it out at {{ .ReleaseURL }}' +git: + tag_sort: -version:refname +github_urls: + download: https://github.com +gitlab_urls: + download: https://gitlab.com From 4b962d75e01201ea2733914604632dbf8d896a3f Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 04:16:07 +0800 Subject: [PATCH 4/6] ignore dist --- .gitignore | 3 +- dist/config.yaml | 126 ----------------------------------------------- 2 files changed, 2 insertions(+), 127 deletions(-) delete mode 100644 dist/config.yaml diff --git a/.gitignore b/.gitignore index 723ef36..dc621d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +dist diff --git a/dist/config.yaml b/dist/config.yaml deleted file mode 100644 index ada0366..0000000 --- a/dist/config.yaml +++ /dev/null @@ -1,126 +0,0 @@ -project_name: clibgen -release: - github: - owner: laureanray - name: clibgen - name_template: '{{.Tag}}' -builds: - - id: clibgen - goos: - - linux - - windows - - darwin - goarch: - - amd64 - - arm64 - - "386" - goarm: - - "6" - gomips: - - hardfloat - goamd64: - - v1 - targets: - - linux_amd64_v1 - - linux_arm64 - - linux_386 - - windows_amd64_v1 - - windows_arm64 - - windows_386 - - darwin_amd64_v1 - - darwin_arm64 - dir: . - main: . - binary: clibgen - builder: go - gobinary: go - command: build - ldflags: - - -X github.com/laureanray/clibgen/cmd.version=`git tag --sort=-version:refname | head -n ` - env: - - CGO_ENABLED=0 -archives: - - id: default - name_template: '{{.ProjectName}}_{{.Os}}-{{.Arch}}' - format: tar.gz - format_overrides: - - goos: windows - format: zip - files: - - src: license* - - src: LICENSE* - - src: readme* - - src: README* - - src: changelog* - - src: CHANGELOG* -snapshot: - name_template: clibgen -checksum: - name_template: checksums.txt - algorithm: sha256 -changelog: - filters: - exclude: - - '^docs:' - - '^test:' - sort: asc -dist: dist -env_files: - github_token: ~/.config/goreleaser/github_token - gitlab_token: ~/.config/goreleaser/gitlab_token - gitea_token: ~/.config/goreleaser/gitea_token -before: - hooks: - - go mod tidy - - go generate ./... -source: - name_template: '{{ .ProjectName }}-{{ .Version }}' - format: tar.gz -gomod: - gobinary: go -announce: - twitter: - message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' - mastodon: - message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' - server: "" - reddit: - title_template: '{{ .ProjectName }} {{ .Tag }} is out!' - url_template: '{{ .ReleaseURL }}' - slack: - message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' - username: GoReleaser - discord: - message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' - author: GoReleaser - color: "3888754" - icon_url: https://goreleaser.com/static/avatar.png - teams: - title_template: '{{ .ProjectName }} {{ .Tag }} is out!' - message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' - color: '#2D313E' - icon_url: https://goreleaser.com/static/avatar.png - smtp: - subject_template: '{{ .ProjectName }} {{ .Tag }} is out!' - body_template: 'You can view details from: {{ .ReleaseURL }}' - mattermost: - message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' - title_template: '{{ .ProjectName }} {{ .Tag }} is out!' - username: GoReleaser - linkedin: - message_template: '{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}' - telegram: - message_template: '{{ .ProjectName }} {{ mdv2escape .Tag }} is out! Check it out at {{ mdv2escape .ReleaseURL }}' - parse_mode: MarkdownV2 - webhook: - message_template: '{ "message": "{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}"}' - content_type: application/json; charset=utf-8 - opencollective: - title_template: '{{ .Tag }}' - message_template: '{{ .ProjectName }} {{ .Tag }} is out!
Check it out at {{ .ReleaseURL }}' -git: - tag_sort: -version:refname -github_urls: - download: https://github.com -gitlab_urls: - download: https://gitlab.com From 08eff5ed75e79eab475cf64872552fc88a9855f3 Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 04:22:18 +0800 Subject: [PATCH 5/6] fix test fixture --- testdata/help.golden | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testdata/help.golden b/testdata/help.golden index 692070e..74b8286 100644 --- a/testdata/help.golden +++ b/testdata/help.golden @@ -1,5 +1,5 @@ -Clibgen is a CLI application to search and download epubs, pdfs, from library genesis. +Clibgen is a CLI application to search and download epubs, pdfs, from library genesis. Useful if you are lazy to open up a browser to download e-books/resources. Usage: @@ -9,9 +9,9 @@ Available Commands: completion Generate the autocompletion script for the specified shell help Help about any command search search for a book, paper or article + version Print the version number of Hugo Flags: - -h, --help help for clibgen - -t, --toggle Help message for toggle + -h, --help help for clibgen Use "clibgen [command] --help" for more information about a command. From 972c71c71018f8ec6ac61be2bd10c8b2982a491c Mon Sep 17 00:00:00 2001 From: laureanray Date: Thu, 17 Aug 2023 04:25:19 +0800 Subject: [PATCH 6/6] remove useless test case --- test/integration_test.go | 80 ---------------------------------------- testdata/help.golden | 17 --------- 2 files changed, 97 deletions(-) delete mode 100644 testdata/help.golden diff --git a/test/integration_test.go b/test/integration_test.go index 735c540..dfa1829 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -7,16 +7,11 @@ import ( "os" "os/exec" "path/filepath" - "reflect" "strings" "testing" "time" ) -var ( - update = flag.Bool("update", false, "update .golden files") -) - var binaryName = "clibgen" var binaryPath = "" @@ -42,12 +37,6 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -func runBinary(args []string) ([]byte, error) { - cmd := exec.Command(binaryPath, args...) - cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata") - return cmd.CombinedOutput() -} - func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error) { fmt.Println("Running binary with file input", bytesToWrite) fmt.Printf("Executing command: %s", binaryPath) @@ -99,38 +88,6 @@ func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error) return out, err } -func TestStaticCliArgs(t *testing.T) { - tests := []struct { - name string - args []string - fixture string - }{ - {"help args", []string{"--help"}, "help.golden"}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - output, err := runBinary(tt.args) - - if err != nil { - t.Fatal(err) - } - - if *update { - writeFixture(t, tt.fixture, output) - } - - actual := string(output) - - expected := loadFixture(t, tt.fixture) - - if !reflect.DeepEqual(actual, expected) { - t.Fatalf("actual = %s, expected = %s", actual, expected) - } - }) - } -} - func TestSearch(t *testing.T) { tests := []struct { name string @@ -158,40 +115,3 @@ func TestSearch(t *testing.T) { }) } } - -func removeLastLine(str string, num int) string { - lines := strings.Split(str, "\n") - - if len(lines) > 0 { - lines = lines[:len(lines)-num] - } - - return strings.Join(lines, "\n") -} - -func writeFixture(t *testing.T, goldenFile string, actual []byte) { - t.Helper() - goldenPath := "testdata/" + goldenFile - - f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) - defer f.Close() - - _, err = f.WriteString(string(actual)) - - if err != nil { - t.Fatalf("Error writing to file %s: %s", goldenPath, err) - } -} - -func loadFixture(t *testing.T, goldenFile string) string { - goldenPath := "testdata/" + goldenFile - - f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) - - content, err := ioutil.ReadAll(f) - if err != nil { - t.Fatalf("Error opening file %s: %s", goldenPath, err) - } - - return string(content) -} diff --git a/testdata/help.golden b/testdata/help.golden deleted file mode 100644 index 74b8286..0000000 --- a/testdata/help.golden +++ /dev/null @@ -1,17 +0,0 @@ - -Clibgen is a CLI application to search and download epubs, pdfs, from library genesis. -Useful if you are lazy to open up a browser to download e-books/resources. - -Usage: - clibgen [command] - -Available Commands: - completion Generate the autocompletion script for the specified shell - help Help about any command - search search for a book, paper or article - version Print the version number of Hugo - -Flags: - -h, --help help for clibgen - -Use "clibgen [command] --help" for more information about a command.