-
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.
Port the command line tools to cobra/viper. (#65)
* Port the command line tools to cobra/viper. When we started the command line tooling, we quickly hit the limitations of kong. We decided to port everything to cobra/viper and this is the result. This keeps the command flags intact, but changes virtually all environment variables. * Update port for localdev * Update cmd/root/command.go Co-authored-by: Daniel Swärd <excds@kth.se> --------- Co-authored-by: Daniel Swärd <excds@kth.se>
- Loading branch information
Showing
15 changed files
with
536 additions
and
253 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
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
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
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,102 @@ | ||
// Copyright 2024 go-dataspace | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package root | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"slices" | ||
|
||
"github.com/go-dataspace/run-dsp/internal/server" | ||
"github.com/go-dataspace/run-dsp/logging" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
cfgFile string | ||
|
||
validLogLevels = []string{"debug", "info", "warn", "error"} | ||
|
||
rootCmd = &cobra.Command{ | ||
Use: "run-dsp", | ||
Short: "RUN-DSP is a lightweight dataspace connector.", | ||
Long: `A lightweight IDSA dataspace connector, designed to | ||
connect non-dataspace data providers via gRPC`, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
logLevel := viper.GetString("logLevel") | ||
if !slices.Contains(validLogLevels, logLevel) { | ||
return fmt.Errorf("Invalid log level %s, valid levels: %v", logLevel, validLogLevels) | ||
} | ||
ctx := context.Background() | ||
humanReadable := false | ||
if viper.GetBool("debug") { | ||
humanReadable = true | ||
logLevel = "debug" | ||
} | ||
ctx = logging.Inject(ctx, logging.NewJSON(logLevel, humanReadable)) | ||
viper.Set("initCTX", ctx) | ||
return nil | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
cobra.EnableTraverseRunHooks = true | ||
|
||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is /etc/run-dsp/run-dsp.toml)") | ||
rootCmd.PersistentFlags().BoolP("debug", "d", false, "enable debug mode") | ||
rootCmd.PersistentFlags().StringP( | ||
"log-level", "l", "info", fmt.Sprintf("set log level, valid levels: %v", validLogLevels)) | ||
|
||
err := viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug")) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
err = viper.BindPFlag("logLevel", rootCmd.PersistentFlags().Lookup("log-level")) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
viper.SetDefault("debug", false) | ||
viper.SetDefault("logLevel", "info") | ||
|
||
rootCmd.AddCommand(server.Command) | ||
} | ||
|
||
func initConfig() { | ||
if cfgFile != "" { | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
viper.AddConfigPath("/etc/run-dsp") | ||
viper.SetConfigType("toml") | ||
viper.SetConfigName("run-dsp.toml") | ||
} | ||
|
||
viper.AutomaticEnv() | ||
if err := viper.ReadInConfig(); err == nil { | ||
log.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} |
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,24 @@ | ||
### GLOBAL OPTIONS ### | ||
debug = true # Sets the output to a human readable format and sets the log level to debug. ($DEBUG) | ||
logLevel = "debug" # Sets the log level. Valid values: debug/info/warn/error ($LOGLEVEL) | ||
|
||
### SERVER OPTIONS ### | ||
[server] | ||
|
||
## Dataspace component configuration | ||
[server.dsp] | ||
address = "127.0.0.1" # IP address of the local machine to listen to for dataspace requests. ($SERVER.DSP.ADDRESS) | ||
port = 8080 # TCP port to listen on for dataspace requests. ($SERVER.DSP.PORT) | ||
externalURL = "http://127.0.0.1:8080" # Address that we are reachable by to other dataspace participants. ($SERVER.DSP.EXTERNALURL) | ||
|
||
## Provider gRPC settings | ||
[server.provider] | ||
address = "reference-provider:9090" # The address of the provider service. ($SERVER.PROVIDER.ADDRESS) | ||
insecure = true # Disable TLS when connecting to the provider. ($SERVER.PROVIDER.INSECURE) | ||
|
||
## Control service settings | ||
[server.control] | ||
enabled = true # Enable the control service. ($SERVER.CONTROL.ENABLED) | ||
address = "127.0.0.1" # IP address of the local machine to listen to for the control service. ($SERVER.CONTROL.ADDRESS) | ||
port = 8081 # TCP port to listen on for the control service. ($SERVER.CONTROL.PORT) | ||
insecure = true # Disable TLS for the control service ($SERVER.CONTROL.INSECURE) |
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,24 @@ | ||
### GLOBAL OPTIONS ### | ||
debug = true # Sets the output to a human readable format and sets the log level to debug. ($DEBUG) | ||
logLevel = "debug" # Sets the log level. Valid values: debug/info/warn/error ($LOGLEVEL) | ||
|
||
### SERVER OPTIONS ### | ||
[server] | ||
|
||
## Dataspace component configuration | ||
[server.dsp] | ||
address = "127.0.0.1" # IP address of the local machine to listen to for dataspace requests. ($SERVER.DSP.ADDRESS) | ||
port = 8080 # TCP port to listen on for dataspace requests. ($SERVER.DSP.PORT) | ||
externalURL = "http://127.0.0.1:8080" # Address that we are reachable by to other dataspace participants. ($SERVER.DSP.EXTERNALURL) | ||
|
||
## Provider gRPC settings | ||
[server.provider] | ||
address = "127.0.0.1:19090" # The address of the provider service. ($SERVER.PROVIDER.ADDRESS) | ||
insecure = true # Disable TLS when connecting to the provider. ($SERVER.PROVIDER.INSECURE) | ||
|
||
## Control service settings | ||
[server.control] | ||
enabled = true # Enable the control service. ($SERVER.CONTROL.ENABLED) | ||
address = "127.0.0.1" # IP address of the local machine to listen to for the control service. ($SERVER.CONTROL.ADDRESS) | ||
port = 8081 # TCP port to listen on for the control service. ($SERVER.CONTROL.PORT) | ||
insecure = true # Disable TLS for the control service ($SERVER.CONTROL.INSECURE) |
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,35 @@ | ||
## This shows all configuration options of RUN-DSP, what they do and the corresponsing environment variables.###### RUN-DSP reference configuration file ##### | ||
## The default location of the configuration file is /etc/run-dsp/run-dsp.toml but you can change that | ||
## with the `-d` command line flag | ||
|
||
### GLOBAL OPTIONS ### | ||
debug = true # Sets the output to a human readable format and sets the log level to debug. ($DEBUG) | ||
logLevel = "info" # Sets the log level. Valid values: debug/info/warn/error ($LOGLEVEL) | ||
|
||
### SERVER OPTIONS ### | ||
[server] | ||
|
||
## Dataspace component configuration | ||
[server.dsp] | ||
address = "127.0.0.1" # IP address of the local machine to listen to for dataspace requests. ($SERVER.DSP.ADDRESS) | ||
port = 8080 # TCP port to listen on for dataspace requests. ($SERVER.DSP.PORT) | ||
externalURL = "http://127.0.0.1:8080" # Address that we are reachable by to other dataspace participants. ($SERVER.DSP.EXTERNALURL) | ||
|
||
## Provider gRPC settings | ||
[server.provider] | ||
address = "127.0.0.1:9090" # The address of the provider service. ($SERVER.PROVIDER.ADDRESS) | ||
insecure = false # Disable TLS when connecting to the provider. ($SERVER.PROVIDER.INSECURE) | ||
caCert = "/path/to/ca.crt" # Path to the CA certificate of the CA that issued the server certificate of the provider. ($SERVER.PROVIDER.CACERT) | ||
clientCert = "/path/to/client.crt" # Client certificate to authenticate with to the provider. ($SERVER.PROVIDER.CLIENTCERT) | ||
clientCertKey = "/path/to/client.key" # Key to the above mentioned certificate. ($SERVER.PROVIDER.CLIENTCERTKEY) | ||
|
||
## Control service settings | ||
[server.control] | ||
enabled = true # Enable the control service. ($SERVER.CONTROL.ENABLED) | ||
address = "127.0.0.1" # IP address of the local machine to listen to for the control service. ($SERVER.CONTROL.ADDRESS) | ||
port = 8081 # TCP port to listen on for the control service. ($SERVER.CONTROL.PORT) | ||
insecure = false # Disable TLS for the control service ($SERVER.CONTROL.INSECURE) | ||
cert = "/path/to/control.crt" # TLS certificate to use for the control service ($SERVER.CONTROL.CERT) | ||
certKey = "/path/to/control.key" # Key to the above mentioned certificate. ($SERVER.CONTROL.CERTKEY) | ||
verifyClientCerts = true # Only allow access to clients with a certificate issued by the CA defined below. ($SERVER.CONTROL.VERIFYCLIENTCERTS) | ||
clientCACert = "/etc/hosts" # Certificate of the CA that issues client certificates. ($SERVER.CONTROL.CLIENTCACERT) |
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
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
Oops, something went wrong.