Skip to content

Instantly share code, notes, and snippets.

@suzuki-shunsuke
Last active October 11, 2024 07:13
Show Gist options
  • Save suzuki-shunsuke/58961369fbe13c76b3052c6ddc0a3893 to your computer and use it in GitHub Desktop.
Save suzuki-shunsuke/58961369fbe13c76b3052c6ddc0a3893 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"github.com/99designs/keyring"
)
func main() {
if err := get(); err != nil {
log.Fatal(err)
}
}
func get() error {
ring, err := keyring.Open(keyring.Config{
ServiceName: "test-aquaproj-aqua",
})
if err != nil {
return fmt.Errorf("open a key: %w", err)
}
item, err := ring.Get("GITHUB_TOKEN")
if err != nil {
return fmt.Errorf("set a key: %w", err)
}
fmt.Println("value:", string(item.Data))
return nil
}
package main
import (
"fmt"
"log"
"github.com/99designs/keyring"
)
func main() {
if err := set(); err != nil {
log.Fatal(err)
}
}
func set() error {
ring, err := keyring.Open(keyring.Config{
ServiceName: "test-aquaproj-aqua",
})
if err != nil {
return fmt.Errorf("open a key: %w", err)
}
if err := ring.Set(keyring.Item{
Key: "GITHUB_TOKEN",
Data: []byte("secret-bar"),
}); err != nil {
return fmt.Errorf("set a key: %w", err)
}
return nil
}
@suzuki-shunsuke
Copy link
Author

$ time ./bin/get   
value: secret-bar
./bin/get  0.01s user 0.01s system 57% cpu 0.023 total

It's enough fast on macOS.

@suzuki-shunsuke
Copy link
Author

It doesn't work without CGO on macOS.

CGO_ENABLED=0 go build -o bin/get-nocgo get.go
$ ./bin/get-nocgo 
2024/10/11 16:12:22 set a key: No directory provided for file keyring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment