-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhook_executor_default.go
More file actions
111 lines (99 loc) · 3.42 KB
/
hook_executor_default.go
File metadata and controls
111 lines (99 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2022-2025 Salesforce, Inc.
//
// 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 hooks
import (
"bytes"
"context"
"os/exec"
"strings"
"syscall"
"github.com/slackapi/slack-cli/internal/iostreams"
"github.com/slackapi/slack-cli/internal/slackerror"
)
// HookExecutorDefaultProtocol uses the original protocol between the CLI and the SDK where diagnostic info
// and hook responses come in via stdout. Data outside the expected JSON payload is ignored, with the
// exception of the 'start' hook, for which it is printed.
type HookExecutorDefaultProtocol struct {
IO iostreams.IOStreamer
}
// Execute processes the data received by the SDK.
func (e *HookExecutorDefaultProtocol) Execute(ctx context.Context, opts HookExecOpts) (string, error) {
cmdArgs, cmdArgVars, cmdEnvVars, err := processExecOpts(opts)
if err != nil {
return "", err
}
if opts.Exec == nil {
opts.Exec = ShellExec{}
}
e.IO.PrintDebug(ctx,
"starting hook command: %s %s\n", cmdArgs[0], strings.Join(cmdArgVars, " "),
)
defer func() {
e.IO.PrintDebug(ctx,
"finished hook command: %s %s\n", cmdArgs[0], strings.Join(cmdArgVars, " "),
)
}()
buffout := bytes.Buffer{}
bufferr := bytes.Buffer{}
stdout := iostreams.BufferedWriter{
Buff: &buffout,
Stream: iostreams.BufferedWriter{
Buff: opts.Stdout,
Stream: e.IO.WriteDebug(ctx),
},
}
stderr := iostreams.BufferedWriter{
Buff: &bufferr,
Stream: iostreams.BufferedWriter{
Buff: opts.Stderr,
Stream: e.IO.WriteDebug(ctx),
},
}
cmd := opts.Exec.Command(cmdEnvVars, stdout, stderr, opts.Stdin, cmdArgs[0], cmdArgVars...)
err = cmd.Run()
response := strings.TrimSpace(buffout.String())
if err != nil {
// Signal interrupts with an error code when input might be expected
if opts.Stdin != nil {
if ee, ok := err.(*exec.ExitError); ok {
if ws, ok := ee.Sys().(syscall.WaitStatus); ok {
if ws.Signaled() {
switch ws.Signal() {
case syscall.SIGINT, syscall.SIGTERM:
return "", slackerror.New(slackerror.ErrProcessInterrupted)
}
}
}
}
}
// Include stderr outputs in error details if these aren't streamed
details := slackerror.ErrorDetails{}
if opts.Stderr == nil {
details = append(details, slackerror.ErrorDetail{Message: strings.TrimSpace(bufferr.String())})
}
return "", slackerror.New(slackerror.ErrSDKHookInvocationFailed).
WithMessage("Error running '%s' command: %s", opts.Hook.Name, err).
WithDetails(details)
}
// Special handling for the baseline protocol for the `start` hook
if opts.Hook.Name == "Start" {
// All output except for the last line can be displayed to the user.
// The last line should contain stringified JSON of the result object sent as a response.
lines := strings.Split(string(response), "\n")
response = lines[len(lines)-1]
excludesLastLine := lines[0 : len(lines)-1]
_, _ = e.IO.WriteOut().Write([]byte(strings.Join(excludesLastLine, "\n") + "\n"))
}
return response, nil
}