-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup
main
func & use ctx
everywhere
The CLI parsing utilities are already provided by our GO library, so this has been removed from the `main` function. The `context.Context` related changes are necessary as otherwise you will have to wait for `5m` when having Icinga Notifications started with invalid database Config due to the internal database.Connect retrials. Lastly, the logging related changes are useful as we want to properly render the stack traces when we unexpectedly exit from the main function instead of wrapping the error in a confusing `zap.Error()` json key.
- Loading branch information
Showing
5 changed files
with
86 additions
and
104 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
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
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,57 @@ | ||
package daemon | ||
|
||
import ( | ||
"errors" | ||
"github.com/icinga/icinga-go-library/config" | ||
"github.com/icinga/icinga-go-library/utils" | ||
"github.com/icinga/icinga-notifications/internal" | ||
"os" | ||
) | ||
|
||
const ( | ||
ExitSuccess = 0 | ||
ExitFailure = 1 | ||
) | ||
|
||
// daemonConfig holds the configuration state as a singleton. | ||
// It is initialised by the [Init] func and exposed through the [Config] function. | ||
var daemonConfig *ConfigFile | ||
|
||
// Config returns the config that was loaded while starting the daemon. | ||
// Panics when the global *[ConfigFile] instance isn't initialised yet. | ||
func Config() *ConfigFile { | ||
if daemonConfig == nil { | ||
panic("ERROR: daemon.Config not initialized") | ||
} | ||
|
||
return daemonConfig | ||
} | ||
|
||
// Init first parses the CLI flags provided to the executable and tries to load the config from the YAML file. | ||
// Init prints any error during parsing or config loading to [os.Stderr] and exits. | ||
func Init() { | ||
var flags Flags | ||
if err := config.ParseFlags(&flags); err != nil { | ||
if errors.Is(err, config.ErrInvalidArgument) { | ||
panic(err) | ||
} | ||
|
||
utils.PrintErrorThenExit(err, ExitFailure) | ||
} | ||
|
||
if flags.Version { | ||
internal.Version.Print() | ||
os.Exit(ExitSuccess) | ||
} | ||
|
||
cfg := new(ConfigFile) | ||
if err := config.FromYAMLFile(flags.Config, cfg); err != nil { | ||
if errors.Is(err, config.ErrInvalidArgument) { | ||
panic(err) | ||
} | ||
|
||
utils.PrintErrorThenExit(err, ExitFailure) | ||
} | ||
|
||
daemonConfig = cfg | ||
} |