-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
123 lines (112 loc) · 3.08 KB
/
options.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
package main
import (
"fmt"
"os"
"time"
"github.com/pelletier/go-toml"
"github.com/zincium/zinc/modules/base"
"github.com/zincium/zinc/modules/cast"
"github.com/zincium/zinc/modules/env"
)
// version info
var (
VERSION = "1.0"
BUILDTIME string
BUILDCOMMIT string
BUILDBRANCH string
GOVERSION string
ServerVersion = "Silver/" + VERSION
)
func version() {
fmt.Fprintf(os.Stdout, `silver-httpd - Redesigned Git Over HTTP Server
version: %s
build branch: %s
build commit: %s
build time: %s
go version: %s
`, VERSION, BUILDBRANCH, BUILDCOMMIT, BUILDTIME, GOVERSION)
}
func usage() {
fmt.Fprintf(os.Stdout, `silver-httpd - Redesigned Git Over HTTP Server
usage: %s <option> url
-h|--help Show usage text and quit
-v|--version Show version number and quit
-V|--verbose Make the operation more talkative
-p|--profile Set profile path. default: %s\config\silver-httpd.toml
-D|--daemon Run zinc-secured as daemon
`, os.Args[0], env.AppDir())
}
// Options base
type Options struct {
Listen string `toml:"Listen,omitempty"`
GitPath string `toml:"GitPath,omitempty"`
Root string `toml:"Root"`
IdleTimeout string `toml:"IdleTimeout,omitempty"`
idleTimeout time.Duration
WriteTimeout string `toml:"WriteTimeout,omitempty"`
writeTimeout time.Duration
ReadTimeout string `toml:"ReadTimeout,omitempty"`
readTimeout time.Duration
Certificate string `toml:"Certificate,omitempty"`
CertificateKey string `toml:"CertificateKey,omitempty"`
profile string
background bool
}
// Initialize initialize opts
func (opts *Options) Initialize(expander *env.Expander) error {
fd, err := os.Open(opts.profile)
if err != nil {
return base.ErrorCat("unable open configure file: ", err.Error())
}
defer fd.Close()
if toml.NewDecoder(fd).Decode(opts); err != nil {
return base.ErrorCat("unable decode configure: ", err.Error())
}
opts.Certificate = expander.PathExpand(opts.Certificate)
opts.CertificateKey = expander.PathExpand(opts.CertificateKey)
opts.idleTimeout = cast.ToDuration(opts.IdleTimeout)
opts.writeTimeout = cast.ToDuration(opts.WriteTimeout)
opts.readTimeout = cast.ToDuration(opts.ReadTimeout)
if opts.GitPath == "" {
opts.GitPath = "git"
}
return nil
}
// Invoke invoke
func (opts *Options) Invoke(val int, oa, raw string) error {
switch val {
case 'h':
version()
os.Exit(0)
case 'v':
usage()
os.Exit(0)
case 'V':
base.IsDebugMode = true
case 'p':
opts.profile = oa
case 'D':
opts.background = true
}
return nil
}
// ParseArgv parse argv
func (opts *Options) ParseArgv() error {
var pa base.ParseArgs
pa.Add("help", base.NOARG, 'h')
pa.Add("version", base.NOARG, 'v')
pa.Add("verbose", base.NOARG, 'V')
pa.Add("profile", base.REQUIRED, 'p')
pa.Add("daemon", base.NOARG, 'D')
if err := pa.Execute(os.Args, opts); err != nil {
return err
}
expander := env.NewExpander()
if len(opts.profile) == 0 {
opts.profile = expander.PathExpand("${APPDIR}/config/silver-httpd.toml")
}
if err := opts.Initialize(expander); err != nil {
return err
}
return nil
}