-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Artem Bortnikov <[email protected]>
- Loading branch information
1 parent
1ef76e8
commit b574652
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/brongineer/helium" | ||
"github.com/brongineer/helium/flag" | ||
) | ||
|
||
type params struct { | ||
bindAddress string | ||
bindPort uint32 | ||
logLevel string | ||
developmentMode bool | ||
timeout time.Duration | ||
peers []string | ||
} | ||
|
||
func parse(args []string) (params, error) { | ||
fs := helium.NewFlagSet() | ||
fs.String("bind-address", flag.Description("bind listen address"), flag.DefaultValue("localhost")) | ||
fs.Uint32("bind-port", flag.Description("bind listen port"), flag.DefaultValue(uint32(80))) | ||
fs.String("log-level", flag.Description("logging level"), flag.DefaultValue("info")) | ||
fs.Bool("development-mode", flag.Shorthand("d"), flag.DefaultValue(false)) | ||
fs.Duration("timeout", flag.Description("context timeout"), flag.Shorthand("t"), flag.DefaultValue(time.Minute)) | ||
fs.StringSlice("peers", flag.Description("remote peers"), flag.DefaultValue([]string{})) | ||
|
||
if err := fs.Parse(args); err != nil { | ||
return params{}, err | ||
} | ||
|
||
return params{ | ||
bindAddress: fs.GetString("bind-address"), | ||
bindPort: fs.GetUint32("bind-port"), | ||
logLevel: fs.GetString("log-level"), | ||
developmentMode: fs.GetBool("development-mode"), | ||
timeout: fs.GetDuration("timeout"), | ||
peers: fs.GetStringSlice("peers"), | ||
}, nil | ||
} | ||
|
||
func main() { | ||
opts, err := parse(os.Args) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "Error: %v", err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Parsed params:", opts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/brongineer/helium" | ||
"github.com/brongineer/helium/flag" | ||
) | ||
|
||
type params struct { | ||
serverOpts srvParams | ||
loggerOpts logParams | ||
} | ||
|
||
type srvParams struct { | ||
BindAddress string `json:"bindAddress"` | ||
BindPort uint32 `json:"bindPort"` | ||
Timeout time.Duration `json:"timeout"` | ||
Peers []string `json:"peers"` | ||
} | ||
|
||
type logParams struct { | ||
LogLevel string `json:"logLevel"` | ||
LogFormat string `json:"logFormat"` | ||
DevMode bool `json:"devMode"` | ||
} | ||
|
||
func parser[T any](input string) (any, error) { | ||
var ( | ||
b []byte | ||
err error | ||
opts T | ||
) | ||
if input == "" { | ||
return nil, fmt.Errorf("path to config file is empty") | ||
} | ||
b, err = os.ReadFile(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err = json.Unmarshal(b, &opts); err != nil { | ||
return nil, err | ||
} | ||
return &opts, nil | ||
} | ||
|
||
func parse(args []string) (params, error) { | ||
fs := helium.NewFlagSet() | ||
helium.CustomFlag[srvParams](fs, "server-config", flag.CommandLineParser(parser[srvParams])) | ||
helium.CustomFlag[logParams](fs, "log-config", flag.CommandLineParser(parser[logParams])) | ||
|
||
if err := fs.Parse(args); err != nil { | ||
return params{}, err | ||
} | ||
|
||
return params{ | ||
serverOpts: helium.GetCustomFlag[srvParams](fs, "server-config"), | ||
loggerOpts: helium.GetCustomFlag[logParams](fs, "log-config"), | ||
}, nil | ||
} | ||
|
||
func main() { | ||
opts, err := parse(os.Args) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "Error: %v", err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Parsed params:") | ||
fmt.Println(opts.serverOpts) | ||
fmt.Println(opts.loggerOpts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"logLevel": "info", | ||
"logFormat": "json", | ||
"devMode": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"bindAddress": "100.64.0.1", | ||
"bindPort": 8080, | ||
"timeout": 15000000000, | ||
"peers": [ | ||
"https://peer-0.example.com", | ||
"https://peer-1.example.com", | ||
"https://peer-2.example.com" | ||
] | ||
} |