-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.go
96 lines (81 loc) · 2.58 KB
/
serve.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cmd
import (
"fmt"
"log"
"os"
"github.com/karashiiro/gittlz/pkg/api"
"github.com/karashiiro/gittlz/pkg/protocol"
"github.com/spf13/cobra"
)
var Username string
var Password string
var Protocol string
var GitPort int
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start the Git server",
Long: `Starts the Git server. By default, this uses the Git
server protocol, which has no authentication scheme.`,
Run: func(cmd *cobra.Command, args []string) {
// Start the control API
controller, err := api.NewServer(Hostname, ApiPort, Path)
if err != nil {
log.Fatalf("Failed to start API server: %v", err)
}
log.Println("API server started on port", ApiPort)
// Start the Git server
switch Protocol {
case "git":
port := getGitPort(9418)
daemon, err := protocol.StartGit(Hostname, port, BasePath, Path)
if err != nil {
log.Fatalf("Failed to start Git server: %v", err)
}
log.Println("Git server started on port", port)
err = daemon.Wait()
if err != nil {
log.Fatalf("Git server failed unexpectedly: %v", err)
}
case "http":
port := getGitPort(80)
log.Println("Git server started on port", port)
err := protocol.StartSmartHTTP(Hostname, port, Path, Username, Password)
if err != nil {
log.Fatalf("Git server failed unexpectedly: %v", err)
}
case "ssh":
port := getGitPort(22)
wh, err := protocol.StartSSH(Hostname, port, Path, Password)
if err != nil {
log.Fatalf("Failed to start Git server: %v", err)
}
log.Println("Git server started on port", port)
err = wh.Wait()
if err != nil {
log.Fatalf("Git server failed unexpectedly: %v", err)
}
default:
fmt.Printf("Unknown protocol: %s\n", Protocol)
os.Exit(1)
}
// Stop the control API
err = controller.Shutdown()
if err != nil {
log.Fatalf("API server failed unexpectedly: %v", err)
}
},
}
func init() {
rootCmd.AddCommand(serveCmd)
serveCmd.Flags().StringVarP(&Protocol, "protocol", "P", "git", "Git server protocol. Valid options: \"git\", \"http\", \"ssh\".")
serveCmd.Flags().IntVar(&GitPort, "git-port", -1, "The port to run the Git server on. Set this to -1 to use the default ports for each protocol.")
serveCmd.Flags().StringVarP(&Username, "username", "u", "", "Git username for authentication. Leave this empty to disable the username.")
serveCmd.Flags().StringVarP(&Password, "password", "p", "", "Git password for authentication. Leave this empty to disable the password.")
}
func getGitPort(fallback int) int {
if GitPort == -1 {
return fallback
}
return GitPort
}