mirror of
https://github.com/ddworken/hishtory.git
synced 2025-02-19 20:10:58 +01:00
Have hishtory import also read from stdin
This commit is contained in:
parent
18ddbf2ca9
commit
6cd7fa00fc
@ -1383,7 +1383,7 @@ echo %v-bar`, randomCmdUuid, randomCmdUuid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trigger an import
|
// Trigger an import
|
||||||
out = tester.RunInteractiveShell(t, "hishtory import")
|
out = tester.RunInteractiveShell(t, "echo stdincommand | hishtory import")
|
||||||
r := regexp.MustCompile(`Imported (.+) history entries from your existing shell history`)
|
r := regexp.MustCompile(`Imported (.+) history entries from your existing shell history`)
|
||||||
matches := r.FindStringSubmatch(out)
|
matches := r.FindStringSubmatch(out)
|
||||||
if len(matches) != 2 {
|
if len(matches) != 2 {
|
||||||
@ -1399,7 +1399,7 @@ echo %v-bar`, randomCmdUuid, randomCmdUuid)
|
|||||||
|
|
||||||
// Check that the previously recorded commands are in hishtory
|
// Check that the previously recorded commands are in hishtory
|
||||||
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
|
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
|
||||||
expectedOutput = fmt.Sprintf("hishtory export %s\necho %s-foo\necho %s-bar\n/tmp/client install \nhishtory export %s\nhishtory import\n", randomCmdUuid, randomCmdUuid, randomCmdUuid, randomCmdUuid)
|
expectedOutput = fmt.Sprintf("hishtory export %s\necho %s-foo\necho %s-bar\n/tmp/client install \nhishtory export %s\nstdincommand\necho stdincommand | hishtory import\n", randomCmdUuid, randomCmdUuid, randomCmdUuid, randomCmdUuid)
|
||||||
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
if diff := cmp.Diff(expectedOutput, out); diff != "" {
|
||||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
|
||||||
}
|
}
|
||||||
|
@ -407,6 +407,11 @@ func ImportHistory(ctx *context.Context) (int, error) {
|
|||||||
return 0, fmt.Errorf("failed to parse zsh history: %v", err)
|
return 0, fmt.Errorf("failed to parse zsh history: %v", err)
|
||||||
}
|
}
|
||||||
historyEntries = append(historyEntries, extraEntries...)
|
historyEntries = append(historyEntries, extraEntries...)
|
||||||
|
extraEntries, err = readStdin()
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to read stdin: %v", err)
|
||||||
|
}
|
||||||
|
historyEntries = append(historyEntries, extraEntries...)
|
||||||
db := hctx.GetDb(ctx)
|
db := hctx.GetDb(ctx)
|
||||||
currentUser, err := user.Current()
|
currentUser, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -445,6 +450,25 @@ func ImportHistory(ctx *context.Context) (int, error) {
|
|||||||
return len(historyEntries), nil
|
return len(historyEntries), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readStdin() ([]string, error) {
|
||||||
|
ret := make([]string, 0)
|
||||||
|
in := bufio.NewReader(os.Stdin)
|
||||||
|
for {
|
||||||
|
s, err := in.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
s = strings.TrimSpace(s)
|
||||||
|
if s != "" {
|
||||||
|
ret = append(ret, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
func parseBashHistory(homedir string) ([]string, error) {
|
func parseBashHistory(homedir string) ([]string, error) {
|
||||||
return readFileToArray(filepath.Join(homedir, ".bash_history"))
|
return readFileToArray(filepath.Join(homedir, ".bash_history"))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user