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

Commit

Permalink
fixed errors and refined
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 committed Jul 10, 2023
1 parent ad4a8d9 commit ddc64ab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions internal/executer/executer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func New(name string) Executer {

// Execute operates on an executable binary and supports context.
func (c *executable) Execute(ctx context.Context, input io.Reader, action string) ([]byte, error) {
trace := trace.GetTraceHookFromContext(ctx)
trace := trace.GetTraceHooksFromContext(ctx)
if trace != nil {
trace.ExecuteStart(c.name, action)
}
cmd := exec.CommandContext(ctx, c.name, action)
if trace != nil {
trace.ExecuteStart(c.name, action)
trace.ExecuteDone(c.name, action)
}
cmd.Stdin = input
cmd.Stderr = os.Stderr
Expand Down
10 changes: 5 additions & 5 deletions native_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (e *testExecuter) Execute(ctx context.Context, input io.Reader, action stri
case noCredentialsHost:
return []byte("credentials not found"), errCredentialsNotFound
case traceHost:
traceHook := trace.GetTraceHookFromContext(ctx)
traceHook := trace.GetTraceHooksFromContext(ctx)
if traceHook != nil {
traceHook.ExecuteStart("testExecuter", "get")
traceHook.ExecuteDone("testExecuter", "get")
Expand All @@ -93,7 +93,7 @@ func (e *testExecuter) Execute(ctx context.Context, input io.Reader, action stri
case basicAuthHost, bearerAuthHost, exeErrorHost:
return nil, nil
case traceHost:
traceHook := trace.GetTraceHookFromContext(ctx)
traceHook := trace.GetTraceHooksFromContext(ctx)
if traceHook != nil {
traceHook.ExecuteStart("testExecuter", "store")
traceHook.ExecuteDone("testExecuter", "store")
Expand All @@ -107,7 +107,7 @@ func (e *testExecuter) Execute(ctx context.Context, input io.Reader, action stri
case basicAuthHost, bearerAuthHost:
return nil, nil
case traceHost:
traceHook := trace.GetTraceHookFromContext(ctx)
traceHook := trace.GetTraceHooksFromContext(ctx)
if traceHook != nil {
traceHook.ExecuteStart("testExecuter", "erase")
traceHook.ExecuteDone("testExecuter", "erase")
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestNativeStore_trace(t *testing.T) {
ns := &nativeStore{
&testExecuter{},
}
// create a trace hook that writes to buffer
// create trace hooks that write to buffer
buffer := bytes.Buffer{}
traceHook := &trace.ExecutableTrace{
ExecuteStart: func(executableName string, action string) {
Expand All @@ -225,7 +225,7 @@ func TestNativeStore_trace(t *testing.T) {
buffer.WriteString(fmt.Sprintf("test trace, completed the execution of executable %s with action %s", executableName, action))
},
}
ctx, err := trace.WithTraceHook(context.Background(), traceHook)
ctx, err := trace.WithTraceHooks(context.Background(), traceHook)
if err != nil {
t.Errorf("empty trace: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type ExecutableTrace struct {
ExecuteDone func(executableName string, action string)
}

// GetTraceHookFromContext returns the ExecutableTrace associated with the context.
// GetTraceHooksFromContext returns the ExecutableTrace associated with the context.
// If none, it returns nil.
func GetTraceHookFromContext(ctx context.Context) *ExecutableTrace {
func GetTraceHooksFromContext(ctx context.Context) *ExecutableTrace {
trace, _ := ctx.Value(executableTraceHookKey{}).(*ExecutableTrace)
return trace
}

// WithTraceHook takes a Context and an ExecutableTrace, and returns a Context with
// WithTraceHooks takes a Context and an ExecutableTrace, and returns a Context with
// the ExecutableTrace added as a Value.
func WithTraceHook(ctx context.Context, trace *ExecutableTrace) (context.Context, error) {
func WithTraceHooks(ctx context.Context, trace *ExecutableTrace) (context.Context, error) {
if trace == nil {
return nil, errors.New("empty ExecutableTrace")
return nil, errors.New("nil ExecutableTrace")
}
ctx = context.WithValue(ctx, executableTraceHookKey{}, trace)
return ctx, nil
Expand Down

0 comments on commit ddc64ab

Please sign in to comment.