-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
150 lines (131 loc) · 4.28 KB
/
main.go
File metadata and controls
150 lines (131 loc) · 4.28 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"path/filepath"
tea "github.com/charmbracelet/bubbletea"
"github.com/sheenazien8/sq/app"
"github.com/sheenazien8/sq/config"
"github.com/sheenazien8/sq/drivers"
"github.com/sheenazien8/sq/internal/version"
"github.com/sheenazien8/sq/logger"
"github.com/sheenazien8/sq/storage"
)
func main() {
// Parse command line flags
versionFlag := flag.Bool("version", false, "Show version information")
versionShort := flag.Bool("v", false, "Show version information (short)")
// Connection creation flags
createConnFlag := flag.Bool("create-connection", false, "Create a new database connection")
connDriver := flag.String("driver", drivers.DriverTypeMySQL, "Database driver (mysql, postgresql, sqlite)")
connName := flag.String("name", "", "Connection name")
connHost := flag.String("host", "localhost", "Database host")
connPort := flag.String("port", "3306", "Database port")
connUser := flag.String("user", "", "Database user")
connPass := flag.String("password", "", "Database password")
connDB := flag.String("database", "", "Database name or SQLite file path")
flag.Parse()
// Handle version flag
if *versionFlag || *versionShort {
fmt.Printf("sq version %s\n", version.Version)
os.Exit(0)
}
configDir, err := config.ConfigDir()
if err == nil {
os.MkdirAll(configDir, 0755)
logPath := filepath.Join(configDir, "debug.log")
if err := logger.SetFile(logPath); err != nil {
fmt.Println("Failed to setup logger:", err)
}
}
// Handle create connection flag
if *createConnFlag {
if err := handleCreateConnection(*connDriver, *connName, *connHost, *connPort, *connUser, *connPass, *connDB); err != nil {
fmt.Printf("Failed to create connection: %v\n", err)
os.Exit(1)
}
fmt.Println("Connection created successfully!")
os.Exit(0)
}
if os.Getenv("DEBUG") == "true" {
logger.SetLevel(slog.LevelDebug)
} else {
logger.SetLevel(slog.LevelInfo)
}
logger.Info("Application started", nil)
// Initialize app storage (SQLite database)
if err := storage.Init(); err != nil {
logger.Error("Failed to initialize storage", map[string]any{"error": err.Error()})
fmt.Println("Failed to initialize storage:", err)
os.Exit(1)
}
defer storage.Close()
p := tea.NewProgram(
app.New(),
tea.WithAltScreen(),
)
if _, err := p.Run(); err != nil {
os.Exit(1)
}
}
// handleCreateConnection creates a new database connection from CLI flags
func handleCreateConnection(driver, name, host, port, user, password, database string) error {
// Validate driver
supportedDrivers := map[string]bool{
drivers.DriverTypeMySQL: true,
drivers.DriverTypePostgreSQL: true,
drivers.DriverTypeSQLite: true,
}
if !supportedDrivers[driver] {
return fmt.Errorf("Unsupported driver: %s. Supported: mysql, postgresql, sqlite.", driver)
}
// Validate required fields
if name == "" {
return fmt.Errorf("Missing required flag: --name.")
}
if database == "" {
return fmt.Errorf("Missing required flag: --database.")
}
// Validate driver-specific fields
switch driver {
case drivers.DriverTypeSQLite:
// SQLite only needs name and file path
case drivers.DriverTypeMySQL, drivers.DriverTypePostgreSQL:
// MySQL and PostgreSQL need user and database
if user == "" {
return fmt.Errorf("Missing required flag: --user.")
}
}
// Initialize storage
if err := storage.Init(); err != nil {
return fmt.Errorf("Failed to initialize storage: %w", err)
}
defer storage.Close()
// Build connection URL based on driver
var url string
switch driver {
case drivers.DriverTypeMySQL:
if password == "" {
url = fmt.Sprintf("mysql://%s@%s:%s/%s", user, host, port, database)
} else {
url = fmt.Sprintf("mysql://%s:%s@%s:%s/%s", user, password, host, port, database)
}
case drivers.DriverTypePostgreSQL:
if password == "" {
url = fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", user, host, port, database)
} else {
url = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", user, password, host, port, database)
}
case drivers.DriverTypeSQLite:
// SQLite URL format: sqlite:///path/to/database.db
url = fmt.Sprintf("sqlite://%s", database)
}
// Create connection (this will test the connection before saving)
_, err := storage.CreateConnection(name, driver, url)
if err != nil {
return err
}
return nil
}