/
runtime.go
52 lines (44 loc) · 1.43 KB
/
runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bytes"
"fmt"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/bagaking/ankibuild/apkg"
)
// updateCardRuntime Updates the runtime information of the configuration with the IDs of the created note and card.
func updateCardRuntime(cardConf QnACard, note *apkg.Note, card *apkg.Card) (*QnACard, error) {
if cardConf.Question != note.Front() {
return nil, fmt.Errorf("write card runtime back failed, cardConf= %v", cardConf)
}
cardConf.Runtime = CardRuntime{
NoteID: note.ID,
NoteGUID: note.Guid,
CardID: card.ID,
}
return &cardConf, nil
}
// writeRuntimeBack writes the runtime information back into the original .apkg.toml configuration file.
// writeRuntimeBack writes the runtime information back into the original .apkg.toml configuration file with a backup.
func writeRuntimeBack(conf *Barn, filePath string) error {
// Generate the backup file name with timestamp
timeStamp := time.Now().Format("20060102150405")
backupFilePath := filePath + "." + timeStamp + ".bak"
// Read original file
original, err := os.ReadFile(filePath)
if err != nil {
return err
}
// Write to the backup file
if err = os.WriteFile(backupFilePath, original, 0644); err != nil {
return err
}
// Encode the updated configuration
var buf bytes.Buffer
if err = toml.NewEncoder(&buf).Encode(conf); err != nil {
return err
}
// Write back to the original file
return os.WriteFile(filePath, buf.Bytes(), 0644)
}