-
Notifications
You must be signed in to change notification settings - Fork 0
/
www.go
149 lines (126 loc) · 2.84 KB
/
www.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/Bowery/conf"
"github.com/Bowery/www/providers"
)
var (
configPath string
err error
db *conf.JSON
ps map[string]providers.Provider
)
const usage = `Usage:
$ somecmd | www <provider> [options]
www reads from standard input and pipes the given
input to a provider.
Providers: slack, gist, gmail, s3.
To setup a provider
$ www setup <provider>
For information on how to use a provider
$ www <provider> --help
`
func init() {
homeVar := "HOME"
if runtime.GOOS == "windows" {
homeVar = "USERPROFILE"
}
configPath = filepath.Join(os.Getenv(homeVar), ".wwwconf")
ps = providers.Providers
}
func main() {
// Create new local config and attempt to read in configuration
// file specified by configPath. If the file does not exist,
// create it.
config := map[string]map[string]string{}
db, err = conf.NewJSON(configPath)
if err != nil {
if os.IsNotExist(err) {
dat, _ := json.Marshal(config)
ioutil.WriteFile(configPath, dat, os.ModePerm)
} else {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
err = db.Load(&config)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// Validate and set the provider.
if len(os.Args) <= 1 {
fmt.Println(usage)
os.Exit(2)
}
var provider providers.Provider
if os.Args[1] == "setup" {
if len(os.Args) > 2 {
_, ok := config[os.Args[2]]
if !ok {
config[os.Args[2]] = map[string]string{}
}
provider = ps[os.Args[2]]
provider.Setup(config[os.Args[2]])
err = db.Save(config)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
os.Exit(0)
}
}
provider = ps[os.Args[1]]
if provider == nil {
fmt.Fprintln(os.Stderr, "Invalid provider.")
os.Exit(1)
}
// Fill the config with an empty entry if nil.
_, ok := config[os.Args[1]]
if !ok {
config[os.Args[1]] = map[string]string{}
}
// Initialize provider. Passes flags and config
// for that specific provider.
err = provider.Init(os.Args[2:], config[os.Args[1]])
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// Verify there is valid input from Stdin.
stat, err := os.Stdin.Stat()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// If there is no valid input print help.
if stat.Mode()&os.ModeCharDevice != 0 {
os.Exit(1)
}
// Read in Stdin.
var content bytes.Buffer
_, err = io.Copy(&content, os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// Execute `Send` method of provider.
err = provider.Send(content)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// Save any configuration changes made by provider.
err = db.Save(config)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}