Skip to content

Commit b6c97f9

Browse files
committed
refactor: extract env settings struct
1 parent d861f6f commit b6c97f9

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

src/app/main.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"../serverError"
77
"../serverHandler"
88
"../serverLog"
9+
"../setting"
910
"../tpl"
1011
"../util"
1112
"os"
@@ -31,15 +32,15 @@ func (app *App) ReOpenLog() []error {
3132
return app.logFileMan.Reopen()
3233
}
3334

34-
func NewApp(params []*param.Param) (*App, []error) {
35-
errs := writePidFile()
36-
if len(errs) > 0 {
37-
return nil, errs
35+
func NewApp(params []*param.Param, setting *setting.Setting) (*App, []error) {
36+
if len(setting.PidFile) > 0 {
37+
errs := writePidFile(setting.PidFile)
38+
if len(errs) > 0 {
39+
return nil, errs
40+
}
3841
}
3942

40-
verbose := !util.GetBoolEnv("GHFS_QUIET")
41-
42-
if serverHandler.TryEnableWSL1Fix() && verbose {
43+
if serverHandler.TryEnableWSL1Fix() && !setting.Quiet {
4344
ttyFile, teardownTtyFile := util.GetTTYFile()
4445
ttyFile.WriteString("WSL 1 compatible mode enabled\n")
4546
teardownTtyFile()
@@ -114,7 +115,7 @@ func NewApp(params []*param.Param) (*App, []error) {
114115
}
115116
}
116117

117-
if verbose {
118+
if !setting.Quiet {
118119
go printAccessibleURLs(vhSvc)
119120
}
120121

@@ -124,12 +125,7 @@ func NewApp(params []*param.Param) (*App, []error) {
124125
}, nil
125126
}
126127

127-
func writePidFile() (errs []error) {
128-
pidFilename := os.Getenv("GHFS_PID_FILE")
129-
if len(pidFilename) == 0 {
130-
return nil
131-
}
132-
128+
func writePidFile(pidFilename string) (errs []error) {
133129
pidContent := strconv.Itoa(os.Getpid())
134130
pidFile, err := os.OpenFile(pidFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
135131
if err != nil {

src/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"./app"
55
"./param"
66
"./serverError"
7+
"./setting"
78
"./version"
89
"errors"
910
"os"
@@ -35,6 +36,7 @@ func reopenLogOnHup(appInst *app.App) {
3536
}
3637

3738
func main() {
39+
// params
3840
params, printVersion, printHelp, errs := param.ParseFromCli()
3941
serverError.CheckFatal(errs...)
4042
if printVersion {
@@ -46,9 +48,12 @@ func main() {
4648
os.Exit(0)
4749
}
4850

49-
appInst, errs := app.NewApp(params)
50-
serverError.CheckFatal(errs...)
51+
// setting
52+
setting := setting.ParseFromEnv()
5153

54+
// app
55+
appInst, errs := app.NewApp(params, setting)
56+
serverError.CheckFatal(errs...)
5257
if appInst == nil {
5358
serverError.CheckFatal(errors.New("failed to create application instance"))
5459
}
@@ -57,6 +62,5 @@ func main() {
5762
reopenLogOnHup(appInst)
5863
errs = appInst.Open()
5964
serverError.CheckFatal(errs...)
60-
6165
appInst.Close()
6266
}

src/setting/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package setting
2+
3+
import (
4+
"../util"
5+
"os"
6+
)
7+
8+
type Setting struct {
9+
Quiet bool
10+
PidFile string
11+
}
12+
13+
func ParseFromEnv() *Setting {
14+
quiet := util.GetBoolEnv("GHFS_QUIET")
15+
pidFile := os.Getenv("GHFS_PID_FILE")
16+
17+
return &Setting{
18+
Quiet: quiet,
19+
PidFile: pidFile,
20+
}
21+
}

0 commit comments

Comments
 (0)