oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/registry/remote/credentials/trace/example_test.go (about) 1 /* 2 Copyright The ORAS Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package trace_test 15 16 import ( 17 "context" 18 "fmt" 19 20 "oras.land/oras-go/v2/registry/remote/auth" 21 "oras.land/oras-go/v2/registry/remote/credentials" 22 "oras.land/oras-go/v2/registry/remote/credentials/trace" 23 ) 24 25 // An example on how to use ExecutableTrace with Stores. 26 func Example() { 27 // ExecutableTrace works with all Stores that may invoke executables, for 28 // example the Store returned from NewStore and NewNativeStore. 29 store, err := credentials.NewStore("example/path/config.json", credentials.StoreOptions{}) 30 if err != nil { 31 panic(err) 32 } 33 34 // Define ExecutableTrace and add it to the context. The 'action' argument 35 // refers to one of 'store', 'get' and 'erase' defined by the docker 36 // credential helper protocol. 37 // Reference: https://docs.docker.com/engine/reference/commandline/login/#credential-helper-protocol 38 traceHooks := &trace.ExecutableTrace{ 39 ExecuteStart: func(executableName string, action string) { 40 fmt.Printf("executable %s, action %s started", executableName, action) 41 }, 42 ExecuteDone: func(executableName string, action string, err error) { 43 fmt.Printf("executable %s, action %s finished", executableName, action) 44 }, 45 } 46 ctx := trace.WithExecutableTrace(context.Background(), traceHooks) 47 48 // Get, Put and Delete credentials from store. If any credential helper 49 // executable is run, traceHooks is executed. 50 err = store.Put(ctx, "localhost:5000", auth.Credential{Username: "testUsername", Password: "testPassword"}) 51 if err != nil { 52 panic(err) 53 } 54 55 cred, err := store.Get(ctx, "localhost:5000") 56 if err != nil { 57 panic(err) 58 } 59 fmt.Println(cred) 60 61 err = store.Delete(ctx, "localhost:5000") 62 if err != nil { 63 panic(err) 64 } 65 }