(ctx context.DnoteCtx)
| 103 | } |
| 104 | |
| 105 | func newRun(ctx context.DnoteCtx) infra.RunEFunc { |
| 106 | return func(cmd *cobra.Command, args []string) error { |
| 107 | bookName := args[0] |
| 108 | if err := validate.BookName(bookName); err != nil { |
| 109 | return errors.Wrap(err, "invalid book name") |
| 110 | } |
| 111 | |
| 112 | content, err := getContent(ctx) |
| 113 | if err != nil { |
| 114 | return errors.Wrap(err, "getting content") |
| 115 | } |
| 116 | if content == "" { |
| 117 | return errors.New("Empty content") |
| 118 | } |
| 119 | |
| 120 | ts := time.Now().UnixNano() |
| 121 | noteRowID, err := writeNote(ctx, bookName, content, ts) |
| 122 | if err != nil { |
| 123 | return errors.Wrap(err, "Failed to write note") |
| 124 | } |
| 125 | |
| 126 | log.Successf("added to %s\n", bookName) |
| 127 | |
| 128 | db := ctx.DB |
| 129 | info, err := database.GetNoteInfo(db, noteRowID) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | |
| 134 | output.NoteInfo(os.Stdout, info) |
| 135 | |
| 136 | if err := upgrade.Check(ctx); err != nil { |
| 137 | log.Error(errors.Wrap(err, "automatically checking updates").Error()) |
| 138 | } |
| 139 | |
| 140 | return nil |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func writeNote(ctx context.DnoteCtx, bookLabel string, content string, ts int64) (int, error) { |
| 145 | tx, err := ctx.DB.Begin() |
no test coverage detected