changeset 1:76695bcbe426

write cmd
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Tue, 01 Dec 2020 20:30:06 +0900
parents c0a01cfbf234
children d8156a544e6a
files client.go cmd.go cmd_pull.go cmd_push.go cmd_root.go config.go go.mod go.sum growsync.go util.go
diffstat 10 files changed, 247 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client.go	Tue Dec 01 20:30:06 2020 +0900
@@ -0,0 +1,52 @@
+package growsync
+
+import (
+	"context"
+	"io/ioutil"
+
+	"github.com/crowi/go-crowi"
+)
+
+type growClient struct {
+	client *crowi.Client
+}
+
+func NewGrowiClient(url, token string) (*growClient, error) {
+	client, err := crowi.NewClient(crowi.Config{URL: url, Token: token})
+	if err != nil {
+		return nil, err
+	}
+	gClient := growClient{
+		client: client,
+	}
+	return &gClient, nil
+}
+
+func (gClient *growClient) CheckIsExistsToken(path string) (bool, error) {
+	ctx := context.Background()
+	page, err := gClient.client.Pages.Get(ctx, path)
+	if err != nil {
+		return false, err
+	}
+	return page.OK, nil
+}
+
+func (gClient *growClient) CreateNewPage(path string, mdPATH string) error {
+	ctx := context.Background()
+	markdown, err := ioutil.ReadFile(mdPATH)
+	if err != nil {
+		return 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 err
+	}
+	_, err = gClient.client.Pages.Update(ctx, path, string(markdown))
+	return err
+}
--- a/cmd.go	Wed Apr 08 07:09:03 2020 +0900
+++ b/cmd.go	Tue Dec 01 20:30:06 2020 +0900
@@ -8,5 +8,5 @@
 type cmd interface {
 	name() string
 	description() string
-	run(context.Context, []string, io.Writer, io.Writer) error
+	run(context.Context, []string, *growiConfig, io.Writer, io.Writer) error
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd_pull.go	Tue Dec 01 20:30:06 2020 +0900
@@ -0,0 +1,20 @@
+package growsync
+
+import (
+	"context"
+	"io"
+)
+
+type pullCmd struct{}
+
+func (pc *pullCmd) name() string {
+	return "pull"
+}
+
+func (pc *pullCmd) description() string {
+	return "pull from growi web app"
+}
+
+func (pc *pullCmd) run(context.Context, []string, io.Writer, io.Writer) error {
+	return nil
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd_push.go	Tue Dec 01 20:30:06 2020 +0900
@@ -0,0 +1,49 @@
+package growsync
+
+import (
+	"context"
+	"flag"
+	"fmt"
+	"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)
+	isExists, err := client.CheckIsExistsToken(growiSystemPath)
+	fmt.Println(isExists)
+
+	if isExists {
+		return client.UpdatePage(growiSystemPath, markdownPATH)
+	}
+
+	return client.CreateNewPage(growiSystemPath, markdownPATH)
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd_root.go	Tue Dec 01 20:30:06 2020 +0900
@@ -0,0 +1,20 @@
+package growsync
+
+import (
+	"context"
+	"io"
+)
+
+type rootCmd struct{}
+
+func (pc *rootCmd) name() string {
+	return "root"
+}
+
+func (pc *rootCmd) description() string {
+	return "root from growi web app"
+}
+
+func (pc *rootCmd) run(context.Context, []string, io.Writer, io.Writer) error {
+	return nil
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.go	Tue Dec 01 20:30:06 2020 +0900
@@ -0,0 +1,51 @@
+package growsync
+
+import (
+	"fmt"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+
+	"github.com/goccy/go-yaml"
+)
+
+type growiConfig struct {
+	url       string `yaml:"growi_url"`
+	userName  string `yaml:"user_name"`
+	token     string `yaml:"token"`
+	localRoot string `yaml:"local_root"`
+}
+
+func parseConfig() (*growiConfig, error) {
+	configFilePATH, _ := getConfingPATH()
+	configBytes, err := ioutil.ReadFile(configFilePATH)
+	if err != nil {
+		return nil, err
+	}
+	config := growiConfig{}
+
+	if err := yaml.Unmarshal(configBytes, &config); err != nil {
+		return nil, err
+	}
+	return &config, nil
+}
+
+func getConfingPATH() (string, error) {
+	home, err := os.UserHomeDir()
+	if err != nil {
+		return "", err
+	}
+	configFilePATH := filepath.Join(home, ".config", "growsync", "config.yaml")
+	if !fileCheck(configFilePATH) {
+		return "", fmt.Errorf("[ERROR] conf file not found")
+	}
+	return configFilePATH, nil
+}
+
+func fileCheck(conf string) bool {
+	info, err := os.Stat(conf)
+	if os.IsNotExist(err) {
+		return false
+	}
+	return !info.IsDir()
+}
--- a/go.mod	Wed Apr 08 07:09:03 2020 +0900
+++ b/go.mod	Tue Dec 01 20:30:06 2020 +0900
@@ -1,5 +1,10 @@
 module www.cr.ie.u-ryukyu.ac.jp/hg/Members/anatofuz/growsync
 
-go 1.13
+go 1.15
 
-require github.com/pkg/errors v0.9.1
+require (
+	github.com/crowi/go-crowi v0.0.0-20170809061107-ef04e39ae1ac
+	github.com/pkg/errors v0.9.1
+	golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
+	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
+)
--- a/go.sum	Wed Apr 08 07:09:03 2020 +0900
+++ b/go.sum	Tue Dec 01 20:30:06 2020 +0900
@@ -1,2 +1,17 @@
+github.com/crowi/go-crowi v0.0.0-20170809061107-ef04e39ae1ac h1:52GTmB0wFiTZTKBG/n1g84xum2dL5lkhN1gXtY3xRSo=
+github.com/crowi/go-crowi v0.0.0-20170809061107-ef04e39ae1ac/go.mod h1:fHmF2Li4ysI3D5EXN3eUO4FEEumXn+S6DUrl/F6Gxlg=
 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
 github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
+golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
--- a/growsync.go	Wed Apr 08 07:09:03 2020 +0900
+++ b/growsync.go	Tue Dec 01 20:30:06 2020 +0900
@@ -13,7 +13,7 @@
 const cmdName = "growsync"
 
 var (
-	subCommands       = []cmd{}
+	subCommands       = []cmd{&pushCmd{}}
 	dispatch          = make(map[string]cmd, len(subCommands))
 	maxSubcommandName int
 )
@@ -58,7 +58,12 @@
 	if !ok {
 		return errors.Errorf("unknown subcommand: %s", argv[0])
 	}
-	return rnr.run(context.Background(), argv[1:], outStream, errStream)
+
+	config, err := parseConfig()
+	if err != nil {
+		return err
+	}
+	return rnr.run(context.Background(), argv[1:], config, outStream, errStream)
 }
 
 func printVersion(out io.Writer) error {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util.go	Tue Dec 01 20:30:06 2020 +0900
@@ -0,0 +1,25 @@
+package growsync
+
+import (
+	"path/filepath"
+	"strings"
+)
+
+func convertGrowiSystemPath(inputPath string) string {
+	/*
+		iputPath: Gears -> /Gears
+		inputPath: Gears/CbC/hoge.md -> /Gears/CbC/hoge
+	*/
+
+	outputPATH := inputPath
+
+	if index := strings.Index(inputPath, "/"); index != 0 {
+		outputPATH = "/" + inputPath
+	}
+
+	if strings.HasSuffix(outputPATH, ".md") {
+		outputPATH = strings.TrimSuffix(outputPATH, filepath.Ext(outputPATH))
+	}
+
+	return outputPATH
+}