Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 50.dockter.2 #52

Merged
merged 11 commits into from
Mar 14, 2023
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

-

## [0.3.8] - 2023-03-14

### Changed in 0.3.8

- Update dependencies
- Standardize use of Viper/Cobra

## [0.3.7] - 2023-03-13

### Changed in 0.3.7
Expand Down
233 changes: 118 additions & 115 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,179 +11,182 @@ import (

"github.com/senzing/go-common/g2engineconfigurationjson"
"github.com/senzing/go-logging/logger"
"github.com/senzing/senzing-tools/constant"
"github.com/senzing/senzing-tools/envar"
"github.com/senzing/senzing-tools/helper"
"github.com/senzing/senzing-tools/option"
"github.com/senzing/servegrpc/grpcserver"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
buildIteration string = "0"
buildVersion string = "0.3.7"
configurationFile string
const (
defaultConfiguration string = ""
defaultDatabaseUrl string = ""
defaultEnableG2config bool = false
defaultEnableG2configmgr bool = false
defaultEnableG2diagnostic bool = false
defaultEnableG2engine bool = false
defaultEnableG2product bool = false
defaultEngineConfigurationJson string = ""
defaultEngineLogLevel int = 0
defaultEngineModuleName string = fmt.Sprintf("initdatabase-%d", time.Now().Unix())
defaultGrpcPort int = 8258
defaultLogLevel string = "INFO"
)

func makeVersion(version string, iteration string) string {
result := ""
if buildIteration == "0" {
result = version
} else {
result = fmt.Sprintf("%s-%s", buildVersion, buildIteration)
}
return result
}

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "servegrpc",
Short: "Start a gRPC server for the Senzing SDK API",
Long: `
Start a gRPC server for the Senzing SDK API.
For more information, visit https://github.com/Senzing/servegrpc
`,
PreRun: func(cobraCommand *cobra.Command, args []string) {

// Integrate with Viper.
var (
buildIteration string = "0"
buildVersion string = "0.3.7"
defaultEngineModuleName string = fmt.Sprintf("initdatabase-%d", time.Now().Unix())
)

replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("SENZING_TOOLS")
// If a configuration file is present, load it.
func loadConfigurationFile(cobraCommand *cobra.Command) {
configuration := cobraCommand.Flags().Lookup(option.Configuration).Value.String()
if configuration != "" { // Use configuration file specified as a command line option.
viper.SetConfigFile(configuration)
} else { // Search for a configuration file.

// Define flags in Viper.
// Determine home directory.

viper.SetDefault("enable-g2config", false)
viper.BindPFlag("enable-g2config", cobraCommand.Flags().Lookup("enable-g2config"))
home, err := os.UserHomeDir()
cobra.CheckErr(err)

viper.SetDefault("enable-g2configmgr", false)
viper.BindPFlag("enable-g2configmgr", cobraCommand.Flags().Lookup("enable-g2configmgr"))
// Specify configuration file name.

viper.SetDefault("enable-g2diagnostic", false)
viper.BindPFlag("enable-g2diagnostic", cobraCommand.Flags().Lookup("enable-g2diagnostic"))
viper.SetConfigName("servegrpc")
viper.SetConfigType("yaml")

viper.SetDefault("enable-g2engine", false)
viper.BindPFlag("enable-g2engine", cobraCommand.Flags().Lookup("enable-g2engine"))
// Define search path order.

viper.SetDefault("enable-g2product", false)
viper.BindPFlag("enable-g2product", cobraCommand.Flags().Lookup("enable-g2product"))
viper.AddConfigPath(home + "/.senzing-tools")
viper.AddConfigPath(home)
viper.AddConfigPath("/etc/senzing-tools")
}

viper.SetDefault("engine-log-level", defaultEngineLogLevel)
viper.BindPFlag("engine-log-level", cobraCommand.Flags().Lookup("engine-log-level"))
// If a config file is found, read it in.

viper.SetDefault("grpc-port", defaultGrpcPort)
viper.BindPFlag("grpc-port", cobraCommand.Flags().Lookup("grpc-port"))
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Applying configuration file:", viper.ConfigFileUsed())
}
}

viper.SetDefault("database-url", defaultDatabaseUrl)
viper.BindPFlag("database-url", cobraCommand.Flags().Lookup("database-url"))
// Configure Viper with user-specified options.
func loadOptions(cobraCommand *cobra.Command) {
viper.AutomaticEnv()
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix(constant.SetEnvPrefix)

// Bools

boolOptions := map[string]bool{
option.EnableG2config: defaultEnableG2config,
option.EnableG2configmgr: defaultEnableG2configmgr,
option.EnableG2diagnostic: defaultEnableG2diagnostic,
option.EnableG2engine: defaultEnableG2engine,
option.EnableG2product: defaultEnableG2product,
}
for optionKey, optionValue := range boolOptions {
viper.SetDefault(optionKey, optionValue)
viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
}

viper.SetDefault("engine-configuration-json", defaultEngineConfigurationJson)
viper.BindPFlag("engine-configuration-json", cobraCommand.Flags().Lookup("engine-configuration-json"))
// Ints

viper.SetDefault("engine-module-name", defaultEngineModuleName)
viper.BindPFlag("engine-module-name", cobraCommand.Flags().Lookup("engine-module-name"))
intOptions := map[string]int{
option.EngineLogLevel: defaultEngineLogLevel,
option.GrpcPort: defaultGrpcPort,
}
for optionKey, optionValue := range intOptions {
viper.SetDefault(optionKey, optionValue)
viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
}

viper.SetDefault("log-level", defaultLogLevel)
viper.BindPFlag("log-level", cobraCommand.Flags().Lookup("log-level"))
// Strings

// Set version template.
stringOptions := map[string]string{
option.Configuration: defaultConfiguration,
option.DatabaseUrl: defaultDatabaseUrl,
option.EngineConfigurationJson: defaultEngineConfigurationJson,
option.EngineModuleName: defaultEngineModuleName,
option.LogLevel: defaultLogLevel,
}
for optionKey, optionValue := range stringOptions {
viper.SetDefault(optionKey, optionValue)
viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
}
}

versionTemplate := `{{printf "%s: %s - version %s\n" .Name .Short .Version}}`
cobraCommand.SetVersionTemplate(versionTemplate)
// RootCmd represents the command.
var RootCmd = &cobra.Command{
Use: "servegrpc",
Short: "Start a gRPC server for the Senzing SDK API",
Long: `
Start a gRPC server for the Senzing SDK API.
For more information, visit https://github.com/Senzing/servegrpc
`,
PreRun: func(cobraCommand *cobra.Command, args []string) {
loadConfigurationFile(cobraCommand)
loadOptions(cobraCommand)
cobraCommand.SetVersionTemplate(constant.VersionTemplate)
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error = nil
ctx := context.TODO()

logLevel, ok := logger.TextToLevelMap[viper.GetString("log-level")]
logLevel, ok := logger.TextToLevelMap[viper.GetString(option.LogLevel)]
if !ok {
logLevel = logger.LevelInfo
}

senzingEngineConfigurationJson := viper.GetString("engine-configuration-json")
senzingEngineConfigurationJson := viper.GetString(option.EngineConfigurationJson)
if len(senzingEngineConfigurationJson) == 0 {
senzingEngineConfigurationJson, err = g2engineconfigurationjson.BuildSimpleSystemConfigurationJson(viper.GetString("database-url"))
senzingEngineConfigurationJson, err = g2engineconfigurationjson.BuildSimpleSystemConfigurationJson(viper.GetString(option.DatabaseUrl))
if err != nil {
return err
}
}

grpcserver := &grpcserver.GrpcServerImpl{
EnableG2config: viper.GetBool("enable-g2config"),
EnableG2configmgr: viper.GetBool("enable-g2configmgr"),
EnableG2diagnostic: viper.GetBool("enable-g2diagnostic"),
EnableG2engine: viper.GetBool("enable-g2engine"),
EnableG2product: viper.GetBool("enable-g2product"),
Port: viper.GetInt("grpc-port"),
EnableG2config: viper.GetBool(option.EnableG2config),
EnableG2configmgr: viper.GetBool(option.EnableG2configmgr),
EnableG2diagnostic: viper.GetBool(option.EnableG2diagnostic),
EnableG2engine: viper.GetBool(option.EnableG2engine),
EnableG2product: viper.GetBool(option.EnableG2product),
Port: viper.GetInt(option.GrpcPort),
LogLevel: logLevel,
SenzingEngineConfigurationJson: senzingEngineConfigurationJson,
SenzingModuleName: viper.GetString("engine-module-name"),
SenzingVerboseLogging: viper.GetInt("engine-log-level"),
SenzingModuleName: viper.GetString(option.EngineModuleName),
SenzingVerboseLogging: viper.GetInt(option.EngineLogLevel),
}
err = grpcserver.Serve(ctx)
return err
},
Version: makeVersion(buildVersion, buildIteration),
Version: helper.MakeVersion(buildVersion, buildIteration),
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
err := RootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

// Since init() is always invoked, define command line parameters.
func init() {
cobra.OnInitialize(initConfig)

// Define flags for Cobra command.

RootCmd.Flags().Bool("enable-g2config", false, "Enable G2Config service [SENZING_TOOLS_ENABLE_G2CONFIG]")
RootCmd.Flags().Bool("enable-g2configmgr", false, "Enable G2ConfigMgr service [SENZING_TOOLS_ENABLE_G2CONFIGMGR]")
RootCmd.Flags().Bool("enable-g2diagnostic", false, "Enable G2Diagnostic service [SENZING_TOOLS_ENABLE_G2DIAGNOSTIC]")
RootCmd.Flags().Bool("enable-g2engine", false, "Enable G2Config service [SENZING_TOOLS_ENABLE_G2ENGINE]")
RootCmd.Flags().Bool("enable-g2product", false, "Enable G2Config service [SENZING_TOOLS_ENABLE_G2PRODUCT]")
RootCmd.Flags().Int("engine-log-level", defaultEngineLogLevel, "Log level for Senzing Engine [SENZING_TOOLS_ENGINE_LOG_LEVEL]")
RootCmd.Flags().Int("grpc-port", defaultGrpcPort, "Port used to serve gRPC [SENZING_TOOLS_GRPC_PORT]")
RootCmd.Flags().String("database-url", defaultDatabaseUrl, "URL of database to initialize [SENZING_TOOLS_DATABASE_URL]")
RootCmd.Flags().String("engine-configuration-json", defaultEngineConfigurationJson, "JSON string sent to Senzing's init() function [SENZING_TOOLS_ENGINE_CONFIGURATION_JSON]")
RootCmd.Flags().String("engine-module-name", defaultEngineModuleName, "Identifier given to the Senzing engine [SENZING_TOOLS_ENGINE_MODULE_NAME]")
RootCmd.Flags().String("log-level", defaultLogLevel, "Log level of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or PANIC [SENZING_TOOLS_LOG_LEVEL]")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if configurationFile != "" {
// Use config file from the flag.
viper.SetConfigFile(configurationFile)
} else {

// Find home directory.

home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".senzing-tools" (without extension).

viper.AddConfigPath(home + "/.senzing-tools")
viper.AddConfigPath(home)
viper.AddConfigPath("/etc/senzing-tools")
viper.SetConfigType("yaml")
viper.SetConfigName("servegrpc")
}

// Read in environment variables that match "SENZING_TOOLS_*" pattern.

viper.AutomaticEnv()

// If a config file is found, read it in.

if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
RootCmd.Flags().Bool(option.EnableG2config, defaultEnableG2config, fmt.Sprintf("Enable G2Config service [%s]", envar.EnableG2config))
RootCmd.Flags().Bool(option.EnableG2configmgr, defaultEnableG2configmgr, fmt.Sprintf("Enable G2ConfigMgr service [%s]", envar.EnableG2configmgr))
RootCmd.Flags().Bool(option.EnableG2diagnostic, defaultEnableG2diagnostic, fmt.Sprintf("Enable G2Diagnostic service [%s]", envar.EnableG2diagnostic))
RootCmd.Flags().Bool(option.EnableG2engine, defaultEnableG2engine, fmt.Sprintf("Enable G2Config service [%s]", envar.EnableG2engine))
RootCmd.Flags().Bool(option.EnableG2product, defaultEnableG2product, fmt.Sprintf("Enable G2Config service [%s]", envar.EnableG2product))
RootCmd.Flags().Int(option.EngineLogLevel, defaultEngineLogLevel, fmt.Sprintf("Log level for Senzing Engine [%s]", envar.EngineLogLevel))
RootCmd.Flags().Int(option.GrpcPort, defaultGrpcPort, fmt.Sprintf("Port used to serve gRPC [%s]", envar.GrpcPort))
RootCmd.Flags().String(option.Configuration, defaultConfiguration, fmt.Sprintf("Path to configuration file [%s]", envar.Configuration))
RootCmd.Flags().String(option.DatabaseUrl, defaultDatabaseUrl, fmt.Sprintf("URL of database to initialize [%s]", envar.DatabaseUrl))
RootCmd.Flags().String(option.EngineConfigurationJson, defaultEngineConfigurationJson, fmt.Sprintf("JSON string sent to Senzing's init() function [%s]", envar.EngineConfigurationJson))
RootCmd.Flags().String(option.EngineModuleName, defaultEngineModuleName, fmt.Sprintf("Identifier given to the Senzing engine [%s]", envar.EngineModuleName))
RootCmd.Flags().String(option.LogLevel, defaultLogLevel, fmt.Sprintf("Log level of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or PANIC [%s]", envar.LogLevel))
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ go 1.20
require (
github.com/aquilax/truncate v1.0.0
github.com/senzing/g2-sdk-go v0.4.1
github.com/senzing/g2-sdk-go-base v0.1.1
github.com/senzing/g2-sdk-go-base v0.1.2
github.com/senzing/g2-sdk-proto/go v0.0.0-20230310202231-e2f8d3edf4b8
github.com/senzing/go-common v0.1.2
github.com/senzing/go-logging v1.1.3
github.com/senzing/go-observing v0.2.0
github.com/senzing/senzing-tools v0.1.4
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
Expand Down Expand Up @@ -37,7 +38,7 @@ require (
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/protobuf v1.29.0 // indirect
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/senzing/g2-sdk-go v0.4.1 h1:McZVlNweYtp4rh1AKdOeu0p4J5cPUdBv/z09W6WHRqE=
github.com/senzing/g2-sdk-go v0.4.1/go.mod h1:tz+pX1kT4S5ooDb0DwO8gkSAYZcpPn/jFB0oZfssd84=
github.com/senzing/g2-sdk-go-base v0.1.1 h1:or8MelMRn3iXhE1YGSQE70GGEjFl6HA/2JCXFM27eZo=
github.com/senzing/g2-sdk-go-base v0.1.1/go.mod h1:sy+Qeol8MNYB52SjgGiRzpHD3b5Va6FlXaDq4eQPjCw=
github.com/senzing/g2-sdk-go-base v0.1.2 h1:KyxEk7jMiJjPZ9zEAWlatTw5Cha5foYZaEMR+LUBMpE=
github.com/senzing/g2-sdk-go-base v0.1.2/go.mod h1:eP463BSCJQdUxxa/V68crxpprtyacG0sWSGcSk96RL0=
github.com/senzing/g2-sdk-proto/go v0.0.0-20230310202231-e2f8d3edf4b8 h1:JliRWMBMR45nJbt/wsWyeqDTZL4BnibZzQvU4j5CapE=
github.com/senzing/g2-sdk-proto/go v0.0.0-20230310202231-e2f8d3edf4b8/go.mod h1:c8P58slWygCjkg2apMDZfoYXRD97AYuXPx9+gKqm7Ik=
github.com/senzing/go-common v0.1.2 h1:d4E4cyKAqBvHvexLvj51WwoU78jTbLlBXmE67o41CM8=
Expand All @@ -169,6 +169,8 @@ github.com/senzing/go-logging v1.1.3 h1:eTWuEgI+4bwyS0gSpYKNbz8gL8ATAqByA3q1sbRJ
github.com/senzing/go-logging v1.1.3/go.mod h1:FarStyY/kYd7+ymtawOXvOk48j/6fHRZ8unSwpi4FzI=
github.com/senzing/go-observing v0.2.0 h1:QTFFTaZJ/1S2u96N17w2aCh/AHGHSOZCwX+VNU5v6tc=
github.com/senzing/go-observing v0.2.0/go.mod h1:M5zrUXIYC4dL1fevBezB+aXIaU6uut/wynlQ93OcZhg=
github.com/senzing/senzing-tools v0.1.4 h1:ubixuMJEEej/6v4bmKTvqDYNuGoVl8pf51iEj0ctvpE=
github.com/senzing/senzing-tools v0.1.4/go.mod h1:7xHqJlD9xTkDxGMvrtaCMZu+pBPQCyDzbFyOMOC01XI=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
Expand Down Expand Up @@ -495,8 +497,8 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.29.0 h1:44S3JjaKmLEE4YIkjzexaP+NzZsudE3Zin5Njn/pYX0=
google.golang.org/protobuf v1.29.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM=
google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down