Skip to content

Commit

Permalink
Handle case when no snippet file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 committed Nov 13, 2017
1 parent f5756ec commit 8e67fe4
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 121 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@

[[constraint]]
name = "golang.org/x/oauth2"

[[constraint]]
name = "github.com/briandowns/spinner"
3 changes: 2 additions & 1 deletion cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"

"github.com/knqyf263/pet/config"
petSync "github.com/knqyf263/pet/sync"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ func edit(cmd *cobra.Command, args []string) (err error) {
}

if config.Conf.Gist.AutoSync {
return autoSync(snippetFile)
return petSync.AutoSync(snippetFile)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/knqyf263/pet/config"
petSync "github.com/knqyf263/pet/sync"
"github.com/spf13/cobra"
)

Expand All @@ -23,7 +24,7 @@ Write access_token in config file (pet configure).
`)
}

return autoSync(config.Conf.General.SnippetFile)
return petSync.AutoSync(config.Conf.General.SnippetFile)
}

func init() {
Expand Down
118 changes: 0 additions & 118 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,19 @@ package cmd

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"time"

"golang.org/x/oauth2"

"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/google/go-github/github"
"github.com/knqyf263/pet/config"
"github.com/knqyf263/pet/dialog"
"github.com/knqyf263/pet/snippet"
)

func autoSync(file string) error {
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Start()
defer s.Stop()

fi, err := os.Stat(file)
if err != nil {
return err
}

client := githubClient()
gist, _, err := client.Gists.Get(context.Background(), config.Conf.Gist.GistID)
if err != nil {
return err
}
local := fi.ModTime().UTC()
remote := gist.UpdatedAt.UTC()

switch {
case local.After(remote):
return upload()
case remote.After(local):
return download(gist)
default:
return nil
}
}

func upload() (err error) {
ctx := context.Background()

var snippets snippet.Snippets
if err := snippets.Load(); err != nil {
return err
}

body, err := snippets.ToString()
if err != nil {
return err
}

client := githubClient()
gist := github.Gist{
Description: github.String("description"),
Public: github.Bool(config.Conf.Gist.Public),
Files: map[github.GistFilename]github.GistFile{
github.GistFilename(config.Conf.Gist.FileName): github.GistFile{
Content: github.String(body),
},
},
}

gistID := config.Conf.Gist.GistID
if gistID == "" {
var retGist *github.Gist
retGist, _, err = client.Gists.Create(ctx, &gist)
if err != nil {
return err
}
fmt.Printf("Gist ID: %s\n", retGist.GetID())
} else {
_, _, err = client.Gists.Edit(ctx, gistID, &gist)
if err != nil {
return err
}
}
fmt.Println("Upload success")
return nil
}

func download(gist *github.Gist) error {
var (
content = ""
snippetFile = config.Conf.General.SnippetFile
filename = config.Conf.Gist.FileName
)
for _, file := range gist.Files {
if *file.Filename == filename {
content = *file.Content
}
}
if content == "" {
return fmt.Errorf("%s is empty", filename)
}

var snippets snippet.Snippets
if err := snippets.Load(); err != nil {
return err
}
body, err := snippets.ToString()
if err != nil {
return err
}
if content == body {
// no need to download
return nil
}

fmt.Println("Download success")
return ioutil.WriteFile(snippetFile, []byte(content), os.ModePerm)
}

func editFile(command, file string) error {
command += " " + file
return run(command, os.Stdin, os.Stdout)
Expand Down Expand Up @@ -191,12 +82,3 @@ func filter(options []string) (commands []string, err error) {
}
return commands, nil
}

func githubClient() *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.Conf.Gist.AccessToken},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
return client
}
5 changes: 5 additions & 0 deletions snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ type SnippetInfo struct {
// Load reads toml file.
func (snippets *Snippets) Load() error {
snippetFile := config.Conf.General.SnippetFile
if _, err := os.Stat(snippetFile); os.IsNotExist(err) {
return nil
}
if _, err := toml.DecodeFile(snippetFile, snippets); err != nil {
return fmt.Errorf("Failed to load snippet file. %v", err)
}
return nil
}

// Save saves the snippets to toml file.
func (snippets *Snippets) Save() error {
snippetFile := config.Conf.General.SnippetFile
f, err := os.Create(snippetFile)
Expand All @@ -39,6 +43,7 @@ func (snippets *Snippets) Save() error {
return toml.NewEncoder(f).Encode(snippets)
}

// ToString returns the contents of toml file.
func (snippets *Snippets) ToString() (string, error) {
var buffer bytes.Buffer
err := toml.NewEncoder(&buffer).Encode(snippets)
Expand Down
Loading

0 comments on commit 8e67fe4

Please sign in to comment.