on main help sign in
feat: create `.crush/.gitignore` automatically

Closes #588 Closes #254

commitc5c8bfd1d8425ebc6d03b113527e348c04bb93e1 authorAndrey Nering <andreynering@users.noreply.github.com> dateFri 15 Aug 2025, 15:35 (1 year ago) parent9bc0e10
2 files changed+20−4
internal/cmd/root.go+20 internal/db/connect.go−4
internal/cmd/root.go+20
@@ -6,6 +6,7 @@ import (
66 "io"
77 "log/slog"
88 "os"
9+ "path/filepath"
910
1011 tea "github.com/charmbracelet/bubbletea/v2"
1112 "github.com/charmbracelet/crush/internal/app"
@@ -112,6 +113,10 @@ func setupApp(cmd *cobra.Command) (*app.App, error) {
112113 }
113114 cfg.Permissions.SkipRequests = yolo
114115
116+ if err := createDotCrushDir(cfg.Options.DataDirectory); err != nil {
117+ return nil, err
118+ }
119+
115120 // Connect to DB; this will also run migrations.
116121 conn, err := db.Connect(ctx, cfg.Options.DataDirectory)
117122 if err != nil {
@@ -160,3 +165,18 @@ func ResolveCwd(cmd *cobra.Command) (string, error) {
160165 }
161166 return cwd, nil
162167}
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 (
55 "database/sql"
66 "fmt"
77 "log/slog"
8− "os"
98 "path/filepath"
109
1110 _ "github.com/ncruces/go-sqlite3/driver"
@@ -18,9 +17,6 @@ func Connect(ctx context.Context, dataDir string) (*sql.DB, error) {
1817 if dataDir == "" {
1918 return nil, fmt.Errorf("data.dir is not set")
2019 }
21− if err := os.MkdirAll(dataDir, 0o700); err != nil {
22− return nil, fmt.Errorf("failed to create data directory: %w", err)
23− }
2420 dbPath := filepath.Join(dataDir, "crush.db")
2521 // Open the SQLite database
2622 db, err := sql.Open("sqlite3", dbPath)