func HttpApp(ctx context.Context) (context.Context, error) { ctx = core.RegisterCommand(ctx, "get", getHttp) ctx = core.RegisterCommand(ctx, "head", headHttp) return ctx, nil }
func main() { running := true quit := func(ctx context.Context, msg *core.Message) error { log.Printf("quitting") running = false return nil } core.RegisterApp("shell", ShellApp) core.RegisterApp("http", HttpApp) var app string var ctx context.Context var setup func(string) context.Context setApp := func(c2 context.Context, msg *core.Message) error { ctx = setup(strings.TrimSpace(msg.Data[0])) return nil } setup = func(appName string) context.Context { ctx := context.Background() ctx = core.RegisterCommand(ctx, "quit", quit) ctx = core.RegisterCommand(ctx, "app", setApp) ctx, err := core.CallApp(appName, ctx) if err != nil { log.Printf("error calling app: %s", err) } app = appName return ctx } ctx = setup("shell") r := bufio.NewReader(os.Stdin) w := os.Stdout for running { w.Write([]byte(fmt.Sprintf("%s > ", app))) line, err := r.ReadString('\n') if err != nil { log.Fatalf("Error: %s", err) break } segments := strings.Split(line, " ") cmdName := strings.TrimSpace(segments[0]) if cmdName == "" { continue } err = core.CallCommand(ctx, cmdName, &core.Message{ Data: segments[1:], }) if err != nil { log.Printf("Error calling command %s: %s", cmdName, err) } } }
func ShellApp(ctx context.Context) (context.Context, error) { ctx = core.RegisterCommand(ctx, "ls", lsCommand) ctx = core.RegisterCommand(ctx, "echo", echoCommand) return ctx, nil }