-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (119 loc) · 3.06 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/memfs"
"gopkg.in/src-d/go-git.v4"
ssh2 "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"gopkg.in/src-d/go-git.v4/storage/memory"
"gopkg.in/yaml.v2"
)
var app App
type App struct {
listener string
static string
key string
pass string
config string
}
func init() {
pflag.StringVarP(&app.listener, "listener", "l", "0.0.0.0:8765", "ip/port to listen on")
pflag.StringVarP(&app.key, "key", "k", "id_rsa", "path to ssh key")
pflag.StringVarP(&app.pass, "pass", "p", "", "password of the ssh key")
pflag.StringVarP(&app.static, "static", "s", "", "serve given dir as http root")
pflag.StringVarP(&app.config, "config", "c", "cms.yaml", "configuration file")
}
type Site struct {
Git string `yaml:"git"`
Key string `yaml:"key"`
BaseDir string `yaml:"baseDir"`
Tag string `yaml:"tag"`
fs billy.Filesystem
repo *git.Repository
}
type Config struct {
Sites map[string]*Site `yaml:"sites"`
}
func NewConfig(path string) (Config, error) {
c := Config{}
data, err := ioutil.ReadFile(path)
if err != nil {
return c, fmt.Errorf("Error while reading configuration file '%s': %s", path, err.Error())
}
err = yaml.Unmarshal(data, &c)
if err != nil {
return c, fmt.Errorf("Error while unmarshalling configuration '%s' from yaml: %s", path, err.Error())
}
for _, site := range c.Sites {
site.BaseDir = strings.Trim(site.BaseDir, "/")
}
return c, nil
}
func main() {
pflag.Parse()
c, err := NewConfig(app.config)
CheckIfError(err)
pem, err := ioutil.ReadFile(app.key)
CheckIfError(err)
signer, err := ssh.ParsePrivateKeyWithPassphrase(pem, []byte(app.pass))
CheckIfError(err)
auth := &ssh2.PublicKeys{User: "git", Signer: signer}
for name, site := range c.Sites {
fmt.Printf("Loading %s from %s...\n", name, site.Git)
fs := memfs.New()
c.Sites[name].repo, err = git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: site.Git,
Auth: auth,
Progress: os.Stdout,
})
CheckIfError(err)
c.Sites[name].fs = fs
}
s := NewServer(app.listener, app.static, c.Sites)
s.Run()
}
type Node struct {
Name string `json:"name"`
IsDir bool `json:"is_dir"`
FullPath string `json:"full_path"`
Children []Node `json:"children"`
}
func WalkNode(path string, fs billy.Filesystem, trim string) (Node, error) {
n := Node{
Children: []Node{},
FullPath: strings.TrimPrefix(path, trim),
}
e, err := fs.Stat(path)
if err != nil {
return n, err
}
n.Name = e.Name()
n.IsDir = e.IsDir()
if n.IsDir {
elems, err := fs.ReadDir(path)
if err != nil {
return n, err
}
for _, elem := range elems {
elemName := fmt.Sprintf("%s/%s", path, elem.Name())
c, err := WalkNode(elemName, fs, trim)
if err != nil {
return n, err
}
n.Children = append(n.Children, c)
}
}
return n, nil
}
func CheckIfError(err error) {
if err == nil {
return
}
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}