@@ -6,6 +6,7 @@ import (
"io"
"log/slog"
"os"
+ "path/filepath"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/crush/internal/app"
@@ -112,6 +113,10 @@ func setupApp(cmd *cobra.Command) (*app.App, error) {
}
cfg.Permissions.SkipRequests = yolo
+ if err := createDotCrushDir(cfg.Options.DataDirectory); err != nil {
+ return nil, err
+ }
+
// Connect to DB; this will also run migrations.
conn, err := db.Connect(ctx, cfg.Options.DataDirectory)
if err != nil {
@@ -160,3 +165,18 @@ func ResolveCwd(cmd *cobra.Command) (string, error) {
}
return cwd, nil
}
+
+func createDotCrushDir(dir string) error {
+ if err := os.MkdirAll(dir, 0o700); err != nil {
+ return fmt.Errorf("failed to create data directory: %q %w", dir, err)
+ }
+
+ gitIgnorePath := filepath.Join(dir, ".gitignore")
+ if _, err := os.Stat(gitIgnorePath); os.IsNotExist(err) {
+ if err := os.WriteFile(gitIgnorePath, []byte("*\n"), 0o644); err != nil {
+ return fmt.Errorf("failed to create .gitignore file: %q %w", gitIgnorePath, err)
+ }
+ }
+
+ return nil
+}
@@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"
"log/slog"
- "os"
"path/filepath"
_ "github.com/ncruces/go-sqlite3/driver"
@@ -18,9 +17,6 @@ func Connect(ctx context.Context, dataDir string) (*sql.DB, error) {
if dataDir == "" {
return nil, fmt.Errorf("data.dir is not set")
}
- if err := os.MkdirAll(dataDir, 0o700); err != nil {
- return nil, fmt.Errorf("failed to create data directory: %w", err)
- }
dbPath := filepath.Join(dataDir, "crush.db")
// Open the SQLite database
db, err := sql.Open("sqlite3", dbPath)