-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhush.go
47 lines (42 loc) · 913 Bytes
/
hush.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package hush
import (
"flag"
"fmt"
"io"
"os"
"strings"
)
// Run runs the hush shell
func Run() int {
cancel, err := ttySetup()
if err != nil {
panic(err)
}
defer cancel()
return run(os.Stdin, os.Stdout, os.Stderr, os.Args)
}
func run(in io.Reader, out, outErr io.Writer, args []string) int {
set := flag.NewFlagSet(args[0], flag.ContinueOnError)
command := set.String("c", "", "Read and execute commands from the given string value.")
err := set.Parse(args[1:])
if err != nil {
fmt.Fprintln(outErr, err)
return 2
}
var reader io.RuneReader
if *command != "" {
reader = newRuneReader(strings.NewReader(*command))
} else {
reader = newRuneReader(in)
}
out, err = newCarriageReturnWriter(out)
if err != nil {
panic(err)
}
outErr, err = newCarriageReturnWriter(outErr)
if err != nil {
panic(err)
}
term := newTerminal(out, outErr)
return term.ReadEvalPrintLoop(reader)
}