Skip to content

Commit

Permalink
chore: Improve the flag parsing
Browse files Browse the repository at this point in the history
Change-Id: Id2e25bb22b1ba1544f4da72d6397abc81e3b2442
  • Loading branch information
andeya committed Jun 29, 2019
1 parent 51c2464 commit 0a890a4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
23 changes: 19 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package faygo
import (
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -191,11 +193,25 @@ func ConfigDir() string {
return configDir
}

func resetFlag() {
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
flag.CommandLine.SetOutput(nil)
for _, arg := range os.Args[1:] {
if arg == "-help" || arg == "--help" || arg == "-h" || arg == "--h" {
fmt.Fprintf(flag.CommandLine.Output(), "Usage:\n")
flag.PrintDefaults()
os.Exit(2)
}
}
}

// global config
var globalConfig = func() GlobalConfig {
// get config dir
flag.StringVar(&configDir, "cfg_dir", configDir, "Configuration files directory")
flag.Parse()
flag.CommandLine.Init(os.Args[0], -1) // ignore error
flag.CommandLine.SetOutput(ioutil.Discard)
flag.CommandLine.StringVar(&configDir, "cfg_dir", configDir, "Configuration files directory")
flag.CommandLine.Parse(os.Args[1:])

var background = &GlobalConfig{
Cache: CacheConfig{
Expand All @@ -216,8 +232,7 @@ var globalConfig = func() GlobalConfig {
FileLevel: "debug",
},
}
filename := configDir + globalConfigFile

filename := filepath.Join(configDir, globalConfigFile)
err := SyncINI(
background,
func(onceUpdateFunc func() error) error {
Expand Down
14 changes: 10 additions & 4 deletions faygo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func GetFrame(name string, version ...string) (*Framework, bool) {

// Run starts all web services.
func Run() {
global.beforeRun()
global.framesLock.Lock()
for _, frame := range global.frames {
if !frame.Running() {
Expand All @@ -88,9 +89,6 @@ func Run() {
}
}
global.framesLock.Unlock()
global.graceOnce.Do(func() {
graceSignal()
})
select {}
}

Expand Down Expand Up @@ -456,7 +454,7 @@ type (
// executed after services are closed, but not guaranteed to be completed.
postCloseFunc func() error

graceOnce sync.Once
beforeRunOnce sync.Once
}
// PresetStatic is the system default static file routing information
PresetStatic struct {
Expand Down Expand Up @@ -569,3 +567,11 @@ func addFrame(frame *Framework) {
}
global.frames = append(global.frames, frame)
}

func (g *GlobalVariables) beforeRun() {
g.beforeRunOnce.Do(func() {
resetFlag()
WritePid(LogDir() + "app.pid")
go graceSignal()
})
}
4 changes: 1 addition & 3 deletions framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,8 @@ func (frame *Framework) Run() {
if frame.Running() {
return
}
global.beforeRun()
go frame.run()
global.graceOnce.Do(func() {
graceSignal()
})
select {}
}

Expand Down
1 change: 0 additions & 1 deletion graceful_a.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
)

func graceSignal() {
WritePid(LogDir() + "app.pid")
// subscribe to SIGINT signals
ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, os.Kill)
Expand Down
1 change: 0 additions & 1 deletion graceful_b.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

func graceSignal() {
WritePid(LogDir() + "app.pid")
// subscribe to SIGINT signals
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR2)
Expand Down

0 comments on commit 0a890a4

Please sign in to comment.