use go-flags

This commit is contained in:
Celogeek 2021-12-12 19:00:55 +01:00
parent 5b74921ced
commit 650b8737b1
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
5 changed files with 31 additions and 18 deletions

View File

@ -11,4 +11,4 @@ $(BINARY): $(SOURCES)
all: $(BINARY) test all: $(BINARY) test
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

View File

@ -21,7 +21,7 @@ go build ./cmd/imgcat
## Usage ## 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
``` ```
<img width="1069" alt="Capture décran 2021-12-11 à 17 40 55" src="https://user-images.githubusercontent.com/65178/145684510-39617c22-5818-4573-896c-7e0e6915db91.png"> <img width="1069" alt="Capture décran 2021-12-11 à 17 40 55" src="https://user-images.githubusercontent.com/65178/145684510-39617c22-5818-4573-896c-7e0e6915db91.png">
@ -29,10 +29,13 @@ go build ./cmd/imgcat
## Help ## Help
``` ```
# imgcat -h Usage:
Usage of imgcat: imgcat [OPTIONS]
-height int
maximum height in lines Application Options:
-url string -u, --url= url of the jpg
url of the jpg -r, --rows= maximum number of rows
Help Options:
-h, --help Show this help message
``` ```

View File

@ -2,18 +2,19 @@ package main
import ( import (
"encoding/base64" "encoding/base64"
"flag"
"fmt" "fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"github.com/jessevdk/go-flags"
) )
type Options struct { type Options struct {
url string Url string `short:"u" long:"url" description:"url of the jpg"`
height int Rows int `short:"r" long:"rows" description:"maximum number of rows"`
} }
func PrintHeader(size int64, height int) { func PrintHeader(size int64, height int) {
@ -46,16 +47,16 @@ func PrintImg(img io.ReadCloser) {
} }
func main() { func main() {
var opts = &Options{} var options Options
flag.StringVar(&opts.url, "url", "", "url of the jpg") if _, err := flags.Parse(&options); err != nil {
flag.IntVar(&opts.height, "height", 0, "maximum height in lines") os.Exit(1)
flag.Parse() }
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") log.Fatalln("url doesn't start with http")
} }
resp, err := http.Get(opts.url) resp, err := http.Get(options.Url)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -64,7 +65,7 @@ func main() {
log.Fatalln(fmt.Sprintf("Issue to get img, status = %d", resp.StatusCode)) 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) PrintImg(resp.Body)
PrintFooter() PrintFooter()

5
go.mod
View File

@ -1,3 +1,8 @@
module github.com/celogeek/imgcat module github.com/celogeek/imgcat
go 1.17 go 1.17
require (
github.com/jessevdk/go-flags v1.5.0 // indirect
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
)

4
go.sum Normal file
View File

@ -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=