Skip to content

Commit

Permalink
Merge pull request #4 from Senzing/3-dockter-1
Browse files Browse the repository at this point in the history
3 dockter 1
  • Loading branch information
docktermj authored May 17, 2023
2 parents 8111ece + f351d3b commit 367b0a7
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 176 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/go-security.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Based on
# - https://github.com/securego/gosec

name: go-security.yaml
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
tests:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- name: Checkout Source
uses: actions/checkout@v3
- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
args: ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

# Go workspace file
go.work
.history
20 changes: 4 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Thing 5
- Thing 4
-

## [1.0.1] - yyyy-mm-dd
## [0.1.0] - 2023-05-17

### Added to 1.0.1
### Added to 0.1.0

- Thing 3

### Fixed in 1.0.1

- Thing 2

## [1.0.0] - yyyy-mm-dd

### Added to 1.0.0

- Thing 2
- Thing 1
- Initial functionality
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,102 @@
# observe
# observe

## :warning: WARNING: observe is still in development :warning: _

At the moment, this is "work-in-progress" with Semantic Versions of `0.n.x`.
Although it can be reviewed and commented on,
the recommendation is not to use it yet.

## Synopsis

`observe` is a command in the
[senzing-tools](https://github.com/Senzing/senzing-tools)
suite of tools.
This command receives
[Observer](https://github.com/Senzing/go-observing)
messages over gRPC.

[![Go Reference](https://pkg.go.dev/badge/github.com/senzing/observe.svg)](https://pkg.go.dev/github.com/senzing/observe)
[![Go Report Card](https://goreportcard.com/badge/github.com/senzing/observe)](https://goreportcard.com/report/github.com/senzing/observe)
[![go-test.yaml](https://github.com/Senzing/observe/actions/workflows/go-test.yaml/badge.svg)](https://github.com/Senzing/observe/actions/workflows/go-test.yaml)
[![License](https://img.shields.io/badge/License-Apache2-brightgreen.svg)](https://github.com/Senzing/observe/blob/main/LICENSE)

## Overview

`observe` performs the following:

## Install

1. The `observe` command is installed with the
[senzing-tools](https://github.com/Senzing/senzing-tools)
suite of tools.
See senzing-tools [install](https://github.com/Senzing/senzing-tools#install).

## Use

```console
export LD_LIBRARY_PATH=/opt/senzing/g2/lib/
senzing-tools observe [flags]
```

1. For options and flags:
1. [Online documentation](https://hub.senzing.com/senzing-tools/senzing-tools_observe.html)
1. Runtime documentation:

```console
export LD_LIBRARY_PATH=/opt/senzing/g2/lib/
senzing-tools observe --help
```

1. In addition to the following simple usage examples, there are additional [Examples](docs/examples.md).

### Using command line options

1. :pencil2: Specify database using command line option.
Example:

```console
export LD_LIBRARY_PATH=/opt/senzing/g2/lib/
senzing-tools observe
```

1. See [Parameters](#parameters) for additional parameters.

### Using environment variables

1. :pencil2: Specify database using environment variable.
Example:

```console
export SENZING_TOOLS_DATABASE_URL=postgresql://username:password@postgres.example.com:5432/G2
export LD_LIBRARY_PATH=/opt/senzing/g2/lib/
senzing-tools observe
```

1. See [Parameters](#parameters) for additional parameters.

### Using Docker

This usage shows how to initialze a database with a Docker container.

1. :pencil2: Run `senzing/senzing-tools`.
Example:

```console
docker run \
--env SENZING_TOOLS_DATABASE_URL=postgresql://username:password@postgres.example.com:5432/G2 \
--rm \
senzing/senzing-tools observe
```

1. See [Parameters](#parameters) for additional parameters.

### Parameters

- **[SENZING_TOOLS_LOG_LEVEL](https://github.com/Senzing/knowledge-base/blob/main/lists/environment-variables.md#senzing_tools_log_level)**

## References

- [Command reference](https://hub.senzing.com/senzing-tools/senzing-tools_observe.html)
- [Development](docs/development.md)
- [Errors](docs/errors.md)
- [Examples](docs/examples.md)
17 changes: 8 additions & 9 deletions cmd/github.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// 0.2.1
// 0.1.0
// Created by make-go-github-file.yaml on Fri Mar 31 21:16:54 UTC 2023
package cmd

var githubDate string = "2023-03-31"
var githubIteration string = "0"
var githubRef string = "refs/tags/0.2.1"
var githubRefName string = "0.2.1"
var githubRepository string = "Senzing/observe"
var githubRepositoryName string = "observe"
var githubVersion string = "0.2.1"

var githubDate string = "2023-05-12"
var githubIteration string = "0"
var githubRef string = "refs/tags/0.1.0"
var githubRefName string = "0.1.0"
var githubRepository string = "Senzing/observe"
var githubRepositoryName string = "observe"
var githubVersion string = "0.1.0"
117 changes: 79 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,37 @@ import (
"os"
"strings"

"github.com/senzing/observe/observer"
"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/observe/examplepackage"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
)

const (
defaultConfiguration string = ""
defaultEngineConfigurationJson string = ""
defaultEngineLogLevel int = 0
defaultLogLevel string = "INFO"
defaultLogLevel string = "INFO"
defaultObserverGrpcPort int = 8260
Short string = "Aggregate observations"
Use string = "observe"
Long string = `
Listen for Observer messages over gRPC and print them to STDOUT.
`
)

// ----------------------------------------------------------------------------
// Private functions
// ----------------------------------------------------------------------------

// Since init() is always invoked, define command line parameters.
func init() {
RootCmd.Flags().Int("observer-grpc-port", defaultObserverGrpcPort, fmt.Sprintf("Port used to listen to Observer messages over gRPC [%s]", "SENZING_TOOLS_OBSERVER_GRPC_PORT"))
RootCmd.Flags().String(option.LogLevel, defaultLogLevel, fmt.Sprintf("Log level of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or PANIC [%s]", envar.LogLevel))
// RootCmd.Flags().Int(option.ObserverGrpcPort, defaultObserverGrpcPort, fmt.Sprintf("Port used to listen to Observer messages over gRPC [%s]", envar.ObserverGrpcPort))
}

// If a configuration file is present, load it.
func loadConfigurationFile(cobraCommand *cobra.Command) {
configuration := ""
Expand Down Expand Up @@ -61,6 +76,7 @@ func loadConfigurationFile(cobraCommand *cobra.Command) {

// Configure Viper with user-specified options.
func loadOptions(cobraCommand *cobra.Command) {
var err error = nil
viper.AutomaticEnv()
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
Expand All @@ -69,48 +85,34 @@ func loadOptions(cobraCommand *cobra.Command) {
// Ints

intOptions := map[string]int{
option.EngineLogLevel: defaultEngineLogLevel,
"observer-grpc-port": defaultObserverGrpcPort,
// option.ObserverGrpcPort: defaultObserverGrpcPort,
}
for optionKey, optionValue := range intOptions {
viper.SetDefault(optionKey, optionValue)
viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
err = viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
if err != nil {
panic(err)
}
}

// Strings

stringOptions := map[string]string{
option.EngineConfigurationJson: defaultEngineConfigurationJson,
option.LogLevel: defaultLogLevel,
option.LogLevel: defaultLogLevel,
}
for optionKey, optionValue := range stringOptions {
viper.SetDefault(optionKey, optionValue)
viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
err = viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
if err != nil {
panic(err)
}
}
}

// RootCmd represents the command.
var RootCmd = &cobra.Command{
Use: "observe",
Short: "observe short description",
Long: `
observe long description.
`,
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()
examplePackage := &examplepackage.ExamplePackageImpl{
Something: "Main says 'Hi!'",
}
err = examplePackage.SaySomething(ctx)
return err
},
Version: helper.MakeVersion(githubVersion, githubIteration),
}
// ----------------------------------------------------------------------------
// Public functions
// ----------------------------------------------------------------------------

// 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.
Expand All @@ -121,9 +123,48 @@ func Execute() {
}
}

// Since init() is always invoked, define command line parameters.
func init() {
RootCmd.Flags().Int(option.EngineLogLevel, defaultEngineLogLevel, fmt.Sprintf("Log level for Senzing Engine [%s]", envar.EngineLogLevel))
RootCmd.Flags().String(option.Configuration, defaultConfiguration, fmt.Sprintf("Path to configuration file [%s]", envar.Configuration))
RootCmd.Flags().String(option.EngineConfigurationJson, defaultEngineConfigurationJson, fmt.Sprintf("JSON string sent to Senzing's init() function [%s]", envar.EngineConfigurationJson))
// Used in construction of cobra.Command
func PreRun(cobraCommand *cobra.Command, args []string) {
loadConfigurationFile(cobraCommand)
loadOptions(cobraCommand)
cobraCommand.SetVersionTemplate(constant.VersionTemplate)
}

// Used in construction of cobra.Command
func RunE(_ *cobra.Command, _ []string) error {
var err error = nil
ctx := context.TODO()

// TODO: Support various gRPC server options.

serverOptions := []grpc.ServerOption{}

// Create and run gRPC server.

observer := &observer.ObserverImpl{
Port: viper.GetInt("observer-grpc-port"),
// Port: viper.GetInt(option.ObserverGrpcPort),
ServerOptions: serverOptions,
}
err = observer.Serve(ctx)
return err
}

// Used in construction of cobra.Command
func Version() string {
return helper.MakeVersion(githubVersion, githubIteration)
}

// ----------------------------------------------------------------------------
// Command
// ----------------------------------------------------------------------------

// RootCmd represents the command.
var RootCmd = &cobra.Command{
Use: Use,
Short: Short,
Long: Long,
PreRun: PreRun,
RunE: RunE,
Version: Version(),
}
12 changes: 0 additions & 12 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@

1. See Go's [Download and install](https://go.dev/doc/install)

## Install Senzing C library

Since the Senzing library is a prerequisite, it must be installed first.

1. Verify Senzing C shared objects, configuration, and SDK header files are installed.
1. `/opt/senzing/g2/lib`
1. `/opt/senzing/g2/sdk/c`
1. `/etc/opt/senzing`

1. If not installed, see
[How to Install Senzing for Go Development](https://github.com/Senzing/knowledge-base/blob/main/HOWTO/install-senzing-for-go-development.md).

## Install Git repository

1. Identify git repository.
Expand Down
Loading

0 comments on commit 367b0a7

Please sign in to comment.