Skip to content

Commit a4e28ec

Browse files
committed
Move path to positional arg in serve
1 parent 8171fed commit a4e28ec

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

cmd/serve/serve.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,27 @@ import (
88
"github.com/spf13/cobra"
99
)
1010

11-
var path string
1211
var address string
1312
var port int
1413

1514
func init() {
16-
Cmd.Flags().StringVarP(&path, "dir", "d", "./", "Directory to serve")
1715
Cmd.Flags().StringVarP(&address, "address", "a", "", "Address to listen on")
1816
Cmd.Flags().IntVarP(&port, "port", "p", 8000, "Post to listen on")
1917
}
2018

21-
func serveHelp() {
22-
fmt.Println("Usage: please serve <PATH> [<ADDRESS>[:<PORT>]]")
23-
fmt.Println()
24-
fmt.Println("Serves the contents of PATH on the specified address and port.")
25-
fmt.Println("Requested paths will be printed to stdout.")
26-
}
27-
2819
var Cmd = &cobra.Command{
29-
Use: "serve",
30-
Short: "Serve the contents of a directory through a simple web server",
20+
Use: "serve (PATH)",
21+
Short: "Serve the contents of PATH (current directory if omitted) through a simple web server",
22+
Args: cobra.RangeArgs(0, 1),
3123
Run: func(cmd *cobra.Command, args []string) {
24+
path := "./"
25+
if len(args) == 1 {
26+
path = args[0]
27+
}
28+
3229
address = fmt.Sprintf("%s:%d", address, port)
3330

34-
fmt.Println("Listening on", address)
31+
fmt.Printf("Serving %s on %s\n", path, address)
3532

3633
fsys := loggingFileSystem{http.Dir(path)}
3734

0 commit comments

Comments
 (0)