-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (105 loc) · 2.6 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/spf13/cobra"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Tag struct {
ID uint `gorm:"primarykey"`
Name string `gorm:"unique;not null"`
}
type Note struct {
ID uint `gorm:"primarykey"`
Title *string
Summary *string
Body *string
Tags []Tag `gorm:"many2many:note_tags;"`
}
var db *gorm.DB
var dbFile string
var rootCmd = &cobra.Command{
Use: "z2",
Short: "z2 is a simple note-taking system",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initDB()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
dbClose()
},
}
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new note",
Run: createNote,
}
func initDB() {
var err error
db, err = gorm.Open(sqlite.Open(dbFile), &gorm.Config{})
if err != nil {
fmt.Println("Failed to connect to the database:", err)
os.Exit(1)
}
db.AutoMigrate(&Tag{}, &Note{})
}
func dbClose() {
sqlDB, err := db.DB()
if err != nil {
fmt.Println("Failed to close the database:", err)
os.Exit(1)
}
sqlDB.Close()
}
func init() {
// Set defaults from environment variables
defaultDBFile := os.Getenv("Z2_DB_FILE")
if defaultDBFile == "" {
defaultDBFile = "z2.db"
}
// Define flags
rootCmd.PersistentFlags().StringVar(&dbFile, "db", defaultDBFile, "Database file")
rootCmd.AddCommand(createCmd)
// Define flags for create command
createCmd.Flags().StringP("title", "t", "", "Title of the note")
createCmd.Flags().StringP("summary", "s", "", "Summary of the note")
createCmd.Flags().StringSliceP("tags", "g", []string{}, "Tags for the note")
createCmd.Flags().StringP("body-file", "b", "", "Path to the Markdown file for the note body")
}
func createNote(cmd *cobra.Command, args []string) {
title, _ := cmd.Flags().GetString("title")
summary, _ := cmd.Flags().GetString("summary")
tags, _ := cmd.Flags().GetStringSlice("tags")
bodyFilePath, _ := cmd.Flags().GetString("body-file")
if bodyFilePath == "" {
fmt.Println("Error: --body-file is required")
os.Exit(1)
}
body, err := ioutil.ReadFile(bodyFilePath)
if err != nil {
fmt.Printf("Error reading from body file: %s\n", err)
os.Exit(1)
}
bodyStr := string(body)
var noteTags []Tag
for _, tagName := range tags {
tag := Tag{Name: tagName}
db.FirstOrCreate(&tag, Tag{Name: tagName})
noteTags = append(noteTags, tag)
}
note := Note{
Title: &title,
Summary: &summary,
Body: &bodyStr,
Tags: noteTags,
}
db.Create(¬e)
fmt.Println("Note created successfully!")
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}