Files
gopass-mirror/pkg/pwgen/external.go
google-labs-jules[bot] 86720090b6 docs: Add GoDoc to pkg and improve markdown files (#3251)
This change adds GoDoc comments to many of the public symbols in the
`pkg/` directory. It also includes various improvements to the
documentation in `README.md` and other markdown files in the `docs/`
directory.

This is a partial documentation effort, as requested by the user, to
get a pull request submitted quickly.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-09-22 19:37:15 +02:00

54 lines
1.1 KiB
Go

package pwgen
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
shellquote "github.com/kballard/go-shellquote"
)
var (
// ErrNoExternal is returned when no external generator is set.
ErrNoExternal = fmt.Errorf("no external generator")
// ErrNoCommand is returned when no command is set.
ErrNoCommand = fmt.Errorf("no command")
)
// GenerateExternal will invoke an external password generator,
// if set, and return its output.
// The external generator is configured via the GOPASS_EXTERNAL_PWGEN environment variable.
func GenerateExternal(pwlen int) (string, error) {
c := os.Getenv("GOPASS_EXTERNAL_PWGEN")
if c == "" {
return "", ErrNoExternal
}
cmdArgs, err := shellquote.Split(c)
if err != nil {
return "", fmt.Errorf("failed to split %s: %w", c, err)
}
if len(cmdArgs) < 1 {
return "", ErrNoCommand
}
exe := cmdArgs[0]
args := []string{}
if len(cmdArgs) > 1 {
args = cmdArgs[1:]
}
args = append(args, strconv.Itoa(pwlen))
out, err := exec.Command(exe, args...).Output()
if err != nil {
return "", fmt.Errorf("failed to execute %s %v: %w", exe, args, err)
}
return strings.TrimSpace(string(out)), nil
}