starting a shell

This commit is contained in:
Chris Lu
2012-08-23 20:56:09 -07:00
parent 0109cc0e30
commit 0121f35c12
8 changed files with 297 additions and 16 deletions

View File

@@ -0,0 +1,54 @@
package main
import (
"bufio"
"os"
"fmt"
)
func init() {
cmdShell.Run = runShell // break init cycle
}
var cmdShell = &Command{
UsageLine: "shell",
Short: "run interactive commands, now just echo",
Long: `run interactive commands.
`,
}
var (
)
func runShell(command *Command, args []string) bool {
r := bufio.NewReader(os.Stdin)
o := bufio.NewWriter(os.Stdout)
e := bufio.NewWriter(os.Stderr)
prompt := func () {
o.WriteString("> ")
o.Flush()
};
readLine := func () string {
ret, err := r.ReadString('\n')
if err != nil {
fmt.Fprint(e,err);
os.Exit(1)
}
return ret
}
execCmd := func (cmd string) int {
if cmd != "" {
o.WriteString(cmd)
}
return 0
}
cmd := ""
for {
prompt()
cmd = readLine()
execCmd(cmd)
}
return true
}

View File

@@ -1,12 +1,12 @@
package main
import (
"encoding/json"
"encoding/json"
"flag"
"fmt"
"net/http"
"io"
"log"
"net/http"
"os"
"strings"
"sync"
@@ -22,7 +22,8 @@ var port *int
var commands = []*Command{
cmdFix,
cmdMaster,
cmdUpload,
cmdUpload,
cmdShell,
cmdVersion,
cmdVolume,
}
@@ -50,6 +51,12 @@ func main() {
if args[0] == "help" {
help(args[1:])
for _, cmd := range commands {
if cmd.Name() == args[1] && cmd.Run != nil {
fmt.Fprintf(os.Stderr, "Default Parameters:\n")
cmd.Flag.PrintDefaults()
}
}
return
}
@@ -59,10 +66,10 @@ func main() {
cmd.Flag.Parse(args[1:])
args = cmd.Flag.Args()
if !cmd.Run(cmd, args) {
fmt.Fprintf(os.Stderr, "\n")
cmd.Flag.Usage()
fmt.Fprintf(os.Stderr, "Default Parameters:\n")
cmd.Flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
cmd.Flag.Usage()
}
exit()
return
@@ -173,15 +180,15 @@ func exitIfErrors() {
}
}
func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
w.Header().Set("Content-Type", "application/javascript")
bytes, _ := json.Marshal(obj)
callback := r.FormValue("callback")
if callback == "" {
w.Write(bytes)
} else {
w.Write([]uint8(callback))
w.Write([]uint8("("))
fmt.Fprint(w, string(bytes))
w.Write([]uint8(")"))
}
w.Header().Set("Content-Type", "application/javascript")
bytes, _ := json.Marshal(obj)
callback := r.FormValue("callback")
if callback == "" {
w.Write(bytes)
} else {
w.Write([]uint8(callback))
w.Write([]uint8("("))
fmt.Fprint(w, string(bytes))
w.Write([]uint8(")"))
}
}