2019-01-01 19:35:22 +01:00
package main
import (
"fmt"
2020-10-11 13:14:57 +02:00
goutil "git.gutmet.org/goutil.git/misc"
2019-01-01 19:35:22 +01:00
"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
}
2020-10-15 11:32:12 +02:00
func ( c * credentials ) writeCredentials ( appDir string ) {
2019-01-01 19:35:22 +01:00
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 )
}
2020-10-15 11:32:12 +02:00
func readCredentials ( appDir string ) * credentials {
2019-01-01 19:35:22 +01:00
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
}
2020-10-21 08:34:11 +02:00
fmt . Fprintln ( os . Stderr , "Did not find " + appDir + ", creating." )
fmt . Fprintln ( os . Stderr , "Go to " + registerAppURL + " to register a developer account, register a new app and generate access tokens with read & write permissions\n" )
2019-01-01 19:35:22 +01:00
err := os . MkdirAll ( appDir , 0755 )
optLogFatal ( "createAppDir" , err )
2020-08-08 11:52:42 +02:00
ck := ask ( "Consumer Key/API Key" )
cs := ask ( "Consumer Secret/API Secret" )
2019-01-01 19:35:22 +01:00
at := ask ( "Access Token" )
ats := ask ( "Access Token Secret" )
c := credentials { ck , cs , at , ats }
2020-10-15 11:32:12 +02:00
c . writeCredentials ( appDir )
2019-01-01 19:35:22 +01:00
}
2020-10-15 11:32:12 +02:00
func appDir ( ) string {
log := func ( err error ) { optLogFatal ( "appDir" , err ) }
2019-01-01 19:35:22 +01:00
currentUser , err := user . Current ( )
log ( err )
homeDir := currentUser . HomeDir
appDir := path . Join ( homeDir , AppDir )
if ! goutil . PathExists ( appDir ) {
createAppDir ( appDir )
}
2020-10-15 11:32:12 +02:00
return appDir
2019-01-01 19:35:22 +01:00
}
func getClient ( ) * http . Client {
2020-10-15 11:32:12 +02:00
c := readCredentials ( appDir ( ) )
2019-01-01 19:35:22 +01:00
config := oauth1 . NewConfig ( c . consumerKey , c . consumerSecret )
token := oauth1 . NewToken ( c . accessToken , c . accessTokenSecret )
return config . Client ( oauth1 . NoContext , token )
}