Skip to content

Instantly share code, notes, and snippets.

@dustinmm80
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinmm80/6ab1edcd1d50617c53c8 to your computer and use it in GitHub Desktop.
Save dustinmm80/6ab1edcd1d50617c53c8 to your computer and use it in GitHub Desktop.
Shelling out from Go to retrieve a keychain item's password
package main
/*
./keychain -name mysecret
Siam589_logy
On run you'll get a popup window asking for access.
*/
import (
"flag"
"fmt"
"os/exec"
"strings"
)
func GetSecretValue(name string) (string, error) {
cmd := exec.Command(
"security",
"find-generic-password",
"-l",
name,
"-w",
)
out, err := cmd.CombinedOutput()
strippedOut := strings.TrimSpace(string(out))
if err != nil {
return "", fmt.Errorf("%s\n%s", strippedOut, err)
}
return strippedOut, nil
}
func main() {
secretName := flag.String("name", "", "Name of the secret")
flag.Parse()
secretValue, err := GetSecretValue(*secretName)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Print(secretValue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment