Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-orusapi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
orus-io
go-orusapi
Commits
b9c79b641362
Commit
b9c79b641362
authored
4 years ago
by
Christophe de Vienne
Browse files
Options
Downloads
Patches
Plain Diff
Add configuration file handling
parent
28952ee02687
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#6152
passed
4 years ago
Changes
4
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
database/options.go
+2
-2
2 additions, 2 deletions
database/options.go
generate-config.go
+40
-0
40 additions, 0 deletions
generate-config.go
logging.go
+2
-2
2 additions, 2 deletions
logging.go
templates/server/cmd.gotmpl
+52
-4
52 additions, 4 deletions
templates/server/cmd.gotmpl
with
96 additions
and
8 deletions
database/options.go
+
2
−
2
View file @
b9c79b64
...
...
@@ -4,8 +4,8 @@
// Options is a jessevdk/go-flags compatible struct for db-related options
type
Options
struct
{
DSN
string
`long:"
db-
dsn" env:"
DB_
DSN" ini-name:"dsn" description:"DSN of the database"`
MaxConn
int
`long:"
db-
max-conn" env:"
DB_
MAX_CONN" ini-name:"max-conn" description:"Database max connection" default:"0"`
DSN
string
`long:"dsn" env:"DSN" ini-name:"dsn" description:"DSN of the database"`
MaxConn
int
`long:"max-conn" env:"MAX_CONN" ini-name:"max-conn" description:"Database max connection" default:"0"`
}
// Open a connection to the database
...
...
This diff is collapsed.
Click to expand it.
generate-config.go
0 → 100644
+
40
−
0
View file @
b9c79b64
package
orusapi
import
(
"io"
"os"
flags
"github.com/orus-io/go-flags"
)
// GenerateConfigCmd is a command that generates a configuration file
type
GenerateConfigCmd
struct
{
Output
string
`short:"o" long:"output" default:"-" no-ini:"t" description:"output file"`
parser
*
flags
.
Parser
}
// NewGenerateConfigCmd creates a GenerateConfigCmd
func
NewGenerateConfigCmd
(
parser
*
flags
.
Parser
)
*
GenerateConfigCmd
{
return
&
GenerateConfigCmd
{
parser
:
parser
}
}
// Execute write out a configuration file
func
(
c
*
GenerateConfigCmd
)
Execute
([]
string
)
(
errResult
error
)
{
var
out
io
.
Writer
if
c
.
Output
==
"-"
{
out
=
os
.
Stdout
}
else
{
f
,
err
:=
os
.
OpenFile
(
c
.
Output
,
os
.
O_RDWR
|
os
.
O_CREATE
,
0755
)
if
err
!=
nil
{
return
err
}
defer
func
()
{
errResult
=
f
.
Close
()
}()
out
=
f
}
fp
:=
flags
.
NewIniParser
(
c
.
parser
)
fp
.
Write
(
out
,
flags
.
IniIncludeDefaults
|
flags
.
IniCommentDefaults
|
flags
.
IniDefault
)
return
nil
}
This diff is collapsed.
Click to expand it.
logging.go
+
2
−
2
View file @
b9c79b64
...
...
@@ -38,8 +38,8 @@
// LoggingOptions holds the logging options
type
LoggingOptions
struct
{
Level
func
(
string
)
error
`long:"
log-
level" env:"
LOG_
LEVEL" ini-name:"log-level" choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"fatal" choice:"panic" choice:"auto" default:"auto" description:"log level. 'auto' selects 'info' when stdout is a tty, 'error' otherwise."`
Format
func
(
string
)
error
`long:"
log-
format" env:"
LOG_
FORMAT" ini-name:"log-format" choice:"json" choice:"pretty" choice:"auto" default:"auto" description:"Logs format. 'auto' selects 'pretty' if stdout is a tty."`
Level
func
(
string
)
error
`long:"level" env:"LEVEL" ini-name:"log-level" choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"fatal" choice:"panic" choice:"auto" default:"auto" description:"log level. 'auto' selects 'info' when stdout is a tty, 'error' otherwise."`
Format
func
(
string
)
error
`long:"format" env:"FORMAT" ini-name:"log-format" choice:"json" choice:"pretty" choice:"auto" default:"auto" description:"Logs format. 'auto' selects 'pretty' if stdout is a tty."`
Verbose
func
()
`short:"v" long:"verbose" no-ini:"t" description:"Increase log verbosity. Can be repeated"`
logFinalOutput
io
.
Writer
`no-flag:"t"`
...
...
This diff is collapsed.
Click to expand it.
templates/server/cmd.gotmpl
+
52
−
4
View file @
b9c79b64
...
...
@@ -8,7 +8,11 @@
"orus.io/orus-io/go-orus-api/database"
)
type ConfigFile struct {
ConfigFile string `long:"config" short:"c" env:"CONFIG" no-ini:"t" description:"A configuration file"`
}
var (
Logger = orusapi.DefaultLogger(os.Stdout)
LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout))
DatabaseOptions = &database.Options{}
...
...
@@ -11,6 +15,7 @@
var (
Logger = orusapi.DefaultLogger(os.Stdout)
LoggingOptions = orusapi.MustLoggingOptions(orusapi.NewLoggingOptions(&Logger, os.Stdout))
DatabaseOptions = &database.Options{}
ConfigFileOption = &ConfigFile{}
parser = flags.NewNamedParser("{{ dasherize (pascalize .Name) }}", flags.HelpFlag|flags.PassDoubleDash)
...
...
@@ -15,5 +20,6 @@
parser = flags.NewNamedParser("{{ dasherize (pascalize .Name) }}", flags.HelpFlag|flags.PassDoubleDash)
bootstrapParser = flags.NewNamedParser("{{ dasherize (pascalize .Name) }}", flags.IgnoreUnknown)
)
func Run() int {
...
...
@@ -17,6 +23,20 @@
)
func Run() int {
if _, err := bootstrapParser.Parse(); err != nil {
Logger.Err(err).Msg("")
return 1
}
if ConfigFileOption.ConfigFile != "" {
Logger.Debug().Str("configfile", ConfigFileOption.ConfigFile).Msg("parsing configuration file")
iniParser := flags.NewIniParser(parser)
if err := iniParser.ParseFile(ConfigFileOption.ConfigFile); err != nil {
Logger.Err(err).Msg("")
return 1
}
}
if _, err := parser.Parse(); err != nil {
code := 1
if fe, ok := err.(*flags.Error); ok {
...
...
@@ -26,6 +46,6 @@
// so we print it on the console
fmt.Println(err)
} else {
l
og.Error().Msg(err.Error())
L
og
ger
.Error().Msg(err.Error())
}
} else {
...
...
@@ -30,6 +50,6 @@
}
} else {
l
og.Err(err).Msg("")
L
og
ger
.Err(err).Msg("")
}
return code
}
...
...
@@ -37,6 +57,34 @@
}
func init() {
parser.AddGroup("Logging", "Logging options", LoggingOptions)
parser.AddGroup("Database", "Database options", DatabaseOptions)
parser.NamespaceDelimiter = "-"
parser.EnvNamespaceDelimiter = "_"
parser.EnvNamespace = "CARNET"
parser.AddGroup("Configuration", "Configuration file", ConfigFileOption)
g, err := parser.AddGroup("Logging", "Logging options", LoggingOptions)
if err != nil {
panic(err)
}
g.Namespace="log"
g.EnvNamespace="LOG"
g, err = parser.AddGroup("Database", "Database options", DatabaseOptions)
if err != nil {
panic(err)
}
g.Namespace="db"
g.EnvNamespace="DB"
parser.AddCommand("generate-config", "Generate a configuration file", "", orusapi.NewGenerateConfigCmd(parser))
bootstrapParser.NamespaceDelimiter = "-"
bootstrapParser.EnvNamespaceDelimiter = "_"
bootstrapParser.EnvNamespace = "CARNET"
bootstrapParser.AddGroup("Configuration", "Configuration file", ConfigFileOption)
g, err = bootstrapParser.AddGroup("Logging", "Logging options", LoggingOptions)
if err != nil {
panic(err)
}
g.Namespace="log"
g.EnvNamespace="LOG"
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment