view cmd_push.go @ 5:af840bc25791

impl edit cmd
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Fri, 04 Dec 2020 18:27:09 +0900
parents 3032e9f78e4b
children c775cee5aac2
line wrap: on
line source

package growsync

import (
	"context"
	"flag"
	"io"

	"golang.org/x/xerrors"
)

type pushCmd struct{}

func (pc *pushCmd) name() string {
	return "push"
}

func (pc *pushCmd) description() string {
	return "push from growi web app"
}

func (pc *pushCmd) run(ctx context.Context, argv []string, config *growiConfig, stdWriter io.Writer, errorWriter io.Writer) error {
	fs := flag.NewFlagSet("growsync push", flag.ContinueOnError)
	fs.SetOutput(errorWriter)

	if err := fs.Parse(argv); err != nil {
		return nil
	}

	if fs.NArg() < 1 {
		return xerrors.New("usage: growsync push [md file]")
	}

	client, err := NewGrowiClient(config.URL, config.TOKEN)
	if err != nil {
		return err
	}

	markdownPATH := fs.Arg(0)
	growiSystemPath := convertGrowiSystemPath(markdownPATH)
	id, err := client.IsExistsPageOnGrowi(growiSystemPath)

	if err != nil {
		return err
	}

	if id == nil {
		return client.CreateNewPage(growiSystemPath, markdownPATH)
	}

	return client.UpdatePage(growiSystemPath, markdownPATH)
}