Skip to content

Commit

Permalink
Merge pull request #3 from Tomohiro/screenshot
Browse files Browse the repository at this point in the history
Support take a screenshot
  • Loading branch information
tomohiro committed Sep 4, 2014
2 parents 7aee71b + 88a779f commit 93b855e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OUTPUT = gyazo
BUILDTOOL = gox
BUILDDIR = $(TOP)/pkg
XC_OS = "darwin linux windows"
XC_ARCH = "386 amd64 arm"
XC_ARCH = "386 amd64"
DISTDIR = $(BUILDDIR)/dist/$(VERSION)


Expand Down
43 changes: 34 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
Expand Down Expand Up @@ -63,16 +65,23 @@ type Image struct {
//
// Gyazo API docs: https://gyazo.com/api/docs/image
func upload(c *cli.Context) {
if len(c.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Try `gyazo --help` for more information")
exitCode = 1
return
var filename string
var err error

filename = c.Args().First()

if filename == "" {
filename, err = takeScreenshot()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to take a screenshot: %s\n", err)
exitCode = 1
}
}

filename := c.Args().First()
// Open and load the content from an image.
content, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open and read %s\n", filename)
fmt.Fprintf(os.Stderr, "Failed to open and read: %s\n", filename)
return
}
defer content.Close()
Expand Down Expand Up @@ -127,9 +136,25 @@ func upload(c *cli.Context) {
}
}

// takeScreenshot takes a screenshot and then returns it file path.
func takeScreenshot() (string, error) {
var err error
path := fmt.Sprintf("/tmp/image_upload%d.png", os.Getpid())

switch runtime.GOOS {
case "darwin":
err = exec.Command("screencapture", "-i", path).Run()
case "linux":
case "windows":
err = errors.New("unsupported os")
}

return path, err
}

// imageURL returns url of uploaded image.
func imageURL(r *http.Response) (string, error) {
var url = ""
var url string
var err error
if os.Getenv("GYAZO_ACCESS_TOKEN") != "" {
image := Image{}
Expand Down Expand Up @@ -157,7 +182,7 @@ func imageURL(r *http.Response) (string, error) {

// gyazoID returns Gyazo ID from stored file.
func gyazoID() string {
var id = ""
var id string
body, err := ioutil.ReadFile(gyazoIDPath())
if err != nil {
return id
Expand Down Expand Up @@ -206,7 +231,7 @@ func storeGyazoID(id string) error {
func gyazoIDPath() string {
homedir, _ := homedir.Dir()

var path = ""
var path string
switch runtime.GOOS {
case "darwin":
path = fmt.Sprintf("%s/Library/Gyazo/id", homedir)
Expand Down

0 comments on commit 93b855e

Please sign in to comment.