forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitserver.go
119 lines (101 loc) · 2.81 KB
/
gitserver.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gitserver
import (
"flag"
"fmt"
"io"
"log"
"net/url"
"os"
"github.com/openshift/origin/pkg/cmd/templates"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/gitserver"
"github.com/openshift/origin/pkg/gitserver/autobuild"
)
const LogLevelEnv = "LOGLEVEL"
var (
longCommandDesc = templates.LongDesc(`
Start a Git server
This command launches a Git HTTP/HTTPS server that supports push and pull, mirroring,
and automatic creation of applications on push.
%[1]s`)
repositoryBuildConfigsDesc = templates.LongDesc(`
Retrieve build configs for a gitserver repository
This command lists build configurations in the current namespace that correspond to a given git repository.`)
)
// CommandFor returns gitrepo-buildconfigs command or gitserver command
func CommandFor(basename string) *cobra.Command {
var cmd *cobra.Command
out := os.Stdout
setLogLevel()
switch basename {
case "gitrepo-buildconfigs":
cmd = NewCommandRepositoryBuildConfigs(basename, out)
default:
cmd = NewCommandGitServer("gitserver")
}
return cmd
}
// NewCommandGitServer launches a Git server
func NewCommandGitServer(name string) *cobra.Command {
cmd := &cobra.Command{
Use: name,
Short: "Start a Git server",
Long: fmt.Sprintf(longCommandDesc, gitserver.EnvironmentHelp),
Run: func(c *cobra.Command, args []string) {
err := RunGitServer()
cmdutil.CheckErr(err)
},
}
return cmd
}
func RunGitServer() error {
config, err := gitserver.NewEnvironmentConfig()
if err != nil {
return err
}
link, err := autobuild.NewAutoLinkBuildsFromEnvironment()
switch {
case err == autobuild.ErrNotEnabled:
case err != nil:
log.Fatal(err)
default:
link.LinkFn = func(name string) *url.URL { return gitserver.RepositoryURL(config, name, nil) }
clones, err := link.Link()
if err != nil {
log.Printf("error: %v", err)
break
}
for name, v := range clones {
config.InitialClones[name] = v
}
}
return gitserver.Start(config)
}
func NewCommandRepositoryBuildConfigs(name string, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("%s REPOSITORY_NAME", name),
Short: "Retrieve build configs for a gitserver repository",
Long: repositoryBuildConfigsDesc,
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
err := cmdutil.UsageError(c, "This command takes a single argument - the name of the repository")
cmdutil.CheckErr(err)
}
repoName := args[0]
client, err := gitserver.GetClient()
cmdutil.CheckErr(err)
err = gitserver.GetRepositoryBuildConfigs(client, repoName, out)
cmdutil.CheckErr(err)
},
}
return cmd
}
func setLogLevel() {
logLevel := os.Getenv(LogLevelEnv)
if len(logLevel) > 0 {
if flag.CommandLine.Lookup("v") != nil {
flag.CommandLine.Set("v", logLevel)
}
}
}