| 15 | ) |
| 16 | |
| 17 | func runAPI(args []string) int { |
| 18 | flag := flag.NewFlagSet("api", flag.ContinueOnError) |
| 19 | cwd := flag.String("cwd", core.Must(os.Getwd()), "current working directory") |
| 20 | pipePath := flag.String("pipe", "", "use named pipe or Unix domain socket for communication instead of stdio") |
| 21 | callbacks := flag.String("callbacks", "", "comma-separated list of FS callbacks to enable (readFile,fileExists,directoryExists,getAccessibleEntries,realpath)") |
| 22 | async := flag.Bool("async", false, "use JSON-RPC protocol instead of MessagePack (for async API)") |
| 23 | timing := flag.Bool("timing", false, "collect per-request server processing time, folded into the client's timing snapshot") |
| 24 | if err := flag.Parse(args); err != nil { |
| 25 | return 2 |
| 26 | } |
| 27 | |
| 28 | defaultLibraryPath := bundled.LibPath() |
| 29 | |
| 30 | // Parse callbacks list |
| 31 | var callbacksList []string |
| 32 | if *callbacks != "" { |
| 33 | callbacksList = strings.Split(*callbacks, ",") |
| 34 | } |
| 35 | |
| 36 | options := &api.StdioServerOptions{ |
| 37 | Err: os.Stderr, |
| 38 | Cwd: *cwd, |
| 39 | DefaultLibraryPath: defaultLibraryPath, |
| 40 | Callbacks: callbacksList, |
| 41 | Async: *async, |
| 42 | CollectTiming: *timing, |
| 43 | } |
| 44 | if *pipePath != "" { |
| 45 | options.PipePath = *pipePath |
| 46 | } else { |
| 47 | options.In = os.Stdin |
| 48 | options.Out = os.Stdout |
| 49 | } |
| 50 | |
| 51 | s := api.NewStdioServer(options) |
| 52 | |
| 53 | ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 54 | defer stop() |
| 55 | |
| 56 | if err := s.Run(ctx); err != nil { |
| 57 | fmt.Fprintln(os.Stderr, err) |
| 58 | return 1 |
| 59 | } |
| 60 | return 0 |
| 61 | } |