-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 000ea9c
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
# Created by https://www.toptal.com/developers/gitignore/api/go | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=go | ||
|
||
### Go ### | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
### Go Patch ### | ||
/vendor/ | ||
/Godeps/ | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/azimut/cli-view/internal/twitter" | ||
) | ||
|
||
type options struct { | ||
userAgent string | ||
timeout time.Duration | ||
width int | ||
} | ||
|
||
var opt options | ||
|
||
func init() { | ||
flag.StringVar(&opt.userAgent, "A", "CliView/0.1", "default User-Agent sent") | ||
flag.DurationVar(&opt.timeout, "t", time.Second*5, "timeout in seconds") | ||
flag.IntVar(&opt.width, "w", 0, "fixed with, defaults to console width") | ||
} | ||
|
||
func usage() { | ||
fmt.Printf("Usage: %s [OPTIONS] URL ...\n", os.Args[0]) | ||
flag.PrintDefaults() | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
flag.Usage = usage | ||
t := twitter.Tweet{} | ||
t.GetHeader("what is this") | ||
println("HELP ME") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/azimut/cli-view | ||
|
||
go 1.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package fetch | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func makeRequest(url, ua string) (*http.Request, error) { | ||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("User-Agent", ua) | ||
return req, nil | ||
} | ||
|
||
func getResponse(req *http.Request, timeout time.Duration) (*http.Response, error) { | ||
client := &http.Client{Timeout: timeout} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
return resp, nil | ||
} | ||
|
||
func handleResponse(resp *http.Response) (string, error) { | ||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("invalid http status code %d", resp.StatusCode) | ||
} | ||
if b, err := ioutil.ReadAll(resp.Body); err == nil { | ||
return string(b), nil | ||
} | ||
return "", fmt.Errorf("no body read") | ||
} | ||
|
||
func Fetch(url, ua string, timeout time.Duration) (string, error) { | ||
request, err := makeRequest(url, ua) | ||
if err != nil { | ||
return "", err | ||
} | ||
response, err := getResponse(request, timeout) | ||
if err != nil { | ||
return "", err | ||
} | ||
body, err := handleResponse(response) | ||
if err != nil { | ||
return "", err | ||
} | ||
return body, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package twitter | ||
|
||
type Tweet struct { | ||
} | ||
|
||
func (t Tweet) GetHeader(url string) { | ||
println(url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package twitter | ||
|
||
type Embedded struct { | ||
Url string | ||
AuthorName string `json:"author_name"` | ||
AuthorUrl string `json:"author_url"` | ||
Html string | ||
width int | ||
height int | ||
kind string `json:"type"` | ||
cacheAge int `json:cache_age` | ||
providerName string `json:provider_name` | ||
providerUrl string `json:provider_url` | ||
version float32 | ||
} |