-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Add option for run hooks from Go plugin
Squashed commit of the following: commit 1b80f51f94cf860ba8516baed4b65e9ded6441fe Author: Marius <maerious@gmail.com> Date: Mon Jun 10 11:41:30 2019 +0200 Minor improvements commit 98daad5 Author: Marius <maerious@gmail.com> Date: Fri Jun 7 13:26:14 2019 +0200 Extract File and Http hooks into own structs
- Loading branch information
Showing
9 changed files
with
300 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package hooks | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
|
||
"github.com/tus/tusd" | ||
) | ||
|
||
type FileHook struct { | ||
Directory string | ||
} | ||
|
||
func (_ FileHook) Setup() error { | ||
return nil | ||
} | ||
|
||
func (h FileHook) InvokeHook(typ HookType, info tusd.FileInfo, captureOutput bool) ([]byte, int, error) { | ||
hookPath := h.Directory + string(os.PathSeparator) + string(typ) | ||
cmd := exec.Command(hookPath) | ||
env := os.Environ() | ||
env = append(env, "TUS_ID="+info.ID) | ||
env = append(env, "TUS_SIZE="+strconv.FormatInt(info.Size, 10)) | ||
env = append(env, "TUS_OFFSET="+strconv.FormatInt(info.Offset, 10)) | ||
|
||
jsonInfo, err := json.Marshal(info) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
reader := bytes.NewReader(jsonInfo) | ||
cmd.Stdin = reader | ||
|
||
cmd.Env = env | ||
cmd.Dir = h.Directory | ||
cmd.Stderr = os.Stderr | ||
|
||
// If `captureOutput` is true, this function will return the output (both, | ||
// stderr and stdout), else it will use this process' stdout | ||
var output []byte | ||
if !captureOutput { | ||
cmd.Stdout = os.Stdout | ||
err = cmd.Run() | ||
} else { | ||
output, err = cmd.Output() | ||
} | ||
|
||
// Ignore the error, only, if the hook's file could not be found. This usually | ||
// means that the user is only using a subset of the available hooks. | ||
if os.IsNotExist(err) { | ||
err = nil | ||
} | ||
|
||
returnCode := cmd.ProcessState.ExitCode() | ||
|
||
return output, returnCode, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package hooks | ||
|
||
import ( | ||
"github.com/tus/tusd" | ||
) | ||
|
||
type HookHandler interface { | ||
Setup() error | ||
InvokeHook(typ HookType, info tusd.FileInfo, captureOutput bool) ([]byte, int, error) | ||
} | ||
|
||
type HookType string | ||
|
||
const ( | ||
HookPostFinish HookType = "post-finish" | ||
HookPostTerminate HookType = "post-terminate" | ||
HookPostReceive HookType = "post-receive" | ||
HookPostCreate HookType = "post-create" | ||
HookPreCreate HookType = "pre-create" | ||
) | ||
|
||
type hookDataStore struct { | ||
tusd.DataStore | ||
} | ||
|
||
type HookError struct { | ||
error | ||
statusCode int | ||
body []byte | ||
} | ||
|
||
func NewHookError(err error, statusCode int, body []byte) HookError { | ||
return HookError{err, statusCode, body} | ||
} | ||
|
||
func (herr HookError) StatusCode() int { | ||
return herr.statusCode | ||
} | ||
|
||
func (herr HookError) Body() []byte { | ||
return herr.body | ||
} | ||
|
||
func (herr HookError) Error() string { | ||
return herr.error.Error() | ||
} |
Oops, something went wrong.