Skip to content

Commit

Permalink
init draft
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Sep 1, 2021
0 parents commit 000ea9c
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
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
37 changes: 37 additions & 0 deletions cmd/twitterview/main.go
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")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/azimut/cli-view

go 1.14
53 changes: 53 additions & 0 deletions internal/fetch/fetch.go
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
}
8 changes: 8 additions & 0 deletions internal/twitter/tweet.go
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)
}
15 changes: 15 additions & 0 deletions internal/twitter/type.go
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
}

0 comments on commit 000ea9c

Please sign in to comment.