diff --git a/README.md b/README.md index 4fb2b70..e73d7b3 100644 --- a/README.md +++ b/README.md @@ -23,14 +23,14 @@ $ brew install qiisync ### CentOS ``` -$ sudo rpm -ivh https://github.com/d-tsuji/qiisync/releases/download/v0.0.1/qiisync_0.0.1_Tux-64-bit.rpm +$ sudo rpm -ivh https://github.com/d-tsuji/qiisync/releases/download/v0.0.3/qiisync_0.0.3_Tux-64-bit.rpm ``` ### Debian, Ubuntu ``` -$ wget https://github.com/d-tsuji/qiisync/releases/download/v0.0.1/qiisync_0.0.1_Tux-64-bit.deb -$ sudo dpkg -i qiisync_0.0.1_Tux-64-bit.deb +$ wget https://github.com/d-tsuji/qiisync/releases/download/v0.0.3/qiisync_0.0.3_Tux-64-bit.deb +$ sudo dpkg -i qiisync_0.0.3_Tux-64-bit.deb ``` ### Golang diff --git a/broker.go b/broker.go index 2f9674e..26be3e6 100644 --- a/broker.go +++ b/broker.go @@ -352,7 +352,13 @@ func (b *Broker) patchArticle(body *PostItem) error { if resp.StatusCode != http.StatusOK { return errors.New(resp.Status) } - Logf("post", "fresh article ---> %s", body.URL) + + var item Item + if err := json.NewDecoder(resp.Body).Decode(&item); err != nil { + return err + } + + Logf("post", "fresh article ---> %s", item.URL) return nil } @@ -365,10 +371,14 @@ func (b *Broker) UploadFresh(a *Article) (bool, error) { } if a.Item.UpdatedAt.After(ra.Item.UpdatedAt) == false { - Logf("", "Article is not updated. remote=%s > local=%s", ra.Item.UpdatedAt, a.Item.UpdatedAt) + Logf("", "article is not updated. remote=%s > local=%s", ra.Item.UpdatedAt, a.Item.UpdatedAt) return false, nil } + if a.Private && !ra.Private { + return false, errors.New("once an article has been published, it cannot be privately published") + } + body := &PostItem{ Body: a.Item.Body, Private: a.Private, diff --git a/broker_test.go b/broker_test.go index c11ee76..b6f4b86 100644 --- a/broker_test.go +++ b/broker_test.go @@ -838,6 +838,39 @@ func TestPatchArticleNoID(t *testing.T) { } } +func TestPatchInvalidResponse(t *testing.T) { + b, mux, _, teardown := setup() + t.Cleanup(func() { + teardown() + if err := os.RemoveAll(b.baseDir()); err != nil { + t.Errorf("remove tempDir: %v", err) + } + }) + + mux.HandleFunc("/api/v2/items/c686397e4a0f4f11683d", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PATCH") + w.WriteHeader(http.StatusOK) + fmt.Fprint(w, `[{}]`) + }) + + err := b.patchArticle(&PostItem{ + Body: "# Example", + Private: false, + Tags: []*Tag{ + { + Name: "Ruby", + Versions: []string{"0.0.1"}, + }, + }, + Title: "Example title", + ID: "c686397e4a0f4f11683d", + }) + if err == nil { + t.Errorf("expected error occured if cannot decode json") + return + } +} + func Test_fetchLocalArticles(t *testing.T) { updateAt := time.Date(2020, 4, 22, 16, 59, 59, 0, time.UTC) @@ -959,6 +992,24 @@ func TestUploadFresh(t *testing.T) { want: false, wantErr: false, }, + { + name: "cannot_private", + localArticle: &Article{ + ArticleHeader: &ArticleHeader{ + ID: "c686397e4a0f4f11683d", + Title: "Update title", + Tags: "Go:1.14", + Author: "d-tsuji", + Private: true, + }, + Item: &Item{ + Body: "# Update Example", + UpdatedAt: time.Date(2020, 4, 23, 05, 41, 36, 0, time.UTC), + }, + }, + want: false, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/cmd/qiisync/main.go b/cmd/qiisync/main.go index dfb11bb..83c02aa 100644 --- a/cmd/qiisync/main.go +++ b/cmd/qiisync/main.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "errors" "fmt" "os" "strconv" @@ -72,31 +71,48 @@ var commandPost = &cli.Command{ // Receives parameters from the stdin. sc := bufio.NewScanner(os.Stdin) + var ( + title string + tag string + private bool + ) fmt.Fprintln(os.Stdout, "") fmt.Fprintln(os.Stdout, `Please enter the "title" of the Article you want to post.`) - _ = sc.Scan() - title := sc.Text() - if title == "" { - return fmt.Errorf("title is required") + if sc.Scan() { + title = sc.Text() + if title == "" { + return fmt.Errorf("title is required") + } + } + if err := sc.Err(); err != nil { + return fmt.Errorf("an unexpected error has occurred when scanning: %w", err) } fmt.Fprintln(os.Stdout, "") fmt.Fprintln(os.Stdout, `Please enter the "tag" of the Article you want to post.`) fmt.Fprintln(os.Stdout, `Tag is like "React,redux,TypeScript" or "Go" or "Python:3.7". To specify more than one, separate them with ",".`) - _ = sc.Scan() - tag := sc.Text() - if tag == "" { - return fmt.Errorf("more than one tag is required") + if sc.Scan() { + tag = sc.Text() + if tag == "" { + return fmt.Errorf("more than one tag is required") + } + } + if err := sc.Err(); err != nil { + return fmt.Errorf("an unexpected error has occurred when scanning: %w", err) } fmt.Fprintln(os.Stdout, "") fmt.Fprintln(os.Stdout, `Do you make the Article you post private? "true" is private, "false" is public.`) - _ = sc.Scan() - text := sc.Text() - private, err := strconv.ParseBool(text) - if err != nil { - return fmt.Errorf("input string (%s) could not be parsed into bool", text) + if sc.Scan() { + text := sc.Text() + private, err = strconv.ParseBool(text) + if err != nil { + return fmt.Errorf("input string (%s) could not be parsed into bool", text) + } + } + if err := sc.Err(); err != nil { + return fmt.Errorf("an unexpected error has occurred when scanning: %w", err) } a, err := qiisync.ArticleFromFile(filename) @@ -140,11 +156,6 @@ var commandUpdate = &cli.Command{ return err } - if !a.Private { - return errors.New("Once published, an Article cannot be made a private publication.\n" + - "\tPlease check if the Private item in the header of the Article is set to false.") - } - b := qiisync.NewBroker(conf) _, err = b.UploadFresh(a) if err != nil { diff --git a/version.go b/version.go index 8ce377a..913589a 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package qiisync // Version is the version to be displayed in the command line help message. -const Version = "0.0.2" +const Version = "0.0.3"