91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"git.gutmet.org/goutil.git"
|
||
|
"github.com/dghubble/oauth1"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/user"
|
||
|
"path"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
AppDir = ".drivel" //inside home dir
|
||
|
fileConsumerKey = "ConsumerKey"
|
||
|
fileConsumerSecret = "ConsumerSecret"
|
||
|
fileAccessToken = "AccessToken"
|
||
|
fileAccessTokenSecret = "AccessTokenSecret"
|
||
|
registerAppURL = "https://apps.twitter.com/app/new"
|
||
|
)
|
||
|
|
||
|
type credentials struct {
|
||
|
consumerKey string
|
||
|
consumerSecret string
|
||
|
accessToken string
|
||
|
accessTokenSecret string
|
||
|
}
|
||
|
|
||
|
func (c *credentials) writeToAppDir(appDir string) {
|
||
|
write := func(file string, s string) {
|
||
|
optLogFatal("writeToAppDir", ioutil.WriteFile(path.Join(appDir, file), []byte(s), 0640))
|
||
|
}
|
||
|
write(fileConsumerKey, c.consumerKey)
|
||
|
write(fileConsumerSecret, c.consumerSecret)
|
||
|
write(fileAccessToken, c.accessToken)
|
||
|
write(fileAccessTokenSecret, c.accessTokenSecret)
|
||
|
}
|
||
|
|
||
|
func readFromAppDir(appDir string) *credentials {
|
||
|
read := func(f string) string {
|
||
|
s, err := goutil.ReadFile(path.Join(appDir, f))
|
||
|
optLogFatal("readFromAppDir", err)
|
||
|
return s
|
||
|
}
|
||
|
ck := read(fileConsumerKey)
|
||
|
cs := read(fileConsumerSecret)
|
||
|
at := read(fileAccessToken)
|
||
|
ats := read(fileAccessTokenSecret)
|
||
|
return &credentials{ck, cs, at, ats}
|
||
|
}
|
||
|
|
||
|
func createAppDir(appDir string) {
|
||
|
ask := func(s string) string {
|
||
|
a, err := goutil.AskFor(s)
|
||
|
optLogFatal("createAppDir", err)
|
||
|
return a
|
||
|
}
|
||
|
fmt.Println("Did not find " + appDir + ", creating.")
|
||
|
fmt.Println("Go to " + registerAppURL + " to register a new app")
|
||
|
fmt.Println("and create an access token\n")
|
||
|
err := os.MkdirAll(appDir, 0755)
|
||
|
optLogFatal("createAppDir", err)
|
||
|
ck := ask("Consumer Key")
|
||
|
cs := ask("Consumer Secret")
|
||
|
at := ask("Access Token")
|
||
|
ats := ask("Access Token Secret")
|
||
|
c := credentials{ck, cs, at, ats}
|
||
|
c.writeToAppDir(appDir)
|
||
|
}
|
||
|
|
||
|
func loadCredentials() *credentials {
|
||
|
log := func(err error) { optLogFatal("loadCredentials", err) }
|
||
|
currentUser, err := user.Current()
|
||
|
log(err)
|
||
|
homeDir := currentUser.HomeDir
|
||
|
appDir := path.Join(homeDir, AppDir)
|
||
|
if !goutil.PathExists(appDir) {
|
||
|
createAppDir(appDir)
|
||
|
}
|
||
|
c := readFromAppDir(appDir)
|
||
|
return c
|
||
|
}
|
||
|
|
||
|
func getClient() *http.Client {
|
||
|
c := loadCredentials()
|
||
|
config := oauth1.NewConfig(c.consumerKey, c.consumerSecret)
|
||
|
token := oauth1.NewToken(c.accessToken, c.accessTokenSecret)
|
||
|
return config.Client(oauth1.NoContext, token)
|
||
|
}
|