forked from sneak/secret
Add blank lines before return statements in all files to satisfy the nlreturn linter. This improves code readability by providing visual separation before return statements. Changes made across 24 files: - internal/cli/*.go - internal/secret/*.go - internal/vault/*.go - pkg/agehd/agehd.go - pkg/bip85/bip85.go All 143 nlreturn issues have been resolved.
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package secret
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// generateRandomString generates a random string of the specified length using the given character set
|
|
func generateRandomString(length int, charset string) (string, error) {
|
|
if length <= 0 {
|
|
return "", fmt.Errorf("length must be positive")
|
|
}
|
|
|
|
result := make([]byte, length)
|
|
charsetLen := big.NewInt(int64(len(charset)))
|
|
|
|
for i := range length {
|
|
randomIndex, err := rand.Int(rand.Reader, charsetLen)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to generate random number: %w", err)
|
|
}
|
|
result[i] = charset[randomIndex.Int64()]
|
|
}
|
|
|
|
return string(result), nil
|
|
}
|
|
|
|
// DetermineStateDir determines the state directory based on environment variables and OS
|
|
func DetermineStateDir(customConfigDir string) string {
|
|
// Check for environment variable first
|
|
if envStateDir := os.Getenv(EnvStateDir); envStateDir != "" {
|
|
return envStateDir
|
|
}
|
|
|
|
// Use custom config dir if provided
|
|
if customConfigDir != "" {
|
|
return filepath.Join(customConfigDir, AppID)
|
|
}
|
|
|
|
// Use os.UserConfigDir() which handles platform-specific directories:
|
|
// - On Unix systems, it returns $XDG_CONFIG_HOME or $HOME/.config
|
|
// - On Darwin, it returns $HOME/Library/Application Support
|
|
// - On Windows, it returns %AppData%
|
|
configDir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
// Fallback to a reasonable default if we can't determine user config dir
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
return filepath.Join(homeDir, ".config", AppID)
|
|
}
|
|
|
|
return filepath.Join(configDir, AppID)
|
|
}
|