Skip to content

Commit

Permalink
Merge pull request #19 from minodisk/dont-duplicate-file
Browse files Browse the repository at this point in the history
Don't duplicate file when create new post with generated file
  • Loading branch information
minodisk committed Feb 5, 2016
2 parents 2fb8cbc + 64c4052 commit 427664b
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 23 deletions.
52 changes: 50 additions & 2 deletions command/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func TestMain(m *testing.M) {
testutil.ResponseError(w, 500, err)
return
}
post.ID = "4bd431809afb1bb99e4f"
post.URL = "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f"
post.CreatedAt = model.Time{Time: time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)}
post.UpdatedAt = post.CreatedAt
b, err = json.Marshal(post)
Expand Down Expand Up @@ -351,6 +353,8 @@ func TestFetchPostWithID(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, os.Stdout, errBuf)
err := app.Run([]string{"qiitactl", "fetch", "post", "-i", "4bd431809afb1bb99e4f"})
Expand All @@ -362,6 +366,8 @@ func TestFetchPostWithID(t *testing.T) {
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 1)

b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -389,6 +395,8 @@ func TestFetchPostWithFilename(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

err := os.MkdirAll("mine/2000/01", 0755)
if err != nil {
t.Fatal(err)
Expand All @@ -410,6 +418,8 @@ tags:
t.Fatal(err)
}

testutil.ShouldExistFile(t, 1)

errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, os.Stdout, errBuf)
err = app.Run([]string{"qiitactl", "fetch", "post", "-f", "mine/2000/01/01-example-title.md"})
Expand All @@ -421,6 +431,8 @@ tags:
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 1)

b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -448,6 +460,8 @@ func TestShowPostWithID(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

buf := bytes.NewBuffer([]byte{})
errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, buf, errBuf)
Expand All @@ -460,6 +474,8 @@ func TestShowPostWithID(t *testing.T) {
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 0)

if string(buf.Bytes()) != `4bd431809afb1bb99e4f 2000/01/01 Example title
` {
t.Errorf("written text is wrong: %s", buf.Bytes())
Expand All @@ -470,6 +486,8 @@ func TestShowPostWithFilename(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

err := os.MkdirAll("mine/2000/01", 0755)
if err != nil {
t.Fatal(err)
Expand All @@ -491,6 +509,8 @@ tags:
t.Fatal(err)
}

testutil.ShouldExistFile(t, 1)

buf := bytes.NewBuffer([]byte{})
errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, buf, errBuf)
Expand All @@ -503,6 +523,8 @@ tags:
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 1)

if string(buf.Bytes()) != `4bd431809afb1bb99e4f 2000/01/01 Example title
` {
t.Errorf("written text is wrong: %s", buf.Bytes())
Expand All @@ -513,6 +535,8 @@ func TestShowPosts(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

buf := bytes.NewBuffer([]byte{})
errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, buf, errBuf)
Expand All @@ -525,6 +549,8 @@ func TestShowPosts(t *testing.T) {
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 0)

if string(buf.Bytes()) != `Posts in Qiita:
4bd431809afb1bb99e4f 2000/01/01 Example title
Posts in Qiita:Team (Increments Inc.):
Expand All @@ -538,6 +564,8 @@ func TestFetchPosts(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, os.Stdout, errBuf)
err := app.Run([]string{"qiitactl", "fetch", "posts"})
Expand All @@ -549,6 +577,8 @@ func TestFetchPosts(t *testing.T) {
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 2)

func() {
b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
Expand Down Expand Up @@ -602,13 +632,15 @@ func TestCreatePost(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

err := os.MkdirAll("mine/2000/01", 0755)
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile("mine/2000/01/01-example-title.md", []byte(`<!--
id: 4bd431809afb1bb99e4f
url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f
id: ""
url: ""
created_at: 2000-01-01T09:00:00+09:00
updated_at: 2000-01-01T09:00:00+09:00
private: false
Expand All @@ -625,6 +657,8 @@ tags:
t.Fatal(err)
}

testutil.ShouldExistFile(t, 1)

errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, os.Stdout, errBuf)
err = app.Run([]string{"qiitactl", "create", "post", "-f", "mine/2000/01/01-example-title.md"})
Expand All @@ -636,6 +670,8 @@ tags:
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 1)

