MCPcopy Index your code
hub / github.com/dnote/dnote / runBook

Function runBook

pkg/cli/cmd/remove/remove.go:165–212  ·  view source on GitHub ↗
(ctx context.DnoteCtx, bookLabel string)

Source from the content-addressed store, hash-verified

163}
164
165func runBook(ctx context.DnoteCtx, bookLabel string) error {
166 db := ctx.DB
167
168 bookUUID, err := database.GetBookUUID(db, bookLabel)
169 if err != nil {
170 return errors.Wrap(err, "finding book uuid")
171 }
172
173 ok, err := maybeConfirm(fmt.Sprintf("delete book '%s' and all its notes?", bookLabel), false)
174 if err != nil {
175 return errors.Wrap(err, "getting confirmation")
176 }
177 if !ok {
178 log.Warnf("aborted by user\n")
179 return nil
180 }
181
182 tx, err := db.Begin()
183 if err != nil {
184 return errors.Wrap(err, "beginning a transaction")
185 }
186
187 if _, err = tx.Exec("UPDATE notes SET deleted = ?, dirty = ?, body = ? WHERE book_uuid = ?", true, true, "", bookUUID); err != nil {
188 tx.Rollback()
189 return errors.Wrap(err, "removing notes in the book")
190 }
191
192 // override the label with a random string
193 uniqLabel, err := utils.GenerateUUID()
194 if err != nil {
195 return errors.Wrap(err, "generating uuid to override with")
196 }
197
198 if _, err = tx.Exec("UPDATE books SET deleted = ?, dirty = ?, label = ? WHERE uuid = ?", true, true, uniqLabel, bookUUID); err != nil {
199 tx.Rollback()
200 return errors.Wrap(err, "removing the book")
201 }
202
203 err = tx.Commit()
204 if err != nil {
205 tx.Rollback()
206 return errors.Wrap(err, "committing transaction")
207 }
208
209 log.Success("removed book\n")
210
211 return nil
212}

Callers 1

newRunFunction · 0.70

Calls 9

GetBookUUIDFunction · 0.92
WarnfFunction · 0.92
GenerateUUIDFunction · 0.92
SuccessFunction · 0.92
maybeConfirmFunction · 0.85
BeginMethod · 0.65
ExecMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65

Tested by

no test coverage detected