Skip to content

Commit

Permalink
Merge pull request #79 from attestantio/fix-version
Browse files Browse the repository at this point in the history
Handle --version before parsing configuration.
  • Loading branch information
mcdee authored Feb 25, 2025
2 parents cf34ffc + b2cb29a commit 961bfd1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 50 deletions.
41 changes: 4 additions & 37 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,9 @@ run:

# output configuration options
output:
# Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions
#
# Multiple can be specified by separating them by comma, output can be provided
# for each of them by separating format name and path by colon symbol.
# Output path can be either `stdout`, `stderr` or path to the file to write to.
# Example: "checkstyle:report.json,colored-line-number"
#
# Default: colored-line-number
# format: json

# Print lines of code with issue.
# Default: true
# print-issued-lines: false

# Print linter name in the end of issue text.
# Default: true
# print-linter-name: false

# Make issues output unique by line.
# Default: true
# uniq-by-line: false

# Add a prefix to the output file references.
# Default is no prefix.
# path-prefix: ""

# Sort results by: filepath, line and column.
# sort-results: true

formats:
- format: colored-line-number
path: stderr

# All available settings of specific linters.
linters-settings:
Expand Down Expand Up @@ -148,30 +122,23 @@ linters:
# Disable specific linter
# https://golangci-lint.run/usage/linters/#disabled-by-default
disable:
- contextcheck
- cyclop
- depguard
- dupl
- err113
- exhaustive
- exhaustruct
- exportloopref
- forcetypeassert
- funlen
- gci
- gochecknoglobals
- gocognit
- goconst
- ireturn
- inamedparam
- lll
- mnd
- musttag
- nestif
- nilnil
- nlreturn
- perfsprint
- promlinter
- tenv
- varnamelen
- wrapcheck
- wsl
31 changes: 18 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ var ReleaseVersion = "1.2.1-rc.1"
func main() {
ctx, cancel := context.WithCancel(context.Background())

if err := fetchConfig(); err != nil {
exit, err := fetchConfig()
if err != nil {
zerologger.Fatal().Err(err).Msg("Failed to fetch configuration")
}
if exit {
os.Exit(0)
}

majordomoSvc, err := util.InitMajordomo(ctx)
if err != nil {
Expand Down Expand Up @@ -155,7 +159,8 @@ func main() {
}

// fetchConfig fetches configuration from various sources.
func fetchConfig() error {
// If this returns true then the calling code should exit.
func fetchConfig() (bool, error) {
pflag.String("base-dir", "", "base directory for configuration files")
pflag.String("log-level", "info", "minimum level of messsages to log")
pflag.String("log-file", "", "redirect log output to a file")
Expand All @@ -170,7 +175,12 @@ func fetchConfig() error {
pflag.String("slashing-protection-file", "", "location of slashing protection file for import or export")
pflag.Parse()
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
return errors.Wrap(err, "failed to bind pflags to viper")
return false, errors.Wrap(err, "failed to bind pflags to viper")
}

if viper.GetBool("version") {
fmt.Fprintf(os.Stdout, "%s\n", ReleaseVersion)
return true, nil
}

if viper.GetString("base-dir") != "" {
Expand All @@ -181,7 +191,7 @@ func fetchConfig() error {
// Home directory.
home, err := homedir.Dir()
if err != nil {
return errors.Wrap(err, "failed to obtain home directory")
return false, errors.Wrap(err, "failed to obtain home directory")
}
viper.AddConfigPath(home)
viper.SetConfigName(".dirk")
Expand All @@ -206,16 +216,16 @@ func fetchConfig() error {
// get very far anyway.
if viper.GetString("server.name") == "" {
// Assume the underlying issue is that the configuration file is missing.
return errors.Wrap(err, "could not find the configuration file")
return false, errors.Wrap(err, "could not find the configuration file")
}
case errors.As(err, &viper.ConfigParseError{}):
return errors.Wrap(err, "could not parse the configuration file")
return false, errors.Wrap(err, "could not parse the configuration file")
default:
return errors.Wrap(err, "failed to obtain configuration")
return false, errors.Wrap(err, "failed to obtain configuration")
}
}

return nil
return false, nil
}

// initProfiling initialises the profiling server.
Expand All @@ -237,11 +247,6 @@ func initProfiling() {
}

func runCommands(ctx context.Context, majordomoSvc majordomo.Service) (bool, int) {
if viper.GetBool("version") {
fmt.Fprintf(os.Stdout, "%s\n", ReleaseVersion)
return true, 0
}

if viper.GetBool("show-certificates") {
err := cmd.ShowCertificates(ctx, majordomoSvc)
if err != nil {
Expand Down

0 comments on commit 961bfd1

Please sign in to comment.