Skip to content

Commit b1a406f

Browse files
authored
Add --suppress-error arg to silence runtime errors (#24)
1 parent 373f1de commit b1a406f

File tree

4 files changed

+79
-21
lines changed

4 files changed

+79
-21
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ The `dynatrace-bootstrapper` is a small CLI binary built into a [Dynatrace CodeM
9797
- This is an **optional** arg
9898
- The `--attribute-container` arg defines the passed in Container attributes that will be used to configure the metadata-enrichment and injected CodeModule. It is a JSON formatted string.
9999
100+
#### `--suppress-error`
101+
102+
*Example*: `--suppress-error`
103+
104+
- This is an **optional** arg
105+
- Defaults to `false`
106+
- The `--suppress-error` arg will silence any errors, causing the executable to return with an exit code 0 even if an error occurred. Intended purpose is to not block the application container from starting, if used as init-container.
107+
108+
#### `--debug`
109+
110+
*Example*: `--debug`
111+
112+
- This is an **optional** arg
113+
- Defaults to `false`
114+
- The `--debug` arg will enabled the debug logs.
115+
100116
## Development
101117
102118
- To run tests: `make test`

main.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import (
1515
)
1616

1717
const (
18-
sourceFolderFlag = "source"
19-
targetFolderFlag = "target"
20-
debugFlag = "debug"
18+
SourceFolderFlag = "source"
19+
TargetFolderFlag = "target"
20+
DebugFlag = "debug"
21+
SuppressErrorsFlag = "suppress-error"
2122
)
2223

2324
var (
24-
log logr.Logger
25-
isDebug bool
25+
log logr.Logger
26+
isDebug bool
27+
areErrorsSuppressed bool
2628

2729
sourceFolder string
2830
targetFolder string
@@ -52,13 +54,17 @@ func bootstrapper(fs afero.Fs) *cobra.Command {
5254
}
5355

5456
func AddFlags(cmd *cobra.Command) {
55-
cmd.PersistentFlags().StringVar(&sourceFolder, sourceFolderFlag, "", "Base path where to copy the codemodule FROM.")
56-
_ = cmd.MarkPersistentFlagRequired(sourceFolderFlag)
57+
cmd.PersistentFlags().StringVar(&sourceFolder, SourceFolderFlag, "", "Base path where to copy the codemodule FROM.")
58+
_ = cmd.MarkPersistentFlagRequired(SourceFolderFlag)
5759

58-
cmd.PersistentFlags().StringVar(&targetFolder, targetFolderFlag, "", "Base path where to copy the codemodule TO.")
59-
_ = cmd.MarkPersistentFlagRequired(targetFolderFlag)
60+
cmd.PersistentFlags().StringVar(&targetFolder, TargetFolderFlag, "", "Base path where to copy the codemodule TO.")
61+
_ = cmd.MarkPersistentFlagRequired(TargetFolderFlag)
6062

61-
cmd.PersistentFlags().BoolVar(&isDebug, debugFlag, false, "Enables debug logs.")
63+
cmd.PersistentFlags().BoolVar(&isDebug, DebugFlag, false, "(Optional) Enables debug logs.")
64+
cmd.PersistentFlags().Lookup(DebugFlag).NoOptDefVal = "true"
65+
66+
cmd.PersistentFlags().BoolVar(&areErrorsSuppressed, SuppressErrorsFlag, false, "(Optional) Always return exit code 0, even on error")
67+
cmd.PersistentFlags().Lookup(SuppressErrorsFlag).NoOptDefVal = "true"
6268
}
6369

6470
func run(fs afero.Fs) func(cmd *cobra.Command, _ []string) error {
@@ -69,21 +75,31 @@ func run(fs afero.Fs) func(cmd *cobra.Command, _ []string) error {
6975
}
7076
version.Print(log)
7177

72-
err := cmd.ValidateRequiredFlags()
73-
if err != nil {
74-
return err
75-
}
76-
7778
aferoFs := afero.Afero{
7879
Fs: fs,
7980
}
8081

81-
err = move.Execute(log, aferoFs, sourceFolder, targetFolder)
82+
err := move.Execute(log, aferoFs, sourceFolder, targetFolder)
8283
if err != nil {
84+
if areErrorsSuppressed {
85+
log.Error(err, "error during moving, the error was suppressed")
86+
87+
return nil
88+
}
89+
return err
90+
}
91+
92+
err = configure.Execute(log, aferoFs, targetFolder)
93+
if err != nil {
94+
if areErrorsSuppressed {
95+
log.Error(err, "error during configuration, the error was suppressed")
96+
97+
return nil
98+
}
8399
return err
84100
}
85101

86-
return configure.Execute(log, aferoFs, targetFolder)
102+
return nil
87103
}
88104
}
89105

main_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,30 @@ func TestBootstrapper(t *testing.T) {
2323

2424
require.NoError(t, err)
2525
})
26+
27+
t.Run("--suppress-error=true -> no error", func(t *testing.T) {
28+
cmd := bootstrapper(afero.NewMemMapFs())
29+
cmd.SetArgs([]string{"--source", "\\\\", "--target", "\\\\"})
30+
31+
err := cmd.Execute()
32+
33+
require.Error(t, err)
34+
35+
cmd = bootstrapper(afero.NewMemMapFs())
36+
// Note: we can't skip/mask the validation of the required flags
37+
cmd.SetArgs([]string{"--source", "\\\\", "--target", "\\\\", "--suppress-error"})
38+
39+
err = cmd.Execute()
40+
41+
require.NoError(t, err)
42+
43+
44+
cmd = bootstrapper(afero.NewMemMapFs())
45+
// Note: we can't skip/mask the validation of the required flags
46+
cmd.SetArgs([]string{"--source", "\\\\", "--target", "\\\\", "--suppress-error", "true"})
47+
48+
err = cmd.Execute()
49+
50+
require.NoError(t, err)
51+
})
2652
}

pkg/move/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
const (
10-
workFolderFlag = "work"
11-
technologyFlag = "technology"
10+
WorkFolderFlag = "work"
11+
TechnologyFlag = "technology"
1212
)
1313

1414
var (
@@ -17,9 +17,9 @@ var (
1717
)
1818

1919
func AddFlags(cmd *cobra.Command) {
20-
cmd.PersistentFlags().StringVar(&workFolder, workFolderFlag, "", "(Optional) Base path for a tmp folder, this is where the command will do its work, to make sure the operations are atomic. It must be on the same disk as the target folder.")
20+
cmd.PersistentFlags().StringVar(&workFolder, WorkFolderFlag, "", "(Optional) Base path for a tmp folder, this is where the command will do its work, to make sure the operations are atomic. It must be on the same disk as the target folder.")
2121

22-
cmd.PersistentFlags().StringVar(&technology, technologyFlag, "", "(Optional) Comma-separated list of technologies to filter files.")
22+
cmd.PersistentFlags().StringVar(&technology, TechnologyFlag, "", "(Optional) Comma-separated list of technologies to filter files.")
2323

2424
}
2525

0 commit comments

Comments
 (0)