on main help sign in
feat: create `.crush/.gitignore` automatically
c5c8bfdAndrey Nering
internal/cmd/root.go+20
@@ -6,6 +6,7 @@ import (
6 "io"
6 "io"
7 "log/slog"
7 "log/slog"
8 "os"
8 "os"
9+ "path/filepath"
9
10
10 tea "github.com/charmbracelet/bubbletea/v2"
11 tea "github.com/charmbracelet/bubbletea/v2"
11 "github.com/charmbracelet/crush/internal/app"
12 "github.com/charmbracelet/crush/internal/app"
@@ -112,6 +113,10 @@ func setupApp(cmd *cobra.Command) (*app.App, error) {
112 }
113 }
113 cfg.Permissions.SkipRequests = yolo
114 cfg.Permissions.SkipRequests = yolo
114
115
116+ if err := createDotCrushDir(cfg.Options.DataDirectory); err != nil {
117+ return nil, err
118+ }
119+
115 // Connect to DB; this will also run migrations.
120 // Connect to DB; this will also run migrations.
116 conn, err := db.Connect(ctx, cfg.Options.DataDirectory)
121 conn, err := db.Connect(ctx, cfg.Options.DataDirectory)
117 if err != nil {
122 if err != nil {
@@ -160,3 +165,18 @@ func ResolveCwd(cmd *cobra.Command) (string, error) {
160 }
165 }
161 return cwd, nil
166 return cwd, nil
162}
167}
168+
169+func createDotCrushDir(dir string) error {
170+ if err := os.MkdirAll(dir, 0o700); err != nil {
171+ return fmt.Errorf("failed to create data directory: %q %w", dir, err)
172+ }
173+
174+ gitIgnorePath := filepath.Join(dir, ".gitignore")
175+ if _, err := os.Stat(gitIgnorePath); os.IsNotExist(err) {
176+ if err := os.WriteFile(gitIgnorePath, []byte("*\n"), 0o644); err != nil {
177+ return fmt.Errorf("failed to create .gitignore file: %q %w", gitIgnorePath, err)
178+ }
179+ }
180+
181+ return nil
182+}
internal/db/connect.go−4
@@ -5,7 +5,6 @@ import (
5 "database/sql"
5 "database/sql"
6 "fmt"
6 "fmt"
7 "log/slog"
7 "log/slog"
8− "os"
9 "path/filepath"
8 "path/filepath"
10
9
11 _ "github.com/ncruces/go-sqlite3/driver"
10 _ "github.com/ncruces/go-sqlite3/driver"
@@ -18,9 +17,6 @@ func Connect(ctx context.Context, dataDir string) (*sql.DB, error) {
18 if dataDir == "" {
17 if dataDir == "" {
19 return nil, fmt.Errorf("data.dir is not set")
18 return nil, fmt.Errorf("data.dir is not set")
20 }
19 }
21− if err := os.MkdirAll(dataDir, 0o700); err != nil {
22− return nil, fmt.Errorf("failed to create data directory: %w", err)
23− }
24 dbPath := filepath.Join(dataDir, "crush.db")
20 dbPath := filepath.Join(dataDir, "crush.db")
25 // Open the SQLite database
21 // Open the SQLite database
26 db, err := sql.Open("sqlite3", dbPath)
22 db, err := sql.Open("sqlite3", dbPath)