fix: resolve errcheck, gosec, and mnd linter errors
- Fixed unhandled errors in init.go (os.Setenv/Unsetenv) - Fixed unhandled errors in test_helpers.go (os.Setenv/Unsetenv) - Replaced magic numbers with named constants: - defaultSecretLength = 16 - mnemonicEntropyBits = 128 - tabWriterPadding = 2
This commit is contained in:
parent
6e01ae6002
commit
6fe49344e2
@ -11,6 +11,11 @@ import (
|
|||||||
"github.com/tyler-smith/go-bip39"
|
"github.com/tyler-smith/go-bip39"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultSecretLength = 16
|
||||||
|
mnemonicEntropyBits = 128
|
||||||
|
)
|
||||||
|
|
||||||
func newGenerateCmd() *cobra.Command {
|
func newGenerateCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "generate",
|
Use: "generate",
|
||||||
@ -55,7 +60,7 @@ func newGenerateSecretCmd() *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().IntP("length", "l", 16, "Length of the generated secret (default 16)")
|
cmd.Flags().IntP("length", "l", defaultSecretLength, "Length of the generated secret (default 16)")
|
||||||
cmd.Flags().StringP("type", "t", "base58", "Type of secret to generate (base58, alnum)")
|
cmd.Flags().StringP("type", "t", "base58", "Type of secret to generate (base58, alnum)")
|
||||||
cmd.Flags().BoolP("force", "f", false, "Overwrite existing secret")
|
cmd.Flags().BoolP("force", "f", false, "Overwrite existing secret")
|
||||||
|
|
||||||
@ -65,7 +70,7 @@ func newGenerateSecretCmd() *cobra.Command {
|
|||||||
// GenerateMnemonic generates a random BIP39 mnemonic phrase
|
// GenerateMnemonic generates a random BIP39 mnemonic phrase
|
||||||
func (cli *Instance) GenerateMnemonic(cmd *cobra.Command) error {
|
func (cli *Instance) GenerateMnemonic(cmd *cobra.Command) error {
|
||||||
// Generate 128 bits of entropy for a 12-word mnemonic
|
// Generate 128 bits of entropy for a 12-word mnemonic
|
||||||
entropy, err := bip39.NewEntropy(128)
|
entropy, err := bip39.NewEntropy(mnemonicEntropyBits)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to generate entropy: %w", err)
|
return fmt.Errorf("failed to generate entropy: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -80,12 +80,12 @@ func (cli *Instance) Init(cmd *cobra.Command) error {
|
|||||||
|
|
||||||
// Set mnemonic in environment for CreateVault to use
|
// Set mnemonic in environment for CreateVault to use
|
||||||
originalMnemonic := os.Getenv(secret.EnvMnemonic)
|
originalMnemonic := os.Getenv(secret.EnvMnemonic)
|
||||||
os.Setenv(secret.EnvMnemonic, mnemonicStr)
|
_ = os.Setenv(secret.EnvMnemonic, mnemonicStr)
|
||||||
defer func() {
|
defer func() {
|
||||||
if originalMnemonic != "" {
|
if originalMnemonic != "" {
|
||||||
os.Setenv(secret.EnvMnemonic, originalMnemonic)
|
_ = os.Setenv(secret.EnvMnemonic, originalMnemonic)
|
||||||
} else {
|
} else {
|
||||||
os.Unsetenv(secret.EnvMnemonic)
|
_ = os.Unsetenv(secret.EnvMnemonic)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ func ExecuteCommandInProcess(args []string, stdin string, env map[string]string)
|
|||||||
|
|
||||||
// Set test environment
|
// Set test environment
|
||||||
for k, v := range env {
|
for k, v := range env {
|
||||||
os.Setenv(k, v)
|
_ = os.Setenv(k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create root command
|
// Create root command
|
||||||
@ -53,9 +53,9 @@ func ExecuteCommandInProcess(args []string, stdin string, env map[string]string)
|
|||||||
// Restore environment
|
// Restore environment
|
||||||
for k, v := range savedEnv {
|
for k, v := range savedEnv {
|
||||||
if v == "" {
|
if v == "" {
|
||||||
os.Unsetenv(k)
|
_ = os.Unsetenv(k)
|
||||||
} else {
|
} else {
|
||||||
os.Setenv(k, v)
|
_ = os.Setenv(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,10 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
tabWriterPadding = 2
|
||||||
|
)
|
||||||
|
|
||||||
// newVersionCmd returns the version management command
|
// newVersionCmd returns the version management command
|
||||||
func newVersionCmd() *cobra.Command {
|
func newVersionCmd() *cobra.Command {
|
||||||
cli := NewCLIInstance()
|
cli := NewCLIInstance()
|
||||||
@ -110,8 +114,8 @@ func (cli *Instance) ListVersions(cmd *cobra.Command, secretName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create table writer
|
// Create table writer
|
||||||
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
|
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, tabWriterPadding, ' ', 0)
|
||||||
fmt.Fprintln(w, "VERSION\tCREATED\tSTATUS\tNOT_BEFORE\tNOT_AFTER")
|
_, _ = fmt.Fprintln(w, "VERSION\tCREATED\tSTATUS\tNOT_BEFORE\tNOT_AFTER")
|
||||||
|
|
||||||
// Load and display each version's metadata
|
// Load and display each version's metadata
|
||||||
for _, version := range versions {
|
for _, version := range versions {
|
||||||
@ -125,7 +129,7 @@ func (cli *Instance) ListVersions(cmd *cobra.Command, secretName string) error {
|
|||||||
if version == currentVersion {
|
if version == currentVersion {
|
||||||
status = "current (error)"
|
status = "current (error)"
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", version, "-", status, "-", "-")
|
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", version, "-", status, "-", "-")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -152,10 +156,10 @@ func (cli *Instance) ListVersions(cmd *cobra.Command, secretName string) error {
|
|||||||
notAfter = sv.Metadata.NotAfter.Format("2006-01-02 15:04:05")
|
notAfter = sv.Metadata.NotAfter.Format("2006-01-02 15:04:05")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", version, createdAt, status, notBefore, notAfter)
|
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", version, createdAt, status, notBefore, notAfter)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Flush()
|
_ = w.Flush()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user