| 216 | const NUM_IMPORTED_ENTRIES_SLOW int = 20_000 |
| 217 | |
| 218 | func ImportHistory(ctx context.Context, shouldReadStdin, force bool) (int, error) { |
| 219 | config := hctx.GetConf(ctx) |
| 220 | if config.HaveCompletedInitialImport && !force { |
| 221 | // Don't run an import if we already have run one. This avoids importing the same entry multiple times. |
| 222 | return 0, nil |
| 223 | } |
| 224 | homedir := hctx.GetHome(ctx) |
| 225 | inputFiles := []string{ |
| 226 | filepath.Join(homedir, ".bash_history"), |
| 227 | filepath.Join(homedir, ".zsh_history"), |
| 228 | } |
| 229 | if histfile := os.Getenv("HISTFILE"); histfile != "" && !slices.Contains(inputFiles, histfile) { |
| 230 | inputFiles = append(inputFiles, histfile) |
| 231 | } |
| 232 | zHistPath := filepath.Join(homedir, ".zhistory") |
| 233 | if !slices.Contains(inputFiles, zHistPath) { |
| 234 | inputFiles = append(inputFiles, zHistPath) |
| 235 | } |
| 236 | entriesIter := parseFishHistory(homedir) |
| 237 | for _, file := range inputFiles { |
| 238 | entriesIter = concatIterators(entriesIter, readFileToIterator(file)) |
| 239 | } |
| 240 | totalNumEntries, err := countLinesInFiles(inputFiles...) |
| 241 | if err != nil { |
| 242 | return 0, fmt.Errorf("failed to count input lines during hishtory import: %w", err) |
| 243 | } |
| 244 | if shouldReadStdin { |
| 245 | extraEntries, err := ReadStdin() |
| 246 | if err != nil { |
| 247 | return 0, fmt.Errorf("failed to read stdin: %w", err) |
| 248 | } |
| 249 | entriesIter = concatIterators(entriesIter, Values(extraEntries)) |
| 250 | totalNumEntries += len(extraEntries) |
| 251 | } |
| 252 | fishLines, err := countLinesInFile(getFishHistoryPath(homedir)) |
| 253 | if err != nil { |
| 254 | return 0, fmt.Errorf("failed to count fish history lines during hishtory import: %w", err) |
| 255 | } |
| 256 | totalNumEntries += fishLines |
| 257 | db := hctx.GetDb(ctx) |
| 258 | currentUser, err := user.Current() |
| 259 | if err != nil { |
| 260 | return 0, err |
| 261 | } |
| 262 | hostname, err := os.Hostname() |
| 263 | if err != nil { |
| 264 | return 0, err |
| 265 | } |
| 266 | numEntriesImported := 0 |
| 267 | var iteratorError error = nil |
| 268 | var batch []data.HistoryEntry |
| 269 | importTimestamp := time.Now().UTC() |
| 270 | importEntryId := uuid.Must(uuid.NewRandom()).String() |
| 271 | var bar *progressbar.ProgressBar |
| 272 | if totalNumEntries > NUM_IMPORTED_ENTRIES_SLOW { |
| 273 | fmt.Println("Importing existing history entries") |
| 274 | bar = progressbar.Default(int64(totalNumEntries)) |
| 275 | defer bar.Finish() |