Mercurial > hg > Members > anatofuz > growsync
view client.go @ 15:0dc44ee170b4
mkdir
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 03 Jan 2021 08:46:08 +0900 |
parents | 1e21b0c29775 |
children |
line wrap: on
line source
package growsync import ( "context" "errors" "fmt" "io/ioutil" "golang.org/x/xerrors" "www.cr.ie.u-ryukyu.ac.jp/hg/Members/anatofuz/growsync/client" ) type growClient struct { client *client.Client } func NewGrowiClient(url, token string) (*growClient, error) { client, err := client.NewClient(client.Config{URL: url, Token: token}) if err != nil { return nil, xerrors.Errorf("failed NweGrowiClient %+w", err) } gClient := growClient{ client: client, } return &gClient, nil } func (gClient *growClient) IsExistsPageOnGrowi(path string) (*client.Page, error) { ctx := context.Background() page, err := gClient.client.Pages.Get(ctx, path) if errors.Is(err, client.ErrorPageNotFOund) { return nil, nil } if err != nil { return nil, xerrors.Errorf("failed isExistsPageOnGrowi at %+w", err) } return page, nil } func (gClient *growClient) CreateNewPage(path string, mdPATH string) error { ctx := context.Background() markdown, err := ioutil.ReadFile(mdPATH) if err != nil { return xerrors.Errorf("failed read %s file %+w", mdPATH, err) } _, err = gClient.client.Pages.Create(ctx, path, string(markdown)) return err } func (gClient *growClient) UpdatePage(path string, mdPATH string) error { ctx := context.Background() markdown, err := ioutil.ReadFile(mdPATH) if err != nil { return xerrors.Errorf("failed read %s file %+w", mdPATH, err) } page, err := gClient.IsExistsPageOnGrowi(path) if err != nil { return err } if page == nil { fmt.Println("[info] create new page", mdPATH) // WIP create directory return gClient.CreateNewPage(path, mdPATH) } _, err = gClient.client.Pages.Update(ctx, page.ID, page.Revision.ID, string(markdown)) return err }