Skip to content

Commit

Permalink
[#73] add flag to optionally print stacktrace of the error
Browse files Browse the repository at this point in the history
  • Loading branch information
kozmod committed Mar 1, 2023
1 parent 6e44d64 commit b817593
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ___
|:----------------------------------------------|:--------:|:------------:|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `-f`[<sup>****</sup>](#config_file) | string | `progen.yml` | specify configuration file path |
| `-v` | bool | `false` | verbose output |
| `-errtrace` | bool | `false` | output errors stack trace |
| `-pf`[<sup>****</sup>](#files_preprocessing) | bool | `true` | `preprocessing files`: load and process all files <br/>(all files `actions`[<sup>****</sup>](#files_actio_desk)) as [text/template](https://pkg.go.dev/text/template) before creating |
| `-dr`[<sup>****</sup>](#dry_run) | bool | `false` | `dry run` mode <br/>(to verbose output should be combine with`-v`) |
| `-awd` | string | `.` | application working directory |
Expand Down
63 changes: 40 additions & 23 deletions internal/flag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,35 @@ import (
//goland:noinspection SpellCheckingInspection
const (
defaultConfigFilePath = "progen.yml"

flagKeyConfigFile = "f"
flagKeyVarbose = "v"
flagKeyErrorStackTrace = "errtrace"
flagKeyDryRun = "dr"
flagKeyVersion = "version"
flagKeyTemplateVariables = "tvar"
flagKeyApplicationWorkingDirectory = "awd"
flagKeySkip = "skip"
flagKeyPreprocessingAllFiles = "pf"
flagKeyMissingKey = "missingkey"
)

var (
ErrDashFlagNotLast = fmt.Errorf(`'-' must be the last argument`)
)

type Flags struct {
ConfigPath string
Verbose bool
DryRun bool
Version bool
ReadStdin bool
TemplateVars TemplateVarsFlag
AWD string // AWD application working directory
Skip SkipFlag
PreprocessFiles bool
MissingKey MissingKeyFlag
ConfigPath string
Verbose bool
DryRun bool
Version bool
ReadStdin bool
TemplateVars TemplateVarsFlag
AWD string // AWD application working directory
Skip SkipFlag
PreprocessFiles bool
MissingKey MissingKeyFlag
PrintErrorStackTrace bool
}

func (f *Flags) FileLocationMessage() string {
Expand Down Expand Up @@ -62,46 +74,51 @@ func parseFlags(fs *flag.FlagSet, args []string) (Flags, error) {
)
fs.StringVar(
&f.ConfigPath,
"f",
flagKeyConfigFile,
defaultConfigFilePath,
fmt.Sprintf("configuration file path (default: %s)", defaultConfigFilePath))
"configuration file path")
fs.BoolVar(
&f.Verbose,
"v",
flagKeyVarbose,
false,
"verbose output")
//goland:noinspection SpellCheckingInspection
fs.BoolVar(
&f.PrintErrorStackTrace,
flagKeyErrorStackTrace,
false,
"output errors stacktrace")
fs.BoolVar(
&f.DryRun,
"dr",
flagKeyDryRun,
false,
"dry run mode (can be combine with `-v`)")
fs.BoolVar(
&f.Version,
"version",
flagKeyVersion,
false,
"output version")
//goland:noinspection SpellCheckingInspection
fs.Var(
&f.TemplateVars,
"tvar",
flagKeyTemplateVariables,
"template variables (override config variables tree)")
fs.StringVar(
&f.AWD,
"awd",
flagKeyApplicationWorkingDirectory,
entity.Dot,
"application working directory (default: '.')")
"application working directory")
fs.Var(
&f.Skip,
"skip",
flagKeySkip,
"list of skipping 'yaml' tags")
fs.BoolVar(
&f.PreprocessFiles,
"pf",
flagKeyPreprocessingAllFiles,
true,
"preprocessing all files before saving (default: 'true')")
"preprocessing all files before saving")
fs.Var(
&f.MissingKey,
"missingkey",
flagKeyMissingKey,
fmt.Sprintf(
"`missingkey` template option: %v, %v, %v, %v",
entity.MissingKeyDefault,
Expand Down
7 changes: 4 additions & 3 deletions internal/flag/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_TemplateVarsFlag(t *testing.T) {
const (
usage = "parse vars"
setName = "template vars test flag set"
flagName = "tvar"
flagName = flagKeyTemplateVariables
)

var (
Expand Down Expand Up @@ -218,6 +218,7 @@ func Test_parseFlags(t *testing.T) {
const (
fsName = "testFlagSet"
v = "-v"
errstack = "-errtrace"
dr = "-dr"
f = "-f"
pf = "-pf"
Expand All @@ -231,10 +232,10 @@ func Test_parseFlags(t *testing.T) {

t.Run("success", func(t *testing.T) {
testFs := flag.NewFlagSet(fsName, flag.ContinueOnError)
flags, err := parseFlags(testFs, []string{v, dr, f, configPath})
flags, err := parseFlags(testFs, []string{v, errstack, dr, f, configPath})
assert.NoError(t, err)
assert.Equal(t,
Flags{Verbose: true, PreprocessFiles: true, DryRun: true, ConfigPath: configPath, AWD: dot},
Flags{Verbose: true, PrintErrorStackTrace: true, PreprocessFiles: true, DryRun: true, ConfigPath: configPath, AWD: dot},
flags)
})
t.Run("success_when_dash_last", func(t *testing.T) {
Expand Down
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,31 @@ func main() {
return
}

logFatalSuffixFn := func(s string) string {
const pv, v = "%+v", "%v"
if flags.PrintErrorStackTrace {
return s + pv
}
return s + v
}

logger, err := factory.NewLogger(flags.Verbose)
if err != nil {
log.Fatalf("create logger: %+v", err)
log.Fatalf(logFatalSuffixFn("create logger: "), err)
}
defer func() {
_ = logger.Sync()
}()

{
if err = os.Chdir(flags.AWD); err != nil {
logger.Fatalf("changes the application working directory: %+v", err)
logger.Fatalf(logFatalSuffixFn("changes the application working directory: "), xerrors.Errorf("%w", err))
}

var awd string
awd, err = os.Getwd()
if err != nil {
logger.Fatalf("get the application working directory: %+v", err)
logger.Fatalf(logFatalSuffixFn("get the application working directory: "), xerrors.Errorf("%w", err))
}
logger.Infof("application working directory: %s", awd)
}
Expand All @@ -54,7 +62,7 @@ func main() {

data, err := config.NewConfigReader(flags).Read()
if err != nil {
logger.Fatalf("read config: %+v", err)
logger.Fatalf(logFatalSuffixFn("read config: "), err)
}

rawConfig, templateData, err := config.NewRawPreprocessor(
Expand All @@ -65,7 +73,7 @@ func main() {
).
Process(data)
if err != nil {
logger.Fatalf("preprocess raw config: %+v", err)
logger.Fatalf(logFatalSuffixFn("preprocess raw config: "), err)
}

var (
Expand All @@ -84,7 +92,7 @@ func main() {
})

if err = eg.Wait(); err != nil {
logger.Fatalf("prepare config: %+v", err)
logger.Fatalf(logFatalSuffixFn("prepare config: "), err)
}

procChain, err := factory.NewExecutorChain(
Expand All @@ -95,11 +103,11 @@ func main() {
flags.PreprocessFiles,
flags.DryRun)
if err != nil {
logger.Fatalf("create processors chain: %+v", err)
logger.Fatalf(logFatalSuffixFn("create processors chain: "), err)
}

err = procChain.Exec()
if err != nil {
logger.Fatalf("execute chain: %+v", err)
logger.Fatalf(logFatalSuffixFn("execute chain: "), err)
}
}

0 comments on commit b817593

Please sign in to comment.