-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
93 lines (72 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"flag"
"fmt"
"github.com/rkoesters/trumpet/source/twitter/logger"
"gopkg.in/ChimeraCoder/anaconda.v2"
"os"
"strings"
)
var (
showVersion = flag.Bool("version", false, "print version information and exit")
outfile = flag.String("o", "trumpet.conf", "output file")
skipVerify = flag.Bool("skip-verify", false, "skip credential verification")
)
func main() {
flag.Parse()
// We don't take any arguments, only flags.
if flag.NArg() != 0 {
flag.Usage()
os.Exit(1)
}
if *showVersion {
fmt.Println(Version())
os.Exit(0)
}
ckey, csecret, atoken, asecret := getInput()
if !*skipVerify {
verifyCredentials(ckey, csecret, atoken, asecret)
}
writeOutput(ckey, csecret, atoken, asecret)
fmt.Println("Wrote configuration to", *outfile)
}
func getInput() (ckey, csecret, atoken, asecret string) {
sc := bufio.NewScanner(os.Stdin)
fmt.Print("Consumer Key: ")
sc.Scan()
ckey = strings.TrimSpace(sc.Text())
fmt.Print("Consumer Secret: ")
sc.Scan()
csecret = strings.TrimSpace(sc.Text())
fmt.Print("Access Token: ")
sc.Scan()
atoken = strings.TrimSpace(sc.Text())
fmt.Print("Access Secret: ")
sc.Scan()
asecret = strings.TrimSpace(sc.Text())
return
}
func verifyCredentials(ckey, csecret, atoken, asecret string) {
anaconda.SetConsumerKey(ckey)
anaconda.SetConsumerSecret(csecret)
tapi := anaconda.NewTwitterApi(atoken, asecret)
tapi.SetLogger(logger.New(logger.LevelInfo))
ok, _ := tapi.VerifyCredentials()
if !ok {
fmt.Fprintf(os.Stderr, "Failed to verify credentials\n")
os.Exit(1)
}
}
func writeOutput(ckey, csecret, atoken, asecret string) {
f, err := os.Create(*outfile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening %v: %v\n", *outfile, err)
os.Exit(1)
}
defer f.Close()
fmt.Fprint(f, "consumer-key=", ckey, "\n")
fmt.Fprint(f, "consumer-secret=", csecret, "\n")
fmt.Fprint(f, "access-token=", atoken, "\n")
fmt.Fprint(f, "access-secret=", asecret, "\n")
}