trace

package
v2.0.0-...-c472316 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Example

An example on how to use ExecutableTrace with Stores.

package main

import (
	"context"
	"fmt"

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

func main() {
	// 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)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithExecutableTrace

func WithExecutableTrace(ctx context.Context, trace *ExecutableTrace) context.Context

WithExecutableTrace takes a Context and an ExecutableTrace, and returns a Context with the ExecutableTrace added as a Value. If the Context has a previously added trace, the hooks defined in the new trace will be added in addition to the previous ones. The recent hooks will be called first.

Types

type ExecutableTrace

type ExecutableTrace struct {
	// ExecuteStart is called before the execution of the executable. The
	// executableName parameter is the name of the credential helper executable
	// used with NativeStore. The action parameter is one of "store", "get" and
	// "erase".
	//
	// Reference:
	//   - https://docs.docker.com/engine/reference/commandline/login#credentials-store
	ExecuteStart func(executableName string, action string)

	// ExecuteDone is called after the execution of an executable completes.
	// The executableName parameter is the name of the credential helper
	// executable used with NativeStore. The action parameter is one of "store",
	// "get" and "erase". The err parameter is the error (if any) returned from
	// the execution.
	//
	// Reference:
	//   - https://docs.docker.com/engine/reference/commandline/login#credentials-store
	ExecuteDone func(executableName string, action string, err error)
}

ExecutableTrace is a set of hooks used to trace the execution of binary executables. Any particular hook may be nil.

func ContextExecutableTrace

func ContextExecutableTrace(ctx context.Context) *ExecutableTrace

ContextExecutableTrace returns the ExecutableTrace associated with the context. If none, it returns nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL