Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ Requirements for running sphinx
Configuration
*************

The Nuts-go library contains some configuration logic which allows for usage of configFiles, Environment variables and commandLine params transparently.
If a Nuts engine is added as Engine it'll automatically work for the given engine. It is also possible for an engine to add the capabilities on a standalone basis.
This allows for testing from within a repo.
The Nuts library contains some configuration logic which allows for usage of configFiles, Environment variables and commandLine params transparently.

The parameters follow the following convention:
``$ nuts --parameter X`` is equal to ``$ NUTS_PARAMETER=X nuts`` is equal to ``parameter: X`` in a yaml file.
Expand All @@ -117,6 +115,12 @@ Config parameters for engines are prepended by the ``engine.ConfigKey`` by defau
is equal to ``$ nuts --engine.nested.parameter X`` is equal to ``$ NUTS_ENGINE_NESTED_PARAMETER=X nuts``


Ordering
********

Command line parameters have the highest priority, then environment variables, then parameters from the configfile and lastly defaults.
The location of the configfile is determined by the environment variable ``NUTS_CONFIGFILE`` or the commandline parameter ``--configfile``. If both are missing the default location ``./nuts.yaml`` is used.

Options
*******

Expand All @@ -129,8 +133,7 @@ Key Default Description
============================ ============== =================================================================================================================================================================================
****
address localhost:1323 Address and port the server will be listening to
configfile nuts.yaml Nuts config file
identity Vendor identity for the node, mandatory when running in server mode. Must be in the format: urn:oid:1.3.6.1.4.1.54851.4:<number>
configfile Nuts config file
strictmode false When set, insecure settings are forbidden.
verbosity info Log level (trace, debug, info, warn, error)
**Crypto**
Expand All @@ -140,7 +143,7 @@ crypto.storage fs Storage to use, 'fs' for file syst
network.advertHashesInterval 2000 Interval (in milliseconds) that specifies how often the node should broadcast its last hashes to other nodes.
network.bootstrapNodes Space-separated list of bootstrap nodes (`<host>:<port>`) which the node initially connect to.
network.certFile PEM file containing the server certificate for the gRPC server. Required when `enableTLS` is `true`.
network.certKeyFile PEM file containing the private key of the server certificate. Required when `enableTLS` is `true`.
network.certKeyFile PEM file containing the private key of the server certificate. Required when `network.enableTLS` is `true`.
network.databaseFile network.db File path to the network database.
network.enableTLS true Whether to enable TLS for inbound gRPC connections. If set to `true` (which is default) `certFile` and `certKeyFile` MUST be configured.
network.grpcAddr \:5555 Local address for gRPC to listen on. If empty the gRPC server won't be started and other nodes will not be able to connect to this node (outbound connections can still be made).
Expand Down
41 changes: 20 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,25 @@ func createRootCommand() *cobra.Command {
}
}

func createPrintConfigCommand(system *core.System) *cobra.Command {
return &cobra.Command{
Use: "config",
Short: "Prints the current config",
Run: func(cmd *cobra.Command, args []string) {
cmd.Println("Current system config")
cmd.Println(system.Config.PrintConfig())
},
}
}

func createServerCommand(system *core.System) *cobra.Command {
return &cobra.Command{
Use: "server",
Short: "Starts the Nuts server",
Run: func(cmd *cobra.Command, args []string) {
logrus.Info("Starting server with config:")
logrus.Info(system.Config.PrintConfig())

// check config on all engines
if err := system.Configure(); err != nil {
logrus.Fatal(err)
Expand All @@ -67,7 +81,6 @@ func createServerCommand(system *core.System) *cobra.Command {
logrus.Fatal(err)
}

cfg := core.NutsConfig()
// start interfaces
echoServer := echoCreator()
system.VisitEngines(func(engine *core.Engine) {
Expand All @@ -81,7 +94,7 @@ func createServerCommand(system *core.System) *cobra.Command {
logrus.Fatal(err)
}
}()
if err := echoServer.Start(cfg.ServerAddress()); err != nil {
if err := echoServer.Start(system.Config.Address); err != nil {
logrus.Fatal(err)
}
},
Expand All @@ -93,7 +106,7 @@ func CreateCommand(system *core.System) *cobra.Command {
command := createRootCommand()
command.SetOut(stdOutWriter)
addSubCommands(system, command)
addFlagSets(system, command, core.NutsConfig())
addFlagSets(system, command)
return command
}

Expand All @@ -117,18 +130,11 @@ func Execute() {
command := CreateCommand(system)
command.SetOut(stdOutWriter)

// Load global Nuts config
cfg := core.NutsConfig()

// Load all config and add generic options
if err := cfg.Load(command); err != nil {
if err := system.Config.Load(command); err != nil {
panic(err)
}

// Load config into engines
injectConfig(system, cfg)
cfg.PrintConfig(system, logrus.StandardLogger())

// blocking main call
command.Execute()
}
Expand All @@ -140,18 +146,11 @@ func addSubCommands(system *core.System, root *cobra.Command) {
}
})
root.AddCommand(createServerCommand(system))
root.AddCommand(createPrintConfigCommand(system))
}

func injectConfig(system *core.System, cfg *core.NutsGlobalConfig) {
if err := system.VisitEnginesE(func(engine *core.Engine) error {
return cfg.InjectIntoEngine(engine)
}); err != nil {
logrus.Fatal(err)
}
}

func addFlagSets(system *core.System, cmd *cobra.Command, cfg *core.NutsGlobalConfig) {
func addFlagSets(system *core.System, cmd *cobra.Command) {
system.VisitEngines(func(engine *core.Engine) {
cfg.RegisterFlags(cmd, engine)
system.Config.RegisterFlags(cmd, engine)
})
}
21 changes: 21 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ func Test_rootCmd(t *testing.T) {
actual := buf.String()
assert.Contains(t, actual, "Available Commands")
})

t.Run("config cmd prints config", func(t *testing.T) {
oldStdout := stdOutWriter
buf := new(bytes.Buffer)
stdOutWriter = buf
defer func() {
stdOutWriter = oldStdout
}()
os.Args = []string{"nuts", "config"}
Execute()
actual := buf.String()
assert.Contains(t, actual, "Current system config")
assert.Contains(t, actual, "address")
})

t.Run("start in server mode", func(t *testing.T) {
ctrl := gomock.NewController(t)
echoServer := core.NewMockEchoServer(ctrl)
Expand All @@ -45,3 +60,9 @@ func Test_rootCmd(t *testing.T) {
Execute()
})
}

func Test_echoCreator(t *testing.T) {
t.Run("creates an echo server", func(t *testing.T) {
assert.NotNil(t, echoCreator())
})
}
Loading