-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
118 lines (103 loc) · 2.44 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// -*- tab-width: 4; -*-
package main
import (
"flag"
"fmt"
"log"
"os"
)
const progname = "twet"
var homedir string
var conf Config = Config{
DiscloseIdentity: true,
Timeline: "full",
}
var configpath string
var debug bool
var dir string
var usage = fmt.Sprintf(`%s is a client for twtxt -- https://twtxt.readthedocs.org/en/stable/
Usage:
%s [flags] command [arguments]
Commands:
following
follow
unfollow
timeline
tweet or twet
Use "%s help [command]" for more information about a command.
Flags:
`, progname, progname, progname)
func main() {
log.SetPrefix(fmt.Sprintf("%s: ", progname))
log.SetFlags(0)
if homedir = os.Getenv("HOME"); homedir == "" {
log.Fatal("HOME env variable empty?! can't proceed")
}
flag.CommandLine.SetOutput(os.Stdout)
flag.BoolVar(&debug, "debug", false, "output debug info")
flag.StringVar(&dir, "dir", "", "set config directory")
flag.Usage = func() {
fmt.Print(usage)
flag.PrintDefaults()
}
flag.Parse()
configpath = conf.Read(dir)
switch flag.Arg(0) {
case "following":
if err := FollowingCommand(flag.Args()[1:]); err != nil {
log.Fatal(err)
}
case "follow":
if err := FollowCommand(flag.Args()[1:]); err != nil {
log.Fatal(err)
}
case "unfollow":
if err := UnfollowCommand(flag.Args()[1:]); err != nil {
log.Fatal(err)
}
case "timeline":
if err := TimelineCommand(flag.Args()[1:]); err != nil {
log.Fatal(err)
}
case "tweet", "twet":
if conf.Hooks.Pre != "" {
if _, err := execShell(homedir, conf.Hooks.Pre); err != nil {
log.Fatalf("error executing pre tweet hook: %s", err)
}
}
if err := TweetCommand(flag.Args()[1:]); err != nil {
log.Fatal(err)
}
if conf.Hooks.Post != "" {
if _, err := execShell(homedir, conf.Hooks.Post); err != nil {
log.Fatalf("error executing post tweet hook: %s", err)
}
}
case "help":
switch flag.Arg(1) {
case "following":
_ = FollowingCommand([]string{"-h"})
case "follow":
_ = FollowCommand([]string{"-h"})
case "unfollow":
_ = UnfollowCommand([]string{"-h"})
case "timeline":
_ = TimelineCommand([]string{"-h"})
case "tweet", "twet":
_ = TweetCommand([]string{"-h"})
case "":
flag.Usage()
os.Exit(2)
default:
log.Printf("Unknown help topic %q.\n", flag.Arg(1))
os.Exit(2)
}
case "version":
fmt.Printf("%s %s\n", progname, GetVersion())
case "":
flag.Usage()
os.Exit(2)
default:
log.Fatal(fmt.Sprintf("%q is not a valid command.\n", flag.Arg(0)))
}
}