Mercurial > hg > Members > anatofuz > growsync
view cmd_edit.go @ 14:1e21b0c29775
remove create page msg
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 17 Dec 2020 11:12:04 +0900 |
parents | c775cee5aac2 |
children | 0dc44ee170b4 |
line wrap: on
line source
package growsync import ( "context" "flag" "fmt" "io" "os" "path/filepath" "time" "golang.org/x/xerrors" ) type editCmd struct{} const layout string = "2006/01/02" func (pc *editCmd) name() string { return "edit" } func (pc *editCmd) description() string { return "edit from growi web app" } func (pc *editCmd) run(ctx context.Context, argv []string, config *growiConfig, stdWriter io.Writer, errorWriter io.Writer) error { fs := flag.NewFlagSet("growsync edit", flag.ContinueOnError) fs.SetOutput(errorWriter) if err := fs.Parse(argv); err != nil { return xerrors.Errorf("[error] failed parse argv %+w", err) } var growiPATH string if fs.NArg() < 1 { growiPATH = createNewDailyMarkdownPATH(config.DailyPATH) } else { growiPATH = convertGrowiSystemPath(fs.Arg(0)) } localFilePATH := filepath.Join(config.LocalRoot, growiPATH+".md") var beforeTime time.Time alreadyExistsFile := existsFile(localFilePATH) if alreadyExistsFile { info, err := os.Stat(localFilePATH) if err != nil { return xerrors.Errorf("[error] failed file stat %s %+w", localFilePATH, err) } beforeTime = info.ModTime() } client, err := NewGrowiClient(config.URL, config.TOKEN) if err != nil { return xerrors.Errorf("[error] failed growi client %+w", err) } err = doEdit(localFilePATH) if err != nil { return xerrors.Errorf("failed edit mardkwodn file %+w", err) } if alreadyExistsFile { info, err := os.Stat(localFilePATH) if err != nil { return xerrors.Errorf("[error] failed file stat %s %+w", localFilePATH, err) } if info.ModTime() == beforeTime { fmt.Printf("[info] %s not update\n", growiPATH) return nil } } return client.UpdatePage(growiPATH, localFilePATH) } func createNewDailyMarkdownPATH(dailyPATH string) string { now := time.Now() return filepath.Join(dailyPATH, now.Format(layout)) }