| 15 | } |
| 16 | |
| 17 | func serveCmd() *cobra.Command { |
| 18 | var ( |
| 19 | port string |
| 20 | dir string |
| 21 | ) |
| 22 | cmd := &cobra.Command{ |
| 23 | Use: "serve", |
| 24 | Short: "serve a directory over http", |
| 25 | Long: `Serve a directory over http.`, |
| 26 | RunE: func(cmd *cobra.Command, args []string) error { |
| 27 | http.Handle("/", http.FileServer(http.Dir(dir))) |
| 28 | log.Fatal(http.ListenAndServe(port, logRequest(http.DefaultServeMux))) |
| 29 | |
| 30 | return nil |
| 31 | }, |
| 32 | } |
| 33 | |
| 34 | cmd.Flags().StringVar(&port, "port", ":8080", "Local port to serve on") |
| 35 | cmd.Flags().StringVar(&dir, "directory", ".", "Directory to serve") |
| 36 | |
| 37 | return cmd |
| 38 | } |