/
go-gist.go
172 lines (157 loc) · 3.98 KB
/
go-gist.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bytes"
"code.google.com/p/goauth2/oauth"
"errors"
"flag"
"fmt"
"github.com/google/go-github/github"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"net/url"
"runtime"
"strings"
)
const VERSION = "0.1.1"
var copyCommands = []string{"pbcopy", "xclip", "xsel", "putclip"}
var pasteCommands = []string{"pbpaste", "xclip -o", "xsel -o", "getclip"}
var private bool
var showHelp bool
var showVersion bool
var paste bool
var copyURL bool
var openGist bool
var description string
func init() {
flag.BoolVar(&private, "p", false, "private gist")
flag.BoolVar(&showHelp, "h", false, "show help")
flag.BoolVar(&showVersion, "v", false, "show version")
flag.BoolVar(&paste, "P", false, "paste from the clipboard")
flag.BoolVar(©URL, "c", false, "copy gist URL to the clipboard")
flag.BoolVar(&openGist, "o", false, "open the gist in a browser")
flag.StringVar(&description, "d", "", "description of the gist")
}
func main() {
flag.Parse()
if showHelp {
flag.PrintDefaults()
os.Exit(0)
}
if showVersion {
fmt.Printf("go-gist v%s\n", VERSION)
os.Exit(0)
}
client := client()
public := !private
gist := &github.Gist{Public: &public, Files: gistFiles(), Description: &description}
gist, _, err := client.Gists.Create(gist)
if err != nil {
log.Fatal(err)
}
fmt.Println(*gist.HTMLURL)
if copyURL {
copyToClipboard(*gist.HTMLURL)
}
if openGist {
openURLInBrowser(*gist.HTMLURL)
}
}
func client() *github.Client {
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
}
client := github.NewClient(t.Client())
if githubURL := os.Getenv("GITHUB_URL"); len(githubURL) > 0 {
if !strings.HasSuffix(githubURL, "/") {
githubURL = githubURL + "/"
}
githubURL += "api/v3/"
url, err := url.Parse(githubURL)
if err != nil {
log.Fatal(err)
}
client.BaseURL = url
}
return client
}
func gistFiles() map[github.GistFilename]github.GistFile {
files := map[github.GistFilename]github.GistFile{}
args := flag.Args()
if paste {
name := "-"
content, err := pasteFromClipboard()
if err != nil {
log.Fatal(err)
}
file := github.GistFile{Filename: &name, Content: &content}
files[github.GistFilename(name)] = file
} else if len(args) == 0 {
name := "-"
bytes, _ := ioutil.ReadAll(os.Stdin)
content := string(bytes)
file := github.GistFile{Filename: &name, Content: &content}
files[github.GistFilename(name)] = file
} else {
for _, arg := range args {
bytes, err := ioutil.ReadFile(arg)
if err != nil {
log.Fatal(err)
}
content := string(bytes)
basename := path.Base(arg)
file := github.GistFile{Filename: &basename, Content: &content}
files[github.GistFilename(arg)] = file
}
}
return files
}
func copyToClipboard(content string) {
for _, command := range copyCommands {
var c *exec.Cmd
c = exec.Command("which", command)
if err := c.Run(); err != nil {
continue
}
c = exec.Command(command)
c.Stdin = strings.NewReader(content)
if err := c.Run(); err != nil {
log.Fatal("Failed to copy to the clipboard")
}
return
}
log.Fatal("Failed to copy to the clipboard")
}
func pasteFromClipboard() (string, error) {
for _, command := range pasteCommands {
var c *exec.Cmd
c = exec.Command("which", command)
if err := c.Run(); err != nil {
continue
}
var stdoutBuffer bytes.Buffer
c = exec.Command(command)
c.Stdout = &stdoutBuffer
if err := c.Run(); err != nil {
return "", errors.New("Failed to paste from the clipboard. (pasting command failed)")
}
return stdoutBuffer.String(), nil
}
return "", errors.New("Failed to paste from the clipboard. (pasting command is not found)")
}
func openURLInBrowser(url string) {
var command string
if envBrowser := os.Getenv("BROWSER"); len(envBrowser) > 0 {
command = envBrowser
} else if runtime.GOOS == "darwin" {
command = "open"
} else {
log.Fatal("Cannot open the gist in the browser")
}
c := exec.Command(command, url)
if err := c.Run(); err != nil {
log.Fatal("Failed to open the gist in the browser")
}
}