-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (103 loc) · 2.72 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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/gorilla/mux"
"github.com/gorilla/rpc/v2"
"github.com/gorilla/rpc/v2/json2"
tth "github.com/buildkite/terminal-to-html"
"github.com/bozso/gotoolbox/cli"
"github.com/bozso/gotoolbox/cli/stream"
"github.com/bozso/gotoolbox/cmd"
"github.com/bozso/gotoolbox/command"
"github.com/bozso/gotoolbox/doc"
"github.com/bozso/gotoolbox/repository"
"github.com/bozso/gotoolbox/services"
)
type Repositories struct {
config repository.Config
vcs string
command string
intoHtml bool
out stream.Out
}
func (r *Repositories) SetCli(c *cli.Cli) {
c.Var(&r.config, "config", "json configuration of repository list")
c.BoolVar(&r.intoHtml, "html", false,
"whether to convert output to html")
c.StringVar(&r.vcs, "vcs", "git",
"type of the version control system to use")
c.StringVar(&r.command, "command", "status",
"which type of command to use")
c.Var(&r.out, "out", "where to write the output")
}
func (r Repositories) Run() (err error) {
var vcs command.VCS
switch strings.ToLower(r.vcs) {
case "git":
vcs = command.DefaultGit()
default:
err = fmt.Errorf("unknown version control system '%s'", r.vcs)
return
}
m := r.config.IntoManager(vcs)
var out []byte
switch strings.ToLower(r.command) {
case "status":
out, err = m.Status()
default:
err = fmt.Errorf("unknown manager command '%s'", r.command)
}
if err != nil {
return
}
if r.intoHtml {
out = tth.Render(out)
}
_, err = r.out.Write(out)
return
}
type Service struct {
port doc.Port
}
func (s *Service) SetCli(c *cli.Cli) {
s.port.SetCli(c)
return
}
func (sv Service) Run() (err error) {
s := rpc.NewServer()
s.RegisterCodec(json2.NewCodec(), "application/json")
s.RegisterCodec(json2.NewCodec(), "application/json;charset=UTF-8")
err = s.RegisterService(services.EncoderService{}, "")
r := mux.NewRouter()
r.Handle("/rpc", s)
return http.ListenAndServe(sv.port.Localhost(), r)
}
func main() {
c := cli.New("toolbox", "Useful functions.")
c.AddAction("repositories",
"manage version control system repositories",
&Repositories{})
c.AddAction("genrepos",
"generate configuration file for git repositories",
&cmd.GenRepos{})
c.AddAction("task",
"run a Taskfile.yml from any directory",
&Tasker{})
c.AddAction("jet-server", "render jet templates through a web server",
&doc.TemplateServer{})
c.AddAction("jet", "render jet templates from the command line",
&cmd.Jet{})
c.AddAction("template", "render jet templates",
&doc.TemplateRender{})
c.AddAction("service", "start services",
&Service{})
c.AddAction("mage", "run a go magefile", &cmd.Mage{})
err := c.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}