Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
docs: add examples for NewStoreFromDocker and NewStoreWithFallbacks (#55
Browse files Browse the repository at this point in the history
)

Resolves #49

Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 authored Apr 27, 2023
1 parent f738d10 commit a191ac6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
66 changes: 66 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,72 @@ func ExampleNewStore() {
}
}

func ExampleNewStoreFromDocker() {
ds, err := credentials.NewStoreFromDocker(credentials.StoreOptions{
AllowPlaintextPut: true,
})
if err != nil {
panic(err)
}

ctx := context.Background()
// save credentials into the store
err = ds.Put(ctx, "localhost:5000", auth.Credential{
Username: "username-example",
Password: "password-example",
})
if err != nil {
panic(err)
}

// get credentials from the store
cred, err := ds.Get(ctx, "localhost:5000")
if err != nil {
panic(err)
}
fmt.Println(cred)

// delete the credentials from the store
err = ds.Delete(ctx, "localhost:5000")
if err != nil {
panic(err)
}
}

func ExampleNewStoreWithFallbacks_configAsPrimaryStoreDockerAsFallback() {
primaryStore, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{
AllowPlaintextPut: true,
})
if err != nil {
panic(err)
}
fallbackStore, err := credentials.NewStoreFromDocker(credentials.StoreOptions{})
sf := credentials.NewStoreWithFallbacks(primaryStore, fallbackStore)

ctx := context.Background()
// save credentials into the store
err = sf.Put(ctx, "localhost:5000", auth.Credential{
Username: "username-example",
Password: "password-example",
})
if err != nil {
panic(err)
}

// get credentials from the store
cred, err := sf.Get(ctx, "localhost:5000")
if err != nil {
panic(err)
}
fmt.Println(cred)

// delete the credentials from the store
err = sf.Delete(ctx, "localhost:5000")
if err != nil {
panic(err)
}
}

func ExampleLogin() {
store, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{
AllowPlaintextPut: true,
Expand Down
2 changes: 1 addition & 1 deletion store.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NewStore(configPath string, opts StoreOptions) (Store, error) {
// $DOCKER_CONFIG/config.json will be used.
// - Otherwise, the default location $HOME/.docker/config.json will be used.
//
// NewStoreFromDocker internally calls [credentials.NewStore].
// NewStoreFromDocker internally calls [NewStore].
//
// References:
// - https://docs.docker.com/engine/reference/commandline/cli/#configuration-files
Expand Down

0 comments on commit a191ac6

Please sign in to comment.