Skip to content

Commit

Permalink
Issue 27.dockter.1 (#28)
Browse files Browse the repository at this point in the history
* #27 Add cmd functionality

* #27 Add cmd functionality

* #27 Add cmd functionality

* #27 Prepare for versioned release
  • Loading branch information
docktermj authored Mar 31, 2023
1 parent d4fb9d3 commit 9315749
Show file tree
Hide file tree
Showing 11 changed files with 760 additions and 49 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/make-go-github-file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: make-go-github-file.yaml

on:
push:
tags:
- "[0-9]+.[0-9]+.[0-9]+"

jobs:
build:
name: Update cmd/version.go
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: "0"
- name: Make github.go file
uses: Senzing/github-action-make-go-github-file@main
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

-

## [0.2.0] - 2023-03-31

### Changed in 0.2.0

- Added `cmd` directory for cobra/viper integration

## [0.1.1] - 2023-02-27

### Changed in 0.1.1
Expand Down
11 changes: 11 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cmd

import (
"testing"
)

/*
* The unit tests in this file simulate command line invocation.
*/
func TestMain(testing *testing.T) {
}
33 changes: 33 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*/
package cmd

import (
"io"
"os"

"github.com/spf13/cobra"
)

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generate bash completion for the command",
Long: `To load completions, run:
source < (template-go completion)
To load completions automaticallon on login, add this line to your .bashrc file:
source < (template-go completion)
`,
RunE: func(cmd *cobra.Command, args []string) error {
return completionAction(os.Stdout)
},
}

func init() {
RootCmd.AddCommand(completionCmd)
}

func completionAction(out io.Writer) error {
return RootCmd.GenBashCompletion(out)
}
4 changes: 4 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
The cmd package is used for Cobra integration.
*/
package cmd
43 changes: 43 additions & 0 deletions cmd/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*/
package cmd

import (
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)

// docsCmd represents the docs command
var docsCmd = &cobra.Command{
Use: "docs",
Short: "Generate documentation for the command",
RunE: func(cmd *cobra.Command, args []string) error {
dir, err := cmd.Flags().GetString("dir")
if err != nil {
return err
}
if dir == "" {
if dir, err = os.MkdirTemp("", "template-go"); err != nil {
return err
}
}
return docsAction(os.Stdout, dir)
},
}

func init() {
RootCmd.AddCommand(docsCmd)
docsCmd.Flags().StringP("dir", "d", "", "Destination directory for docs")
}

func docsAction(out io.Writer, dir string) error {
if err := doc.GenMarkdownTree(RootCmd, dir); err != nil {
return err
}
_, err := fmt.Fprintf(out, "Documentation successfully created in %s\n", dir)
return err
}
11 changes: 11 additions & 0 deletions cmd/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 0.0.1
// Created by make-go-github-file.yaml on Fri Mar 31 19:47:08 UTC 2023
package cmd

var githubDate string = "2023-03-31"
var githubIteration string = "0"
var githubRef string = "refs/tags/0.0.1"
var githubRefName string = "0.0.1"
var githubRepository string = "Senzing/template-go"
var githubRepositoryName string = "template-go"
var githubVersion string = "0.0.1"
129 changes: 129 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
*/
package cmd

import (
"context"
"fmt"
"os"
"strings"

"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/template-go/examplepackage"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
defaultConfiguration string = ""
defaultEngineConfigurationJson string = ""
defaultEngineLogLevel int = 0
defaultLogLevel string = "INFO"
)

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

// Determine home directory.

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

// Specify configuration file name.

viper.SetConfigName("template-go")
viper.SetConfigType("yaml")

// Define search path order.

viper.AddConfigPath(home + "/.senzing-tools")
viper.AddConfigPath(home)
viper.AddConfigPath("/etc/senzing-tools")
}

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

if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Applying configuration file:", viper.ConfigFileUsed())
}
}

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

// Ints

intOptions := map[string]int{
option.EngineLogLevel: defaultEngineLogLevel,
}
for optionKey, optionValue := range intOptions {
viper.SetDefault(optionKey, optionValue)
viper.BindPFlag(optionKey, cobraCommand.Flags().Lookup(optionKey))
}

// Strings

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

// RootCmd represents the command.
var RootCmd = &cobra.Command{
Use: "template-go",
Short: "template-go short description",
Long: `
template-go 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),
}

// 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.
func Execute() {
err := RootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

// 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))
}
23 changes: 22 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@ module github.com/senzing/template-go

go 1.20

require github.com/stretchr/testify v1.8.2
require (
github.com/senzing/senzing-tools v0.2.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.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.0.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9315749

Please sign in to comment.