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/.goreleaser.yaml b/.goreleaser.yaml index cf08383..0438710 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -13,6 +13,8 @@ builds: - linux - windows - darwin + ldflags: + - -s -w -X github.com/laureanray/clibgen/cmd.version={{.Version}} checksum: name_template: 'checksums.txt' snapshot: 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() 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 692070e..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 - -Flags: - -h, --help help for clibgen - -t, --toggle Help message for toggle - -Use "clibgen [command] --help" for more information about a command.