Fix noinlineerr findings: internal/cli (refs #61)
This commit is contained in:
@@ -236,13 +236,14 @@ on macOS, ~/.config/ on Linux, /etc/vaultik/ as root).`,
|
|||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
path := configPathForInit()
|
path := configPathForInit()
|
||||||
|
|
||||||
if _, err := os.Stat(path); err == nil {
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
return fmt.Errorf("config file already exists: %s", path)
|
return fmt.Errorf("config file already exists: %s", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := filepath.Dir(path)
|
dir := filepath.Dir(path)
|
||||||
|
|
||||||
err := os.MkdirAll(dir, 0o755)
|
err = os.MkdirAll(dir, 0o755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("creating config directory %s: %w", dir, err)
|
return fmt.Errorf("creating config directory %s: %w", dir, err)
|
||||||
}
|
}
|
||||||
@@ -353,7 +354,8 @@ Examples:
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yamlPathSet(root, strings.Split(args[0], "."), args[1]); err != nil {
|
err = yamlPathSet(root, strings.Split(args[0], "."), args[1])
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,11 +365,14 @@ Examples:
|
|||||||
}
|
}
|
||||||
|
|
||||||
mode := os.FileMode(0o600)
|
mode := os.FileMode(0o600)
|
||||||
if info, err := os.Stat(path); err == nil {
|
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
mode = info.Mode().Perm()
|
mode = info.Mode().Perm()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(path, out, mode); err != nil {
|
err = os.WriteFile(path, out, mode)
|
||||||
|
if err != nil {
|
||||||
return fmt.Errorf("writing config file: %w", err)
|
return fmt.Errorf("writing config file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,7 +392,9 @@ func loadYAMLFile(path string) (*yaml.Node, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var root yaml.Node
|
var root yaml.Node
|
||||||
if err := yaml.Unmarshal(data, &root); err != nil {
|
|
||||||
|
err = yaml.Unmarshal(data, &root)
|
||||||
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parsing config file: %w", err)
|
return nil, fmt.Errorf("parsing config file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -120,29 +120,35 @@ func TestYAMLPathSet(t *testing.T) {
|
|||||||
root := parseTestYAML(t)
|
root := parseTestYAML(t)
|
||||||
|
|
||||||
// Overwrite existing nested value
|
// Overwrite existing nested value
|
||||||
if err := yamlPathSet(root, splitPath("s3.bucket"), "newbucket"); err != nil {
|
err := yamlPathSet(root, splitPath("s3.bucket"), "newbucket")
|
||||||
|
if err != nil {
|
||||||
t.Fatalf("set s3.bucket: %v", err)
|
t.Fatalf("set s3.bucket: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new nested key with intermediate map
|
// Create new nested key with intermediate map
|
||||||
if err := yamlPathSet(root, splitPath("s3.endpoint"), "s3.example.com"); err != nil {
|
err = yamlPathSet(root, splitPath("s3.endpoint"), "s3.example.com")
|
||||||
|
if err != nil {
|
||||||
t.Fatalf("set s3.endpoint: %v", err)
|
t.Fatalf("set s3.endpoint: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yamlPathSet(root, splitPath("newmap.newkey"), "val"); err != nil {
|
err = yamlPathSet(root, splitPath("newmap.newkey"), "val")
|
||||||
|
if err != nil {
|
||||||
t.Fatalf("set newmap.newkey: %v", err)
|
t.Fatalf("set newmap.newkey: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overwrite a sequence element and append a new one
|
// Overwrite a sequence element and append a new one
|
||||||
if err := yamlPathSet(root, splitPath("age_recipients.0"), "age1bbb"); err != nil {
|
err = yamlPathSet(root, splitPath("age_recipients.0"), "age1bbb")
|
||||||
|
if err != nil {
|
||||||
t.Fatalf("set age_recipients.0: %v", err)
|
t.Fatalf("set age_recipients.0: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yamlPathSet(root, splitPath("age_recipients.1"), "age1ccc"); err != nil {
|
err = yamlPathSet(root, splitPath("age_recipients.1"), "age1ccc")
|
||||||
|
if err != nil {
|
||||||
t.Fatalf("append age_recipients.1: %v", err)
|
t.Fatalf("append age_recipients.1: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yamlPathSet(root, splitPath("age_recipients.5"), "age1ddd"); err == nil {
|
err = yamlPathSet(root, splitPath("age_recipients.5"), "age1ddd")
|
||||||
|
if err == nil {
|
||||||
t.Error("expected out-of-range append to fail")
|
t.Error("expected out-of-range append to fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,7 +64,8 @@ Use --force to skip the confirmation prompt.`,
|
|||||||
dbPath := cfg.IndexPath
|
dbPath := cfg.IndexPath
|
||||||
|
|
||||||
// Check if database exists
|
// Check if database exists
|
||||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
_, err = os.Stat(dbPath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
fmt.Printf("Database does not exist: %s\n", dbPath)
|
fmt.Printf("Database does not exist: %s\n", dbPath)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -76,7 +77,9 @@ Use --force to skip the confirmation prompt.`,
|
|||||||
fmt.Print("Are you sure? Type 'yes' to confirm: ")
|
fmt.Print("Are you sure? Type 'yes' to confirm: ")
|
||||||
|
|
||||||
var confirm string
|
var confirm string
|
||||||
if _, err := fmt.Scanln(&confirm); err != nil || confirm != "yes" {
|
|
||||||
|
_, err = fmt.Scanln(&confirm)
|
||||||
|
if err != nil || confirm != "yes" {
|
||||||
fmt.Println("Aborted.")
|
fmt.Println("Aborted.")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -84,7 +87,8 @@ Use --force to skip the confirmation prompt.`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete the database file
|
// Delete the database file
|
||||||
if err := os.Remove(dbPath); err != nil {
|
err = os.Remove(dbPath)
|
||||||
|
if err != nil {
|
||||||
return fmt.Errorf("failed to delete database: %w", err)
|
return fmt.Errorf("failed to delete database: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ import (
|
|||||||
// Can combine units: "1y6mo", "2w3d", "1d12h30m"
|
// Can combine units: "1y6mo", "2w3d", "1d12h30m"
|
||||||
func parseDuration(s string) (time.Duration, error) {
|
func parseDuration(s string) (time.Duration, error) {
|
||||||
// First try standard Go duration parsing
|
// First try standard Go duration parsing
|
||||||
if d, err := time.ParseDuration(s); err == nil {
|
d, err := time.ParseDuration(s)
|
||||||
|
if err == nil {
|
||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,10 +79,14 @@ func parseDuration(s string) (time.Duration, error) {
|
|||||||
default:
|
default:
|
||||||
// Try parsing as standard Go duration unit
|
// Try parsing as standard Go duration unit
|
||||||
testStr := "1" + unit
|
testStr := "1" + unit
|
||||||
if _, err := time.ParseDuration(testStr); err == nil {
|
|
||||||
|
_, err = time.ParseDuration(testStr)
|
||||||
|
if err == nil {
|
||||||
// It's a valid Go duration unit, parse the full value
|
// It's a valid Go duration unit, parse the full value
|
||||||
fullStr := fmt.Sprintf("%g%s", value, unit)
|
fullStr := fmt.Sprintf("%g%s", value, unit)
|
||||||
if d, err = time.ParseDuration(fullStr); err != nil {
|
|
||||||
|
d, err = time.ParseDuration(fullStr)
|
||||||
|
if err != nil {
|
||||||
return 0, fmt.Errorf("invalid duration %q: %w", fullStr, err)
|
return 0, fmt.Errorf("invalid duration %q: %w", fullStr, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -75,7 +75,8 @@ func GetRootFlags() RootFlags {
|
|||||||
// so the user gets a clear error instead of a downstream YAML parser failure.
|
// so the user gets a clear error instead of a downstream YAML parser failure.
|
||||||
func ResolveConfigPath() (string, error) {
|
func ResolveConfigPath() (string, error) {
|
||||||
if path := rootFlags.ConfigPath; path != "" {
|
if path := rootFlags.ConfigPath; path != "" {
|
||||||
if _, err := os.Stat(path); err != nil {
|
_, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
return "", fmt.Errorf("config file from --config not found: %s (run 'vaultik config init --config %s' to create it)", path, path)
|
return "", fmt.Errorf("config file from --config not found: %s (run 'vaultik config init --config %s' to create it)", path, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +92,8 @@ func ResolveConfigPath() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range defaultConfigPaths() {
|
for _, path := range defaultConfigPaths() {
|
||||||
if _, err := os.Stat(path); err == nil {
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -418,7 +418,8 @@ nuke --force' — it is the single supported entry point for that.`,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := v.Shutdowner.Shutdown(); err != nil {
|
err = v.Shutdowner.Shutdown()
|
||||||
|
if err != nil {
|
||||||
log.Error("Failed to shutdown", "error", err)
|
log.Error("Failed to shutdown", "error", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user