add ability to create a new document

This commit is contained in:
Aaron Bieber 2020-04-15 07:38:25 -06:00
parent 6908fca110
commit 8b70804c6e
3 changed files with 122 additions and 14 deletions

View File

@ -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
}

View File

@ -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

View File

@ -17,7 +17,7 @@ func main() {
)
root := &ffcli.Command{
ShortUsage: "cromp <subcommand>",
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
},
}