From 8b70804c6e40fbaf3b53e6fd9ee370ba96d3b685 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Wed, 15 Apr 2020 07:38:25 -0600 Subject: [PATCH] add ability to create a new document --- cmd/cromp/editor.go | 119 +++++++++++++++++++++++++++++++++++++++----- cmd/cromp/login.go | 6 +++ cmd/cromp/main.go | 11 +++- 3 files changed, 122 insertions(+), 14 deletions(-) diff --git a/cmd/cromp/editor.go b/cmd/cromp/editor.go index 0f31121..d129e7f 100644 --- a/cmd/cromp/editor.go +++ b/cmd/cromp/editor.go @@ -7,10 +7,12 @@ import ( "io/ioutil" "os" "os/exec" + "strings" "github.com/google/uuid" "github.com/peterbourgon/ff/v2/ffcli" "suah.dev/cromp/db" + cromp "suah.dev/cromp/internal" ) // NewEditor makes the "cromp edit" command @@ -33,35 +35,108 @@ func NewEditor() *ffcli.Command { return err } - fmt.Println(*updatedEntry) + fmt.Printf("%#v\n", updatedEntry) return nil }, } } -// OpenInEditor opens a cromp doc in $EDITOR -func OpenInEditor(id uuid.UUID) (*string, error) { - var err error - var params db.GetEntryParams - resp := &db.Entry{} +// NewDoc opens a new doc in $EDITOR +func NewDoc() (*uuid.UUID, error) { + var s string + entry := &db.CreateEntryParams{} + editor, args := editorCmd() + + entry.EntryID = uuid.New() + + f, err := ioutil.TempFile("", fmt.Sprintf("cromp-%s", entry.EntryID.String())) + if err != nil { + return nil, err + } + + args = append(args, f.Name()) + + defer os.Remove(f.Name()) + + // TODO populate with a user defined template here + f.Write([]byte(entry.Body)) + + cmd := exec.Command(editor, args...) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err = cmd.Run() + if err != nil { + return nil, err + } + + b, err := ioutil.ReadFile(f.Name()) + if err != nil { + return nil, err + } + s = string(b) + + header, err := cromp.ParseHeader(s) + if err != nil { + return nil, err + } + + entry.Title = header.Title + entry.Body = s + + result := make(map[string]string) + + err = Post("/entries/add", entry, result) + if err != nil { + return nil, err + } + + fmt.Printf("%#v\n", result) + + return &entry.EntryID, nil +} + +func editorCmd() (string, []string) { editor := "vi" - s := "" - - params.EntryID = id - if c := os.Getenv("EDITOR"); c != "" { editor = c } + + var args []string + if pos := strings.Index(editor, " "); pos != -1 { + args = strings.Fields(editor[pos+1:]) + editor = editor[:pos] + } + + return editor, args +} + +// OpenInEditor opens an existing cromp doc in $EDITOR +func OpenInEditor(id uuid.UUID) (*string, error) { + var err error + var params db.GetEntryParams + var saveParams db.UpdateEntryParams + + s := "" + resp := &db.Entry{} + + params.EntryID = id + err = Post("/entries/get", params, resp) if err != nil { return nil, err } + cmd, args := editorCmd() + if f, err := ioutil.TempFile("", fmt.Sprintf("cromp-%s", resp.EntryID.String())); err == nil { + args = append(args, f.Name()) + defer os.Remove(f.Name()) f.Write([]byte(resp.Body)) - cmd := exec.Command(editor, f.Name()) + + cmd := exec.Command(cmd, args...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -76,5 +151,25 @@ func OpenInEditor(id uuid.UUID) (*string, error) { s = string(b) } - return &s, err + header, err := cromp.ParseHeader(s) + if err != nil { + return nil, err + } + + fmt.Printf("%#v\n", header) + + saveParams.EntryID = params.EntryID + saveParams.Body = s + saveParams.Title = header.Title + + result := make(map[string]string) + + err = Post("/entries/update", saveParams, result) + if err != nil { + return nil, err + } + + fmt.Printf("%#v\n", result) + + return nil, err } diff --git a/cmd/cromp/login.go b/cmd/cromp/login.go index cdbf396..c5a3a1e 100644 --- a/cmd/cromp/login.go +++ b/cmd/cromp/login.go @@ -31,6 +31,12 @@ func NewLogin() *ffcli.Command { return fmt.Errorf("please specify -url") } + cfg.URL = *urlConfFS + err = cfg.WriteConfig() + if err != nil { + return err + } + user, err := Prompt("Login: ") if err != nil { return err diff --git a/cmd/cromp/main.go b/cmd/cromp/main.go index 53d0791..b77b670 100644 --- a/cmd/cromp/main.go +++ b/cmd/cromp/main.go @@ -17,7 +17,7 @@ func main() { ) root := &ffcli.Command{ - ShortUsage: "cromp ", + ShortUsage: "cromp", FlagSet: rootFlagSet, Subcommands: []*ffcli.Command{ NewConfig(), @@ -29,7 +29,14 @@ func main() { NewSimilar(), }, Exec: func(context.Context, []string) error { - return flag.ErrHelp + entryID, err := NewDoc() + if err != nil { + return err + } + + fmt.Printf("Created entry: %s\n", entryID.String()) + + return nil }, }