b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -666,6 +702,8 @@ func TestUpdatePost(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

err := os.MkdirAll("mine/2000/01", 0755)
if err != nil {
t.Fatal(err)
Expand All @@ -689,6 +727,8 @@ tags:
t.Fatal(err)
}

testutil.ShouldExistFile(t, 1)

errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, os.Stdout, errBuf)
err = app.Run([]string{"qiitactl", "update", "post", "-f", "mine/2000/01/01-example-title.md"})
Expand All @@ -700,6 +740,8 @@ tags:
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 1)

b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -730,6 +772,8 @@ func TestDeletePost(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()

testutil.ShouldExistFile(t, 0)

err := os.MkdirAll("mine/2000/01", 0755)
if err != nil {
t.Fatal(err)
Expand All @@ -753,6 +797,8 @@ tags:
t.Fatal(err)
}

testutil.ShouldExistFile(t, 1)

errBuf := bytes.NewBuffer([]byte{})
app := cli.GenerateApp(client, os.Stdout, errBuf)
err = app.Run([]string{"qiitactl", "delete", "post", "-f", "mine/2000/01/01-example-title.md"})
Expand All @@ -764,6 +810,8 @@ tags:
t.Fatal(string(e))
}

testutil.ShouldExistFile(t, 1)

b, err := ioutil.ReadFile("mine/2000/01/01-example-title.md")
if err != nil {
t.Fatal(err)
Expand Down
61 changes: 40 additions & 21 deletions model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Post struct {
Body string `json:"body"` // Markdown形式の本文
RenderedBody string `json:"rendered_body"` // HTML形式の本文
Team *Team `json:"-"` // チーム
Path string `json:"-"`
}

type CreationOptions struct {
Expand Down Expand Up @@ -78,6 +79,7 @@ func NewPostWithFile(path string) (post Post, err error) {
if err != nil {
return
}
post.Path = path
return
}

Expand Down Expand Up @@ -162,26 +164,21 @@ func (post *Post) Delete(client api.Client) (err error) {
return
}

func (post Post) Save() (err error) {
var path string
if post.ID != "" {
path, err = post.findPath()
if err != nil {
return err
}
}
if path == "" {
path = post.createPath()
func (post *Post) Save() (err error) {
err = post.FillPath()
if err != nil {
return
}

dir := filepath.Dir(path)
dir := filepath.Dir(post.Path)
err = os.MkdirAll(dir, 0755)
if err != nil {
return
}

fmt.Printf("Make file: %s\n", path)
f, err := os.Create(path)
fmt.Printf("Make file: %s\n", post.Path)

f, err := os.Create(post.Path)
defer f.Close()
if err != nil {
return
Expand All @@ -193,14 +190,26 @@ func (post Post) Save() (err error) {
return
}

func (post Post) findPath() (path string, err error) {
dir, err := os.Getwd()
if err != nil {
func (post *Post) FillPath() (err error) {
if post.Path != "" {
return
}

if post.ID != "" {
path, err := post.findPath()
if err == nil {
post.Path = path
return err
}
}

post.Path = post.createPath()
return
}

func (post Post) findPath() (path string, err error) {
found := errors.New("found")
err = filepath.Walk(dir, func(p string, info os.FileInfo, e error) (err error) {
err = filepath.Walk(".", func(p string, info os.FileInfo, e error) (err error) {
if e != nil {
err = e
return
Expand All @@ -223,10 +232,11 @@ func (post Post) findPath() (path string, err error) {
}
return
})
if err == found {
err = nil
if err != found {
err = PathNotFoundError{}
return
}
return
return path, nil
}

func (post Post) createPath() (path string) {
Expand All @@ -249,7 +259,9 @@ func (post Post) createPath() (path string) {
_, err := os.Stat(path)
// no error means: a file exists at the path
// error occurs means: no file exists at the path
if err != nil { //TODO test me
if err != nil {
// no file at the path,
// so possible to create file with the path
break
}
filename += "-"
Expand Down Expand Up @@ -284,3 +296,10 @@ func (err EmptyIDError) Error() (msg string) {
msg = "empty ID"
return
}

type PathNotFoundError struct{}

func (err PathNotFoundError) Error() (msg string) {
msg = "path not found"
return
}
Loading

0 comments on commit 427664b

Please sign in to comment.