// Show implements s (show) command. func Show(slice *slices.Slice, filter *utils.Regexp, env *environments.Environment) { var commands []historyentries.HistoryEntry if slice.Start >= 0 && slice.Finish >= 0 { keeper, err := historyentries.GetCommands(historyentries.GetCommandsRange, filter, env, slice.Start, slice.Finish) if err != nil { return } commands = keeper.Result().([]historyentries.HistoryEntry) } else { keeper, err := historyentries.GetCommands(historyentries.GetCommandsAll, filter, env) if err != nil { return } toBeRanged := keeper.Result().([]historyentries.HistoryEntry) sliceStart := slices.GetSliceIndex(slice.Start, len(toBeRanged)) sliceFinish := slices.GetSliceIndex(slice.Finish, len(toBeRanged)) if sliceStart < 0 || sliceFinish < 0 || sliceFinish <= sliceStart { return } commands = toBeRanged[sliceStart:sliceFinish] } for idx := 0; idx < len(commands); idx++ { os.Stdout.WriteString(commands[idx].ToString(env)) os.Stdout.WriteString("\n") } }
// ListTrace implements l command (list trace). func ListTrace(argument string, env *environments.Environment) { number, err := strconv.Atoi(argument) if err != nil || number < 0 { utils.Logger.Panicf("Cannot convert argument to a command number: %s", argument) } commands, err := historyentries.GetCommands(historyentries.GetCommandsPrecise, nil, env, number) if err != nil { utils.Logger.Panic(err) } command := commands.Result().(historyentries.HistoryEntry) hashFilename := command.GetTraceName() filename := env.GetTraceFileName(hashFilename) if _, err := os.Stat(filename); os.IsNotExist(err) { utils.Logger.Panicf("Output for %s is not exist", argument) } file := utils.Open(filename) defer file.Close() ungzippedFile, err := gzip.NewReader(file) if err != nil { utils.Logger.Panic(err) } defer ungzippedFile.Close() scanner := bufio.NewScanner(ungzippedFile) for scanner.Scan() { os.Stdout.WriteString(scanner.Text()) os.Stdout.WriteString("\n") } if err := scanner.Err(); err != nil { utils.Logger.Panic(err) } }
// ExecuteCommandNumber executes command by its number in history file. func ExecuteCommandNumber(number int, interactive bool, pseudoTTY bool, env *environments.Environment) { if number < 0 { utils.Logger.Panic("Cannot find such command") } commands, err := historyentries.GetCommands(historyentries.GetCommandsPrecise, nil, env, number) if err != nil { utils.Logger.Panic(err) } command := commands.Result().(historyentries.HistoryEntry) execute(command.GetCommand(), env.Shell, interactive, pseudoTTY) }
func getPreciseHash(cmd string, env *environments.Environment) (hash string, err error) { commands, err := historyentries.GetCommands(historyentries.GetCommandsAll, nil, env) if err != nil { err = fmt.Errorf("Cannot fetch commands list: %v", err) return } commandList := commands.Result().([]historyentries.HistoryEntry) if len(commandList) == 0 { err = errors.New("Command list is empty") return } // Why do I need such complicated logic here? The reason is trivial: // one may use substitutions in a command like // ah t -- `which python` script $(docker images -a -q) // basically it manages these situations as precise as possible. // Candidates here just means that several command may be executed // simultaneously (e.g. with tmuxinator) so timestamp is not precise // identifier here. candidates, err := getCandidates(commandList) if err != nil { err = fmt.Errorf("Cannot detect proper command: %v", err) return } if len(candidates) == 1 { hash = candidates[0].GetTraceName() return } suitable := getSuitable(candidates, cmd) hash = (*suitable).GetTraceName() return }
// Bookmark implements "b" (bookmark) command. func Bookmark(commandNumber int, bookmarkAs string, env *environments.Environment) { if commandNumber < 0 { utils.Logger.Panic("Command number should be >= 0") } commandsKeeper, err := historyentries.GetCommands(historyentries.GetCommandsAll, nil, env) if err != nil { utils.Logger.Panic(err) } commands := commandsKeeper.Result().([]historyentries.HistoryEntry) if len(commands) <= commandNumber { utils.Logger.Panic("Command number does not exist") } command := commands[commandNumber-1] filename := env.GetBookmarkFileName(bookmarkAs) file, err := os.Create(filename) if err != nil { utils.Logger.Panicf("Cannot create bookmark %s: %v", filename, err) } defer file.Close() file.WriteString(command.GetCommand()) }