Skip to content

Commit

Permalink
hooks: Use built-in function for testing slice elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Jul 3, 2023
1 parent ac45632 commit 124ce13
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cmd/tusd/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/tus/tusd/v2/pkg/hooks"
"golang.org/x/exp/slices"
)

var Flags struct {
Expand Down Expand Up @@ -131,7 +132,7 @@ func SetEnabledHooks() {
for i, h := range slc {
slc[i] = strings.TrimSpace(h)

if !hookTypeInSlice(hooks.HookType(h), hooks.AvailableHooks) {
if !slices.Contains(hooks.AvailableHooks, hooks.HookType(h)) {
stderr.Fatalf("Unknown hook event type in -hooks-enabled-events flag: %s", h)
}

Expand Down
12 changes: 2 additions & 10 deletions cmd/tusd/cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@ import (
"github.com/tus/tusd/v2/pkg/hooks/grpc"
"github.com/tus/tusd/v2/pkg/hooks/http"
"github.com/tus/tusd/v2/pkg/hooks/plugin"
"golang.org/x/exp/slices"
)

// TODO: Move some parts into hooks package

var hookHandler hooks.HookHandler = nil

func hookTypeInSlice(a hooks.HookType, list []hooks.HookType) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

func preCreateCallback(event handler.HookEvent) (handler.HTTPResponse, handler.FileInfoChanges, error) {
ok, hookRes, err := invokeHookSync(hooks.HookPreCreate, event)
if !ok || err != nil {
Expand Down Expand Up @@ -170,7 +162,7 @@ func invokeHookAsync(typ hooks.HookType, event handler.HookEvent) {
// hook completed successfully.
func invokeHookSync(typ hooks.HookType, event handler.HookEvent) (ok bool, res hooks.HookResponse, err error) {
// Stop, if no hook handler is installed or this hook event is not enabled
if hookHandler == nil || !hookTypeInSlice(typ, Flags.EnabledHooks) {
if hookHandler == nil || !slices.Contains(Flags.EnabledHooks, typ) {
return false, hooks.HookResponse{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion examples/hooks/plugin/hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/tus/tusd/v2/cmd/tusd/cli/hooks"
"github.com/tus/tusd/v2/pkg/hooks"
)

// Here is the implementation of our hook handler
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/sethgrid/pester v1.2.0
github.com/stretchr/testify v1.8.4
github.com/vimeo/go-util v1.4.1
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
google.golang.org/api v0.129.0
google.golang.org/grpc v1.56.1
google.golang.org/protobuf v1.31.0
Expand Down
2 changes: 0 additions & 2 deletions pkg/handler/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package handler

import "net/http"

// TODO: Move some parts to hooks package

// HookEvent represents an event from tusd which can be handled by the application.
type HookEvent struct {
// Upload contains information about the upload that caused this hook
Expand Down

0 comments on commit 124ce13

Please sign in to comment.