Skip to content

Commit

Permalink
Port the command line tools to cobra/viper. (#65)
Browse files Browse the repository at this point in the history
* 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
ainmosni and Daniel Swärd authored Sep 20, 2024
1 parent 1773ca0 commit 815f664
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 253 deletions.
20 changes: 5 additions & 15 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@
"mode": "auto",
"program": "${workspaceFolder}/cmd/",
"args": [
"-c",
"${workspaceFolder}/conf/localdev.toml",
"server",
"--debug",
"--provider-address",
"127.0.0.1:9090",
"--provider-insecure",
"--control-enabled",
"--control-insecure",
"--external-url=http://127.0.0.1:8080",
]
],
},
{
"name": "Run DSP devcontainer",
Expand All @@ -31,14 +26,9 @@
"-buildvcs=false"
],
"args": [
"-c",
"${workspaceFolder}/conf/devcontainer.toml",
"server",
"--debug",
"--provider-address",
"reference-provider:9090",
"--provider-insecure",
"--control-enabled",
"--control-insecure",
"--external-url=http://127.0.0.1:8080",
]
}
]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.22.5 as builder
FROM docker.io/library/golang:1.22.5 AS builder
WORKDIR /app
COPY . ./
RUN make build
Expand Down
19 changes: 2 additions & 17 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,11 @@
package main

import (
"os"
_ "time/tzdata"

"github.com/alecthomas/kong"
"github.com/go-dataspace/run-dsp/internal/cli"
"github.com/go-dataspace/run-dsp/internal/server"
"github.com/go-dataspace/run-dsp/cmd/root"
)

var ui struct {
cli.GlobalOptions
Server server.Command `cmd:"" help:"Run server"`
}

func main() {
if len(os.Args) == 1 {
os.Args = append(os.Args, "--help")
}

ctx := kong.Parse(&ui)
params := cli.GenParams(ui.GlobalOptions)
ctx.BindTo(params, (*cli.Params)(nil))
ctx.FatalIfErrorf(ctx.Run())
root.Execute()
}
102 changes: 102 additions & 0 deletions cmd/root/command.go
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)
}
}
24 changes: 24 additions & 0 deletions conf/devcontainer.toml
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)
24 changes: 24 additions & 0 deletions conf/localdev.toml
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)
35 changes: 35 additions & 0 deletions conf/reference.toml
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)
11 changes: 6 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ services:
command: server
environment:
- LOGLEVEL=debug
- PROVIDER_INSECURE=true
- PROVIDER_URL=reference-provider:9090
- EXTERNAL_URL=http://127.0.0.1:8080/
- SERVER.PROVIDER.ADDRESS=reference-provider:9090
- SERVER.PROVIDER.INSECURE=true
- SERVER.DSP.EXTERNALURL=http://127.0.0.1:8080/
ports:
- '8080'
- '18080:8080'
depends_on:
- reference-provider
reference-provider:
Expand All @@ -23,4 +23,5 @@ services:
volumes:
- .:/var/lib/run-dsp/fsprovider
ports:
- '9091'
- '19091:9091'
- '19090:9090'
28 changes: 23 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,51 @@ module github.com/go-dataspace/run-dsp
go 1.22.5

require (
github.com/alecthomas/kong v0.9.0
github.com/go-dataspace/run-dsrpc v0.0.3-alpha1
github.com/go-playground/validator/v10 v10.22.0
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
github.com/justinas/alice v1.2.0
github.com/lmittmann/tint v1.0.5
github.com/samber/slog-http v1.3.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.64.1
google.golang.org/protobuf v1.34.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 815f664

Please sign in to comment.