diff --git a/Makefile b/Makefile index 4cd400e..c3a3845 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,4 @@ $(BINARY): $(SOURCES) all: $(BINARY) test test: - ./$(BINARY) -url "https://images.pexels.com/photos/2558605/pexels-photo-2558605.jpeg?cs=srgb&dl=pexels-anel-rossouw-2558605.jpg&fm=jpg" -height 25 + ./$(BINARY) -u "https://images.pexels.com/photos/2558605/pexels-photo-2558605.jpeg?cs=srgb&dl=pexels-anel-rossouw-2558605.jpg&fm=jpg" -r 25 diff --git a/README.md b/README.md index fc9ea52..469f02d 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ go build ./cmd/imgcat ## Usage ``` -./imgcat -url "https://images.pexels.com/photos/2558605/pexels-photo-2558605.jpeg?cs=srgb&dl=pexels-anel-rossouw-2558605.jpg&fm=jpg" -height 25 +./imgcat -u "https://images.pexels.com/photos/2558605/pexels-photo-2558605.jpeg?cs=srgb&dl=pexels-anel-rossouw-2558605.jpg&fm=jpg" -r 25 ``` Capture d’écran 2021-12-11 à 17 40 55 @@ -29,10 +29,13 @@ go build ./cmd/imgcat ## Help ``` -# imgcat -h -Usage of imgcat: - -height int - maximum height in lines - -url string - url of the jpg +Usage: + imgcat [OPTIONS] + +Application Options: + -u, --url= url of the jpg + -r, --rows= maximum number of rows + +Help Options: + -h, --help Show this help message ``` diff --git a/cmd/imgcat/main.go b/cmd/imgcat/main.go index 6dcdb3b..760d84d 100644 --- a/cmd/imgcat/main.go +++ b/cmd/imgcat/main.go @@ -2,18 +2,19 @@ package main import ( "encoding/base64" - "flag" "fmt" "io" "log" "net/http" "os" "strings" + + "github.com/jessevdk/go-flags" ) type Options struct { - url string - height int + Url string `short:"u" long:"url" description:"url of the jpg"` + Rows int `short:"r" long:"rows" description:"maximum number of rows"` } func PrintHeader(size int64, height int) { @@ -46,16 +47,16 @@ func PrintImg(img io.ReadCloser) { } func main() { - var opts = &Options{} - flag.StringVar(&opts.url, "url", "", "url of the jpg") - flag.IntVar(&opts.height, "height", 0, "maximum height in lines") - flag.Parse() + var options Options + if _, err := flags.Parse(&options); err != nil { + os.Exit(1) + } - if !(strings.HasPrefix(opts.url, "http://") || strings.HasPrefix(opts.url, "https://")) { + if !(strings.HasPrefix(options.Url, "http://") || strings.HasPrefix(options.Url, "https://")) { log.Fatalln("url doesn't start with http") } - resp, err := http.Get(opts.url) + resp, err := http.Get(options.Url) if err != nil { log.Fatalln(err) } @@ -64,7 +65,7 @@ func main() { log.Fatalln(fmt.Sprintf("Issue to get img, status = %d", resp.StatusCode)) } - PrintHeader(resp.ContentLength, opts.height) + PrintHeader(resp.ContentLength, options.Rows) PrintImg(resp.Body) PrintFooter() diff --git a/go.mod b/go.mod index b9c0cc4..3fe5cca 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/celogeek/imgcat go 1.17 + +require ( + github.com/jessevdk/go-flags v1.5.0 // indirect + golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..df31363 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= +github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=