-
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.
Move cmd helpers into cmd/pcert. Update dependencies. Get rid of Merger and instead use WithEnv cobra helper.
- Loading branch information
Showing
22 changed files
with
130 additions
and
363 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 was deleted.
Oops, something went wrong.
File renamed without changes.
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,62 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
Check failure on line 9 in cmd/pcert/cobra.go GitHub Actions / test
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
func WithEnv(c *cobra.Command) *cobra.Command { | ||
if c.HasParent() { | ||
c = c.Root() | ||
} | ||
|
||
args := os.Args[1:] | ||
|
||
var ( | ||
cmd *cobra.Command | ||
err error | ||
) | ||
if c.TraverseChildren { | ||
cmd, _, err = c.Traverse(args) | ||
} else { | ||
cmd, _, err = c.Find(args) | ||
} | ||
|
||
if err != nil { | ||
fmt.Println("ERRROR hier") | ||
return c | ||
} | ||
|
||
var errs []error | ||
for _, fs := range []*pflag.FlagSet{ | ||
cmd.Flags(), | ||
cmd.PersistentFlags(), | ||
} { | ||
fs.VisitAll(func(f *pflag.Flag) { | ||
optName := strings.ToUpper(f.Name) | ||
optName = strings.ReplaceAll(optName, "-", "_") | ||
varName := envVarPrefix + optName | ||
if val, ok := os.LookupEnv(varName); ok { | ||
err := f.Value.Set(val) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("invalid environment variable '%s': %w", varName, err)) | ||
} | ||
f.Changed = true | ||
} | ||
}) | ||
} | ||
if len(errs) != 0 { | ||
// we want to report the error after errors from the normal | ||
// parsing that for example a --help would still take effect. | ||
// to report the error we just overwrite PreRunE | ||
cmd.PreRunE = func(cmd *cobra.Command, args []string) error { | ||
return errors.Join(errs...) | ||
} | ||
} | ||
return c | ||
} |
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
2 changes: 1 addition & 1 deletion
2
cmd/extkeyusage_value_test.go → cmd/pcert/extkeyusage_value_test.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
File renamed without changes.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import "time" | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cmd | ||
package main | ||
|
||
import ( | ||
"crypto/x509" | ||
|
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
module github.com/dvob/pcert | ||
|
||
go 1.18 | ||
go 1.22 | ||
|
||
require ( | ||
github.com/spf13/cobra v1.1.1 | ||
github.com/spf13/cobra v1.8.1 | ||
github.com/spf13/pflag v1.0.5 | ||
) | ||
|
||
require github.com/inconshreveable/mousetrap v1.0.0 // indirect | ||
require github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
|
||
replace github.com/spf13/cobra => ../cobra |
Oops, something went wrong.