Skip to content

Commit

Permalink
Using google/uuid package
Browse files Browse the repository at this point in the history
  • Loading branch information
178inaba committed Oct 23, 2019
1 parent 10b566a commit 862d2be
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
4 changes: 1 addition & 3 deletions go.mod
Expand Up @@ -5,12 +5,10 @@ go 1.13
require (
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/google/uuid v1.1.1
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859
github.com/mitchellh/go-homedir v1.1.0
github.com/satori/go.uuid v1.2.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
11 changes: 2 additions & 9 deletions go.sum
Expand Up @@ -5,19 +5,14 @@ github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4r
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859 h1:smQbSzmT3EHl4EUwtFwFGmGIpiYgIiiPeVv1uguIQEE=
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -31,5 +26,3 @@ golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11 changes: 9 additions & 2 deletions main.go
Expand Up @@ -18,9 +18,9 @@ import (
"golang.org/x/oauth2"

"github.com/google/go-github/github"
"github.com/google/uuid"
tty "github.com/mattn/go-tty"
homedir "github.com/mitchellh/go-homedir"
uuid "github.com/satori/go.uuid"
)

const tokenFileEdgePath = "gistup/token"
Expand Down Expand Up @@ -182,14 +182,21 @@ func getToken(ctx context.Context, apiURL *url.URL, tokenFilePath string) (strin
t.Transport =
&http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}

c := github.NewClient(t.Client())
if apiURL != nil {
c.BaseURL = apiURL
}

u, err := uuid.NewRandom()
if err != nil {
return "", err
}

a, _, err := c.Authorizations.Create(ctx, &github.AuthorizationRequest{
Scopes: []github.Scope{"gist"},
Note: github.String("gistup"),
Fingerprint: github.String(uuid.NewV4().String()),
Fingerprint: github.String(u.String()),
})
if err != nil {
return "", err
Expand Down
62 changes: 46 additions & 16 deletions main_test.go
Expand Up @@ -18,7 +18,6 @@ import (

"github.com/google/go-github/github"
tty "github.com/mattn/go-tty"
uuid "github.com/satori/go.uuid"
)

func TestRun(t *testing.T) {
Expand Down Expand Up @@ -62,7 +61,7 @@ func TestRun(t *testing.T) {
mkdirAll = func(path string, perm os.FileMode) error { return nil }
writeFile = func(filename string, data []byte, perm os.FileMode) error { return nil }

for _, test := range tests {
for i, test := range tests {
isCreateGistErr := test.isCreateGistErr
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if test.isAuthError && r.URL.Path == "/authorizations" {
Expand All @@ -84,7 +83,7 @@ func TestRun(t *testing.T) {
runCmd = func(c *exec.Cmd) error { return test.runCmdError }
removeFile = func(name string) error { return test.removeError }
if got, want := run(), test.exitCode; got != want {
t.Fatalf("run exit code %d, want %d", got, want)
t.Fatalf("[%d] run exit code %d, want %d", i+1, got, want)
}
}
}
Expand Down Expand Up @@ -119,8 +118,13 @@ func TestGetClientWithToken(t *testing.T) {
t.Fatalf("should not be fail: %v", err)
}

td, err := ioutil.TempDir("", "gistup")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
fp := filepath.Join(td, "token")

*isAnonymous = false
fp := filepath.Join(os.TempDir(), uuid.NewV4().String())
readUsername = func(t *tty.TTY) (string, error) { return "", io.EOF }
readPassword = func(t *tty.TTY) (string, error) { return "", nil }
if _, err := getClientWithToken(context.Background(), fp); err == nil {
Expand Down Expand Up @@ -164,12 +168,17 @@ func TestGetToken(t *testing.T) {
t.Fatalf("should be fail: %v", err)
}

fp := filepath.Join(os.TempDir(), uuid.NewV4().String())
td, err := ioutil.TempDir("", "gistup")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
fp := filepath.Join(td, "token")

if err := ioutil.WriteFile(fp, []byte(""), 0600); err != nil {
t.Fatalf("should not be fail: %v", err)
}
defer func() {
if err := os.Remove(fp); err != nil {
if err := os.RemoveAll(td); err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
Expand Down Expand Up @@ -222,12 +231,18 @@ func TestPrompt(t *testing.T) {

func TestSaveToken(t *testing.T) {
token := "foobar"
fp := filepath.Join(os.TempDir(), uuid.NewV4().String())

td, err := ioutil.TempDir("", "gistup")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
fp := filepath.Join(td, "token")

if err := saveToken(token, fp); err != nil {
t.Fatalf("should not be fail: %v", err)
}
defer func() {
if err := os.Remove(fp); err != nil {
if err := os.RemoveAll(td); err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
Expand Down Expand Up @@ -260,12 +275,17 @@ func TestSaveToken(t *testing.T) {
t.Fatalf("should be fail: %v", err)
}

errFP := filepath.Join(os.TempDir(), uuid.NewV4().String(), uuid.NewV4().String())
errTD, err := ioutil.TempDir("", "gistup")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
errFP := filepath.Join(errTD, "token")

if err := os.MkdirAll(errFP, 0700); err != nil {
t.Fatalf("should not be fail: %v", err)
}
defer func() {
if err := os.RemoveAll(filepath.Dir(errFP)); err != nil {
if err := os.RemoveAll(errTD); err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
Expand All @@ -275,8 +295,8 @@ func TestSaveToken(t *testing.T) {
}

func TestCreateGist(t *testing.T) {
filename := uuid.NewV4().String()
tc := "foobar"
filename := "test.md"
tc := "# foobar"
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
Expand All @@ -303,12 +323,17 @@ func TestCreateGist(t *testing.T) {
t.Fatalf("should be fail: %v", err)
}

fp := filepath.Join(os.TempDir(), filename)
td, err := ioutil.TempDir("", "gistup")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}

fp := filepath.Join(td, filename)
if err := ioutil.WriteFile(fp, []byte(tc), 0400); err != nil {
t.Fatalf("should not be fail: %v", err)
}
defer func() {
if err := os.Remove(fp); err != nil {
if err := os.RemoveAll(td); err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
Expand All @@ -331,13 +356,18 @@ func TestCreateGist(t *testing.T) {
}

func TestReadFile(t *testing.T) {
fp := filepath.Join(os.TempDir(), uuid.NewV4().String())
td, err := ioutil.TempDir("", "gistup")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
fp := filepath.Join(td, "token")

tc := "foobar"
if err := ioutil.WriteFile(fp, []byte(tc), 0400); err != nil {
t.Fatalf("should not be fail: %v", err)
}
defer func() {
if err := os.Remove(fp); err != nil {
if err := os.RemoveAll(td); err != nil {
t.Fatalf("should not be fail: %v", err)
}
}()
Expand Down

0 comments on commit 862d2be

Please sign in to comment.