Skip to content

Commit 5c6cc44

Browse files
committed
add cli.OutputOptions()
1 parent e87a5e8 commit 5c6cc44

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

cmd/polls/main.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package main
22

33
import (
4-
"bytes"
5-
"fmt"
6-
"io"
74
"log"
85
"os"
96

10-
"github.com/atotto/clipboard"
7+
"github.com/tj/kingpin"
118

129
"github.com/tj/gh-polls/internal/cli"
1310
"github.com/tj/gh-polls/internal/client"
14-
"github.com/tj/kingpin"
1511
)
1612

1713
// Config.
@@ -43,16 +39,6 @@ func main() {
4339
log.Fatalf("error creating poll: %s", err)
4440
}
4541

46-
var buf bytes.Buffer
47-
48-
for _, o := range *options {
49-
fmt.Fprintln(&buf, cli.Link(out.ID, o))
50-
}
51-
52-
if err := clipboard.WriteAll(buf.String()); err == nil {
53-
fmt.Fprintln(os.Stderr, "Copied to clipboard!")
54-
}
55-
56-
io.Copy(os.Stdout, &buf)
42+
cli.OutputOptions(out.ID, *options)
5743
}
5844
}

internal/cli/cli.go

+20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
package cli
22

33
import (
4+
"bytes"
45
"fmt"
6+
"io"
57
"net/url"
8+
"os"
9+
10+
"github.com/atotto/clipboard"
611
)
712

13+
// OutputOptions outputs markdown options and copies to the clipboard.
14+
func OutputOptions(id string, options []string) {
15+
var buf bytes.Buffer
16+
17+
for _, o := range options {
18+
fmt.Fprintln(&buf, Link(id, o))
19+
}
20+
21+
if err := clipboard.WriteAll(buf.String()); err == nil {
22+
fmt.Fprintln(os.Stderr, "Copied to clipboard!")
23+
}
24+
25+
io.Copy(os.Stdout, &buf)
26+
}
27+
828
// Link returns a poll option link with image.
929
func Link(id, option string) string {
1030
return fmt.Sprintf(`[%s](https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod/poll/%s/%s/vote)`, Image(id, option), id, url.PathEscape(option))

0 commit comments

Comments
 (0)