reduces cyclomatic complexity by actually using data already in scope

This commit is contained in:
Keagan McClelland 2020-09-24 16:44:41 -06:00
parent dba8d53c9f
commit 01c16aeb91
2 changed files with 59 additions and 61 deletions

View File

@ -37,8 +37,7 @@ func wsErr(ws *websocket.Conn, r *http.Request, status int, err error) { //nolin
}
}
func commandsHandler (enableExec bool) handleFunc {
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
var commandsHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return http.StatusInternalServerError, err
@ -60,7 +59,7 @@ func commandsHandler (enableExec bool) handleFunc {
}
}
if !enableExec || !d.user.CanExecute(strings.Split(raw, " ")[0]) {
if !d.server.EnableExec || !d.user.CanExecute(strings.Split(raw, " ")[0]) {
if err := conn.WriteMessage(websocket.TextMessage, cmdNotAllowed); err != nil { //nolint:shadow
wsErr(conn, r, http.StatusInternalServerError, err)
}
@ -108,5 +107,4 @@ func commandsHandler (enableExec bool) handleFunc {
}
return 0, nil
})
}
})

View File

@ -61,7 +61,7 @@ func NewHandler(imgSvc ImgService, fileCache FileCache, store *storage.Storage,
api.PathPrefix("/raw").Handler(monkey(rawHandler, "/api/raw")).Methods("GET")
api.PathPrefix("/preview/{size}/{path:.*}").
Handler(monkey(previewHandler(imgSvc, fileCache, server.EnableThumbnails, server.ResizePreview), "/api/preview")).Methods("GET")
api.PathPrefix("/command").Handler(monkey(commandsHandler(server.EnableExec), "/api/command")).Methods("GET")
api.PathPrefix("/command").Handler(monkey(commandsHandler, "/api/command")).Methods("GET")
api.PathPrefix("/search").Handler(monkey(searchHandler, "/api/search")).Methods("GET")
public := api.PathPrefix("/public").Subrouter()