Skip to content

Commit

Permalink
Support environment variables for api credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
imwally committed Feb 25, 2020
1 parent e949ee9 commit 08f874e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,30 @@ portal](https://developer.twitter.com/en/docs/basics/getting-started).

## How to Use

The 4 keys from above are required for every call to `unlike`.
The 4 keys from above are required for every call to
`unlike`. Environment variables are also supported:

```
Usage of unlike:
-access-token string
Twitter API Access Token
-access-token-secret string
Twitter API Access Token Secret
-consumer string
Twitter API Consumer Key
TWITTER_API_KEY
TWITTER_API_KEY_SECRET
TWITTER_TOKEN
TWITTER_TOKEN_SECRET
```

```
Usage of ./unlike:
-dump
Dump all likes to stdout in json format
-keep-following
Keep liked tweets from people you follow
-secret string
Don't unlike any tweets from people you follow
-key string
Twitter API Consumer Key
-key-secret string
Twitter API Secret Key
-token string
Twitter API Access Token
-token-secret string
Twitter API Access Token Secret
```

### Dump all likes to stdout in json format
Expand Down
55 changes: 32 additions & 23 deletions cmd/unlike/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,61 @@ import (
)

var (
keyConsumer string
keySecret string
accessToken string
accessTokenSecret string
key string
keySecret string
token string
tokenSecret string

keepFollowing bool
dumpLikes bool
)

func init() {
flag.StringVar(&keyConsumer, "consumer", "", "Twitter API Consumer Key")
flag.StringVar(&keySecret, "secret", "", "Twitter API Secret Key")
flag.StringVar(&accessToken, "access-token", "", "Twitter API Access Token")
flag.StringVar(&accessTokenSecret, "access-token-secret", "", "Twitter API Access Token Secret")
flag.StringVar(&key, "key", "", "Twitter API Consumer Key")
flag.StringVar(&keySecret, "key-secret", "", "Twitter API Secret Key")
flag.StringVar(&token, "token", "", "Twitter API Access Token")
flag.StringVar(&tokenSecret, "token-secret", "", "Twitter API Access Token Secret")

flag.BoolVar(&keepFollowing, "keep-following", false, "Don't unlike any tweets from people you follow")
flag.BoolVar(&dumpLikes, "dump", false, "Dump all likes to stdout in json format")
flag.Parse()
}

func main() {
if keySecret == "" {
fmt.Fprintf(os.Stderr, "error: no secret key set\n")
os.Exit(2)

if key == "" {
if key = os.Getenv("TWITTER_API_KEY"); key == "" {
fmt.Fprintf(os.Stderr, "error: no api key set\n")
os.Exit(2)
}
}

if keyConsumer == "" {
fmt.Fprintf(os.Stderr, "error: no consumer key set\n")
os.Exit(2)
if keySecret == "" {
if keySecret = os.Getenv("TWITTER_API_KEY_SECRET"); keySecret == "" {
fmt.Fprintf(os.Stderr, "error: no secret key set\n")
os.Exit(2)
}
}

if accessToken == "" {
fmt.Fprintf(os.Stderr, "error: no access token set\n")
os.Exit(2)
if token == "" {
if token = os.Getenv("TWITTER_API_TOKEN"); token == "" {
fmt.Fprintf(os.Stderr, "error: no access token set\n")
os.Exit(2)
}
}

if accessTokenSecret == "" {
fmt.Fprintf(os.Stderr, "error: no access token secret set\n")
os.Exit(2)
if tokenSecret == "" {
if tokenSecret = os.Getenv("TWITTER_API_TOKEN_SECRET"); tokenSecret == "" {
fmt.Fprintf(os.Stderr, "error: no access token secret set\n")
os.Exit(2)
}
}

ta := &tapi.TwitterAPI{
KeyConsumer: keyConsumer,
KeyConsumer: key,
KeySecret: keySecret,
AccessToken: accessToken,
AccessTokenSecret: accessTokenSecret,
AccessToken: token,
AccessTokenSecret: tokenSecret,
}

// If dump is specified then ONLY dump likes and disregard other flags
Expand Down

0 comments on commit 08f874e

Please sign in to comment.