Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure: Fixes #11 #42

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"errors"
"flag"
"html/template"
"io/ioutil"
"os"
"strings"
"time"

"github.com/muesli/markscribe/pkg/gh"
"github.com/muesli/markscribe/pkg/gr"
"github.com/muesli/markscribe/pkg/rss"
"github.com/muesli/markscribe/pkg/utils"
)

var (
ErrUsage = errors.New("usage error")
)

func Execute() error {
write := flag.String("write", "", "write output to")

flag.Parse()

if len(flag.Args()) == 0 {
return ErrUsage
}

tplIn, err := ioutil.ReadFile(flag.Args()[0])
if err != nil {
return err
}

// Create GitHub and GoodReads clients
ghClient := gh.New()
grClient := gr.New()

// Set GitHub username
err = ghClient.GetUsername()
if err != nil {
return err
}

tpl, err := template.New("tpl").Funcs(template.FuncMap{
/* GitHub */
"recentContributions": ghClient.GetRecentContributions,
"recentPullRequests": ghClient.GetRecentPullRequests,
"recentReleases": ghClient.GetRecentReleases,
"recentRepos": ghClient.GetRecentRepos,
"recentStars": ghClient.GetRecentStars,
"followers": ghClient.GetRecentFollowers,
"sponsors": ghClient.GetSponsors,
"gists": ghClient.GetRecentGists,
/* RSS */
"rss": rss.Feed,
/* GoodReads */
"goodReadsCurrentlyReading": grClient.GetCurrentlyReading,
"goodReadsReviews": grClient.GetReviews,
/* Utils */
"humanize": utils.Humanized,
"toLower": strings.ToLower,
"toUpper": strings.ToUpper,
"reverse": utils.Reverse,
"now": time.Now,
}).Parse(string(tplIn))
if err != nil {
return err
}

w := os.Stdout
if len(*write) > 0 {
f, err := os.Create(*write)
if err != nil {
return err
}
defer f.Close()
w = f
}

return tpl.Execute(w, nil)
}
62 changes: 0 additions & 62 deletions gists.go

This file was deleted.

21 changes: 0 additions & 21 deletions goodreads.go

This file was deleted.

96 changes: 3 additions & 93 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,15 @@
package main

import (
"context"
"flag"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"strings"
"time"

"github.com/KyleBanks/goodreads"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)

var (
gitHubClient *githubv4.Client
goodReadsClient *goodreads.Client
goodReadsID string
username string

write = flag.String("write", "", "write output to")
"github.com/muesli/markscribe/cmd"
)

func main() {
flag.Parse()

if len(flag.Args()) == 0 {
fmt.Println("Usage: markscribe [template]")
os.Exit(1)
}

tplIn, err := ioutil.ReadFile(flag.Args()[0])
if err != nil {
fmt.Println("Can't read file:", err)
os.Exit(1)
}

tpl, err := template.New("tpl").Funcs(template.FuncMap{
/* GitHub */
"recentContributions": recentContributions,
"recentPullRequests": recentPullRequests,
"recentRepos": recentRepos,
"recentReleases": recentReleases,
"followers": recentFollowers,
"recentStars": recentStars,
"gists": gists,
"sponsors": sponsors,
/* RSS */
"rss": rssFeed,
/* GoodReads */
"goodReadsReviews": goodReadsReviews,
"goodReadsCurrentlyReading": goodReadsCurrentlyReading,
/* Utils */
"humanize": humanized,
"reverse": reverse,
"now": time.Now,
"toLower": strings.ToLower,
}).Parse(string(tplIn))
if err != nil {
fmt.Println("Can't parse template:", err)
os.Exit(1)
}

var httpClient *http.Client
gitHubToken := os.Getenv("GITHUB_TOKEN")
goodReadsToken := os.Getenv("GOODREADS_TOKEN")
goodReadsID = os.Getenv("GOODREADS_USER_ID")
if len(gitHubToken) > 0 {
httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gitHubToken},
))
}

gitHubClient = githubv4.NewClient(httpClient)
goodReadsClient = goodreads.NewClient(goodReadsToken)

if len(gitHubToken) > 0 {
username, err = getUsername()
if err != nil {
fmt.Println("Can't retrieve GitHub profile:", err)
os.Exit(1)
}
}

w := os.Stdout
if len(*write) > 0 {
f, err := os.Create(*write)
if err != nil {
fmt.Println("Can't create:", err)
os.Exit(1)
}
defer f.Close()
w = f
}

err = tpl.Execute(w, nil)
if err != nil {
fmt.Println("Can't render template:", err)
if err := cmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
34 changes: 34 additions & 0 deletions pkg/gh/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gh

import (
"context"
"net/http"
"os"

"github.com/shurcooL/githubv4"

"golang.org/x/oauth2"
)

type Client struct {
client *githubv4.Client
username githubv4.String
}

type Map map[string]interface{}

// New creates and returns a new GitHub client.
func New() *Client {
gitHubToken := os.Getenv("GITHUB_TOKEN")
httpClient := http.DefaultClient

if len(gitHubToken) > 0 {
httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gitHubToken},
))
}

return &Client{
client: githubv4.NewClient(httpClient),
}
}
28 changes: 28 additions & 0 deletions pkg/gh/gists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gh

import (
"context"

"github.com/muesli/markscribe/pkg/types"

"github.com/shurcooL/githubv4"
)

// GetRecentGists retrieves 'n' recent gists.
func (c *Client) GetRecentGists(n int) []types.Gist {
gists := []types.Gist{}
variables := Map{
"count": githubv4.Int(n),
"username": c.username,
}
err := c.client.Query(context.Background(), &gistsQuery, variables)
if err != nil {
panic(err)
}

for _, v := range gistsQuery.User.Gists.Edges {
gists = append(gists, types.GistFromQL(v.Node))
}

return gists
}
Loading