|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "spark/internal/git/scanner" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/viper" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + scanDBPath string |
| 16 | + scanSkipAPI bool |
| 17 | +) |
| 18 | + |
| 19 | +var scanCmd = &cobra.Command{ |
| 20 | + Use: "scan [folder-path]", |
| 21 | + Short: "Scan folders for git repositories and save to SQLite", |
| 22 | + Long: `Recursively scan a folder for git repositories, optionally fetch metadata |
| 23 | +from GitHub/GitLab APIs, and save results to a SQLite database. |
| 24 | +
|
| 25 | +The default database path is ~/.innate/feeds.db and can be overridden via |
| 26 | +the --db flag or the 'git.scanner.db' config key in ~/.spark.yaml.`, |
| 27 | + Args: cobra.MaximumNArgs(1), |
| 28 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | + folderPath := "." |
| 30 | + if len(args) > 0 { |
| 31 | + folderPath = args[0] |
| 32 | + } |
| 33 | + |
| 34 | + absPath, err := filepath.Abs(folderPath) |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("resolve path: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + dbPath := scanDBPath |
| 40 | + if dbPath == "" { |
| 41 | + dbPath = viper.GetString("git.scanner.db") |
| 42 | + } |
| 43 | + if dbPath == "" { |
| 44 | + dbPath, err = scanner.DefaultDBPath() |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("resolve default database path: %w", err) |
| 47 | + } |
| 48 | + } else if len(dbPath) >= 2 && dbPath[0] == '~' && (dbPath[1] == '/' || dbPath[1] == '\\') { |
| 49 | + home, err := os.UserHomeDir() |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("resolve home directory: %w", err) |
| 52 | + } |
| 53 | + dbPath = filepath.Join(home, dbPath[2:]) |
| 54 | + } |
| 55 | + |
| 56 | + fmt.Printf("Scanning folder: %s\n", absPath) |
| 57 | + fmt.Println("Looking for git repositories...") |
| 58 | + if !scanSkipAPI { |
| 59 | + fmt.Println("Fetching repository information from APIs...") |
| 60 | + } |
| 61 | + |
| 62 | + repos, err := scanner.Scan(absPath, scanner.Options{SkipAPI: scanSkipAPI}) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + if len(repos) == 0 { |
| 68 | + fmt.Println("No git repositories found.") |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + fmt.Printf("\nFound %d repositories\n", len(repos)) |
| 73 | + |
| 74 | + store, err := scanner.OpenStore(dbPath) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + defer store.Close() |
| 79 | + |
| 80 | + if err := store.SaveRepos(repos); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + fmt.Printf("\nSaved %d repositories to: %s\n", len(repos), dbPath) |
| 85 | + printScanSummary(repos) |
| 86 | + |
| 87 | + return nil |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | +func printScanSummary(repos []scanner.RepoInfo) { |
| 92 | + fmt.Println("\nSummary:") |
| 93 | + |
| 94 | + reposByType := make(map[string]int) |
| 95 | + for _, repo := range repos { |
| 96 | + reposByType[repo.RepoType]++ |
| 97 | + } |
| 98 | + |
| 99 | + for repoType, count := range reposByType { |
| 100 | + fmt.Printf(" - %s: %d repositories\n", repoType, count) |
| 101 | + } |
| 102 | + |
| 103 | + totalStars := 0 |
| 104 | + for _, repo := range repos { |
| 105 | + totalStars += repo.Stars |
| 106 | + } |
| 107 | + if totalStars > 0 { |
| 108 | + fmt.Printf(" - Total stars: %d\n", totalStars) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func init() { |
| 113 | + scanCmd.Flags().StringVarP(&scanDBPath, "db", "d", "", "SQLite database path (default: ~/.innate/feeds.db)") |
| 114 | + scanCmd.Flags().BoolVar(&scanSkipAPI, "skip-api", false, "Skip API calls, only scan local repos") |
| 115 | + |
| 116 | + viper.SetDefault("git.scanner.db", "") |
| 117 | + viper.BindPFlag("git.scanner.db", scanCmd.Flags().Lookup("db")) |
| 118 | + |
| 119 | + GitCmd.AddCommand(scanCmd) |
| 120 | +} |
0 commit comments