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

Commit

Permalink
docs: add an example for trace (#86)
Browse files Browse the repository at this point in the history
Resolves #85

Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 authored Jul 19, 2023
1 parent 2afb422 commit 5e38e75
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions trace/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package trace_test

import (
"context"
"fmt"

credentials "github.com/oras-project/oras-credentials-go"
"github.com/oras-project/oras-credentials-go/trace"
"oras.land/oras-go/v2/registry/remote/auth"
)

// An example on how to use ExecutableTrace with Stores.
func Example() {
// ExecutableTrace works with all Stores that may invoke executables, for
// example the Store returned from NewStore and NewNativeStore.
store, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{})
if err != nil {
panic(err)
}

// Define ExecutableTrace and add it to the context. The 'action' argument
// refers to one of 'store', 'get' and 'erase' defined by the docker
// credential helper protocol.
// Reference: https://docs.docker.com/engine/reference/commandline/login/#credential-helper-protocol
traceHooks := &trace.ExecutableTrace{
ExecuteStart: func(executableName string, action string) {
fmt.Printf("executable %s, action %s started", executableName, action)
},
ExecuteDone: func(executableName string, action string, err error) {
fmt.Printf("executable %s, action %s finished", executableName, action)
},
}
ctx := trace.WithExecutableTrace(context.Background(), traceHooks)

// Get, Put and Delete credentials from store. If any credential helper
// executable is run, traceHooks is executed.
err = store.Put(ctx, "localhost:5000", auth.Credential{Username: "testUsername", Password: "testPassword"})
if err != nil {
panic(err)
}

cred, err := store.Get(ctx, "localhost:5000")
if err != nil {
panic(err)
}
fmt.Println(cred)

err = store.Delete(ctx, "localhost:5000")
if err != nil {
panic(err)
}
}

0 comments on commit 5e38e75

Please sign in to comment.