-
Notifications
You must be signed in to change notification settings - Fork 13
/
host.go
45 lines (37 loc) · 1.3 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2016, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package main
import (
"fmt"
"net/http"
"github.com/cassava/repoctl/internal/term"
"github.com/goulash/osutil"
"github.com/spf13/cobra"
)
var hostListen string
func init() {
MainCmd.AddCommand(hostCmd)
hostCmd.Flags().StringVar(&hostListen, "listen", ":8080", "which address and port to listen on")
}
var hostCmd = &cobra.Command{
Use: "host",
Aliases: []string{"serve"},
Short: "Host repository on a network",
Long: `Host the repository on a network.
This is essentially static file serving the repository on a specific
address and port, and is only meant for temporary use. If you want
to run something like this for longer, consider using darkhttpd.
`,
Args: cobra.ExactArgs(0),
ValidArgsFunction: completeNoFiles,
PreRunE: ProfileInit,
PostRunE: ProfileTeardown,
RunE: func(cmd *cobra.Command, args []string) error {
if ok, _ := osutil.DirExists(Repo.Directory); !ok {
return fmt.Errorf("repo directory %q does not exist", Repo.Directory)
}
term.Printf("Serving %s on %s...\n", Repo.Directory, hostListen)
return http.ListenAndServe(hostListen, http.FileServer(http.Dir(Repo.Directory)))
},
}