Skip to content

Commit

Permalink
Add ReadJSONConfigFromFile for jsoncfg parser, it allows reading conf…
Browse files Browse the repository at this point in the history
…ig file from where the executable file resides as well. See #49, #52
  • Loading branch information
mkideal committed Mar 12, 2019
1 parent 38b9998 commit 41df2d0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
29 changes: 29 additions & 0 deletions _examples/032-jsoncfg/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"os"

"github.com/mkideal/cli"
)

type argT struct {
cli.Helper
Self *argT `json:"-" cli:"c,config" usage:"parse json config from file" parser:"jsoncfg" dft:"test.json"`
A string
B int
C bool
}

func newArgT() *argT {
var argv = new(argT)
argv.Self = argv
return argv
}

func main() {
os.Exit(cli.Run(newArgT(), func(ctx *cli.Context) error {
argv := ctx.Argv().(*argT)
ctx.JSONIndentln(argv, "", " ")
return nil
}))
}
5 changes: 5 additions & 0 deletions _examples/032-jsoncfg/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"A": "a string",
"B": 12,
"C": true
}
10 changes: 10 additions & 0 deletions builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ func (h Helper) AutoHelp() bool {
return h.Help
}

// Helper2 is builtin Help flag
type Helper2 struct {
Help bool `cli:"!h,help" usage:"Display help information" json:"-"`
}

// AutoHelp implements AutoHelper interface
func (h Helper2) AutoHelp() bool {
return h.Help
}

// Deprecated: Addr is builtin host,port flag
type Addr struct {
Host string `cli:"host" usage:"specify host" dft:"0.0.0.0"`
Expand Down
24 changes: 23 additions & 1 deletion cliutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/labstack/gommon/color"
Expand Down Expand Up @@ -93,7 +94,7 @@ func ReadJSON(r io.Reader, argv interface{}) error {
return json.NewDecoder(r).Decode(argv)
}

// ReadJSONFromFile is similar to ReadJSONFromFile, but read from file
// ReadJSONFromFile is similar to ReadJSON, but read from file
func ReadJSONFromFile(filename string, argv interface{}) error {
file, err := os.Open(filename)
if err == nil {
Expand All @@ -102,3 +103,24 @@ func ReadJSONFromFile(filename string, argv interface{}) error {
}
return err
}

// ReadJSONConfigFromFile is similar to ReadJSONFromFile, but allows reading file from where the executable file resides as well
func ReadJSONConfigFromFile(filename string, argv interface{}) error {
file, err := os.Open(filename)
if err == nil {
defer file.Close()
err = ReadJSON(file, argv)
} else {
exe, e := os.Executable()
if e != nil {
return e
}
// allow self-config .json files to go with the executable file, #40
file, err = os.Open(filepath.Dir(exe) + string(filepath.Separator) + filename)
if err == nil {
defer file.Close()
err = ReadJSON(file, argv)
}
}
return err
}
14 changes: 14 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func RegisterFlagParser(name string, creator FlagParserCreator) {
func init() {
RegisterFlagParser("json", newJSONParser)
RegisterFlagParser("jsonfile", newJSONFileParser)
RegisterFlagParser("jsoncfg", newJSONConfigFileParser)
RegisterFlagParser("url", newURLParser)
}

Expand Down Expand Up @@ -55,6 +56,19 @@ func (p JSONFileParser) Parse(s string) error {
return ReadJSONFromFile(s, p.ptr)
}

// JSON config file parser
type JSONConfigFileParser struct {
ptr interface{}
}

func newJSONConfigFileParser(ptr interface{}) FlagParser {
return &JSONConfigFileParser{ptr}
}

func (p JSONConfigFileParser) Parse(s string) error {
return ReadJSONConfigFromFile(s, p.ptr)
}

// URL parser
type URLParser struct {
ptr *url.URL
Expand Down

0 comments on commit 41df2d0

Please sign in to comment.