-
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.
* #27 Add cmd functionality * #27 Add cmd functionality * #27 Add cmd functionality * #27 Prepare for versioned release
- Loading branch information
Showing
11 changed files
with
760 additions
and
49 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
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 |
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,11 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
/* | ||
* The unit tests in this file simulate command line invocation. | ||
*/ | ||
func TestMain(testing *testing.T) { | ||
} |
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,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) | ||
} |
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,4 @@ | ||
/* | ||
The cmd package is used for Cobra integration. | ||
*/ | ||
package cmd |
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,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 | ||
} |
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,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" |
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,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)) | ||
} |
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.