(ctx context.DnoteCtx, rowIDArg string)
| 112 | } |
| 113 | |
| 114 | func runNote(ctx context.DnoteCtx, rowIDArg string) error { |
| 115 | err := validateRunNoteFlags() |
| 116 | if err != nil { |
| 117 | return errors.Wrap(err, "validating flags.") |
| 118 | } |
| 119 | |
| 120 | rowID, err := strconv.Atoi(rowIDArg) |
| 121 | if err != nil { |
| 122 | return errors.Wrap(err, "invalid rowid") |
| 123 | } |
| 124 | |
| 125 | db := ctx.DB |
| 126 | note, err := database.GetActiveNote(db, rowID) |
| 127 | if err == sql.ErrNoRows { |
| 128 | return errors.Errorf("note %d not found", rowID) |
| 129 | } else if err != nil { |
| 130 | return errors.Wrap(err, "querying the book") |
| 131 | } |
| 132 | |
| 133 | content := contentFlag |
| 134 | |
| 135 | // If no flag was provided, launch an editor to get the content |
| 136 | if bookFlag == "" && contentFlag == "" { |
| 137 | c, err := getContent(ctx, note) |
| 138 | if err != nil { |
| 139 | return errors.Wrap(err, "getting content from editor") |
| 140 | } |
| 141 | |
| 142 | content = c |
| 143 | } |
| 144 | |
| 145 | tx, err := ctx.DB.Begin() |
| 146 | if err != nil { |
| 147 | return errors.Wrap(err, "beginning a transaction") |
| 148 | } |
| 149 | |
| 150 | err = updateNote(ctx, tx, note, bookFlag, content) |
| 151 | if err != nil { |
| 152 | tx.Rollback() |
| 153 | return errors.Wrap(err, "updating note fields") |
| 154 | } |
| 155 | |
| 156 | noteInfo, err := database.GetNoteInfo(tx, rowID) |
| 157 | if err != nil { |
| 158 | tx.Rollback() |
| 159 | return errors.Wrap(err, "getting note info") |
| 160 | } |
| 161 | |
| 162 | err = tx.Commit() |
| 163 | if err != nil { |
| 164 | tx.Rollback() |
| 165 | return errors.Wrap(err, "committing a transaction") |
| 166 | } |
| 167 | |
| 168 | log.Success("edited the note\n") |
| 169 | output.NoteInfo(os.Stdout, noteInfo) |
| 170 | |
| 171 | return nil |
no test coverage detected