changeset 3:3032e9f78e4b

add new cmd
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Tue, 01 Dec 2020 21:30:45 +0900
parents d8156a544e6a
children 359eff175bf1
files README.md cmd_new.go cmd_push.go cmd_root.go config.go growsync.go
diffstat 6 files changed, 75 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/README.md	Tue Dec 01 20:31:02 2020 +0900
+++ b/README.md	Tue Dec 01 21:30:45 2020 +0900
@@ -27,6 +27,18 @@
 % go get github.com/AnaTofuZ/growsync
 ```
 
+## CONFFILE
+
+`$HOME/.config/growsync/config.yaml`
+
+```yaml
+growi_url: https://growi.cr.ie.u-ryukyu.ac.jp
+user_name: AnaTofuZ
+token: hogehoge
+local_root: /Users/anatofuz/Documents/cr-growi
+daily_path: /user/anatofuz/note
+```
+
 ## Author
 
 [AnaTofuZ](https://github.com/AnaTofuZ)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd_new.go	Tue Dec 01 21:30:45 2020 +0900
@@ -0,0 +1,52 @@
+package growsync
+
+import (
+	"context"
+	"flag"
+	"fmt"
+	"io"
+	"path/filepath"
+	"time"
+)
+
+type newCmd struct{}
+
+const layout string = "2006/01/02"
+
+func (pc *newCmd) name() string {
+	return "new"
+}
+
+func (pc *newCmd) description() string {
+	return "new from growi web app"
+}
+
+func (pc *newCmd) run(ctx context.Context, argv []string, config *growiConfig, stdWriter io.Writer, errorWriter io.Writer) error {
+	fs := flag.NewFlagSet("growsync new", flag.ContinueOnError)
+	fs.SetOutput(errorWriter)
+
+	if err := fs.Parse(argv); err != nil {
+		return nil
+	}
+
+	var newMarkdownPATH string
+
+	if fs.NArg() < 1 {
+		newMarkdownPATH = createNewDailyMarkdownPATH(config.DailyPATH)
+	} else {
+		newMarkdownPATH = fs.Arg(0)
+	}
+
+	_, err := NewGrowiClient(config.URL, config.TOKEN)
+	if err != nil {
+		return err
+	}
+
+	fmt.Println(newMarkdownPATH)
+	return nil
+}
+
+func createNewDailyMarkdownPATH(dailyPATH string) string {
+	now := time.Now()
+	return filepath.Join(dailyPATH, now.Format(layout))
+}
--- a/cmd_push.go	Tue Dec 01 20:31:02 2020 +0900
+++ b/cmd_push.go	Tue Dec 01 21:30:45 2020 +0900
@@ -31,7 +31,7 @@
 		return xerrors.New("usage: growsync push [md file]")
 	}
 
-	client, err := NewGrowiClient(config.url, config.token)
+	client, err := NewGrowiClient(config.URL, config.TOKEN)
 	if err != nil {
 		return err
 	}
--- a/cmd_root.go	Tue Dec 01 20:31:02 2020 +0900
+++ b/cmd_root.go	Tue Dec 01 21:30:45 2020 +0900
@@ -2,6 +2,7 @@
 
 import (
 	"context"
+	"fmt"
 	"io"
 )
 
@@ -12,9 +13,10 @@
 }
 
 func (pc *rootCmd) description() string {
-	return "root from growi web app"
+	return "show document root"
 }
 
-func (pc *rootCmd) run(context.Context, []string, io.Writer, io.Writer) error {
+func (pc *rootCmd) run(ctx context.Context, args []string, config *growiConfig, outputWriter io.Writer, errorWriter io.Writer) error {
+	fmt.Fprintln(outputWriter, config.LocalRoot)
 	return nil
 }
--- a/config.go	Tue Dec 01 20:31:02 2020 +0900
+++ b/config.go	Tue Dec 01 21:30:45 2020 +0900
@@ -10,10 +10,11 @@
 )
 
 type growiConfig struct {
-	url       string `yaml:"growi_url"`
-	userName  string `yaml:"user_name"`
-	token     string `yaml:"token"`
-	localRoot string `yaml:"local_root"`
+	URL       string `yaml:"growi_url"`
+	UserName  string `yaml:"user_name"`
+	TOKEN     string `yaml:"token"`
+	LocalRoot string `yaml:"local_root"`
+	DailyPATH string `yaml:"daily_path"`
 }
 
 func parseConfig() (*growiConfig, error) {
--- a/growsync.go	Tue Dec 01 20:31:02 2020 +0900
+++ b/growsync.go	Tue Dec 01 21:30:45 2020 +0900
@@ -13,7 +13,7 @@
 const cmdName = "growsync"
 
 var (
-	subCommands       = []cmd{&pushCmd{}}
+	subCommands       = []cmd{&newCmd{}, &pushCmd{}, &rootCmd{}}
 	dispatch          = make(map[string]cmd, len(subCommands))
 	maxSubcommandName int
 )