-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
119 lines (101 loc) · 2.64 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
package main
import (
"fmt"
"os"
"runtime"
"github.com/urfave/cli/v2"
"github.com/alphatr/acme-lego/common/bootstrap"
"github.com/alphatr/acme-lego/common/config"
"github.com/alphatr/acme-lego/common/errors"
"github.com/alphatr/acme-lego/controller/account"
"github.com/alphatr/acme-lego/controller/certificate"
)
const (
defaultConfigPath string = "/etc/lego/config.toml"
)
type errWriter struct{}
func main() {
cli.VersionPrinter = versionPrinter
cli.ErrWriter = &errWriter{}
app := cli.NewApp()
app.Version = GitVersionTag
app.Name = "lego"
app.Usage = "A Let's Encrypt Client"
app.Authors = []*cli.Author{{Name: "AlphaTr"}}
app.Commands = []*cli.Command{
{
Name: "reg",
Usage: "create account",
Action: account.Register,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mail",
Usage: "account email",
},
},
Before: beforeCommand,
},
{
Name: "run",
Usage: "run get certificate",
Action: certificate.Obtain,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "domain",
Aliases: []string{"d"},
Usage: "certificate domain",
},
&cli.StringFlag{
Name: "http-path",
Usage: ".well-known/acme-challenge path",
},
},
Before: beforeCommand,
},
{
Name: "renew",
Usage: "renew certificate",
Action: certificate.Renew,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "domain",
Aliases: []string{"d"},
Usage: "certificate domain",
},
},
Before: beforeCommand,
},
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Value: defaultConfigPath,
Usage: "config `FILE` to use for config",
EnvVars: []string{"LEGO_CONFIG"},
},
}
app.Run(os.Args)
}
func beforeCommand(ctx *cli.Context) error {
configFile := ctx.String("config")
if err := config.InitConfig(configFile); err != nil {
err := errors.NewError(errors.ConfigInitErrno, err)
return cli.NewExitError(err.Error(), 101)
}
if err := bootstrap.InitBootstrap(); err != nil {
err := errors.NewError(errors.BootstrapInitErrno, err)
return cli.NewExitError(err.Error(), 102)
}
config.Config.UserAgent = fmt.Sprintf("%s-cli/%s", config.Config.Name, ctx.App.Version)
return nil
}
func versionPrinter(ctx *cli.Context) {
buildString := fmt.Sprintf("%s/%s; %s; git:%s", runtime.GOOS, runtime.GOARCH, runtime.Version(), GitHash)
buildTime := fmt.Sprintf("build-time: %s", CurrentTime)
fmt.Fprintf(ctx.App.Writer, "%s/v%s (%s) %s\n", ctx.App.Name, ctx.App.Version, buildString, buildTime)
}
func (ins *errWriter) Write(input []byte) (int, error) {
bootstrap.Log.Error(string(input))
return len(input), nil
}