package main import ( "errors" "fmt" "os" "github.com/spf13/cobra" "git.wxccs.org/iceking2nd/winauth-go/internal/config" ) // resolveConfigPath returns the config file path from the --config // flag or the default location. func resolveConfigPath(cmd *cobra.Command) string { if p, _ := cmd.Root().PersistentFlags().GetString("config"); p != "" { return p } return config.DefaultPath() } // cliLoadConfig loads the config at path, prompting for a password if // the file is encrypted. Returns the config and the passphrase (empty // for plaintext configs). Up to 3 password attempts are allowed. func cliLoadConfig(path string) (*config.Config, []byte, error) { cfg, err := config.LoadYAML(path, nil) if err == nil { return cfg, nil, nil } if !errors.Is(err, config.ErrPasswordRequired) { return nil, nil, err } for attempt := 0; attempt < 3; attempt++ { pw, err := readPassword("Password: ") if err != nil { return nil, nil, fmt.Errorf("read password: %w", err) } cfg, err = config.LoadYAML(path, pw) if err == nil { return cfg, pw, nil } if errors.Is(err, config.ErrPasswordWrong) { fmt.Fprintln(os.Stderr, "Wrong password.") continue } return nil, nil, err } return nil, nil, fmt.Errorf("too many failed password attempts") }