Fix Codacy issues

This commit is contained in:
Anders Roos 2022-11-15 13:34:40 +01:00
parent 5645cd23fe
commit 741f315f14
19 changed files with 163 additions and 135 deletions

View file

@ -23,6 +23,7 @@ func New(token string) *API {
}
}
// EnableCors is a middleware that enables CORS for the API
func (api *API) EnableCors(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")

View file

@ -10,20 +10,18 @@ import (
"github.com/containrrr/watchtower/pkg/types"
)
// Handler is an HTTP handle for serving list data
// Handler is an HTTP handle for serving check data
type Handler struct {
Path string
Client container.Client
}
// CheckRequest defines the type for the request data of the Check endpoint
type CheckRequest struct {
ContainerId string
type checkRequest struct {
ContainerID string
}
// CheckResponse defines the type for the response data of the Check endpoint
type CheckResponse struct {
ContainerId string
type checkResponse struct {
ContainerID string
HasUpdate bool
NewVersion string
NewVersionCreated string
@ -47,7 +45,7 @@ func (handle *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
log.Info("Check for update triggered by HTTP API request.")
var request CheckRequest
var request checkRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
log.Error(err)
@ -57,7 +55,7 @@ func (handle *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
}
client := handle.Client
container, err := client.GetContainer(types.ContainerID(request.ContainerId))
container, err := client.GetContainer(types.ContainerID(request.ContainerID))
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
@ -74,8 +72,8 @@ func (handle *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
return
}
data := CheckResponse{
ContainerId: request.ContainerId,
data := checkResponse{
ContainerID: request.ContainerID,
HasUpdate: stale,
NewVersion: newestImage.ShortID(),
NewVersionCreated: created,

View file

@ -17,9 +17,8 @@ type Handler struct {
Client container.Client
}
// ContainerListEntry defines the type of each container in the response
type ContainerListEntry struct {
ContainerId string
type containerListEntry struct {
ContainerID string
ContainerName string
ImageName string
ImageNameShort string
@ -27,9 +26,8 @@ type ContainerListEntry struct {
ImageCreatedDate string
}
// ListResponse defines the return type of the List endpoint
type ListResponse struct {
Containers []ContainerListEntry
type listResponse struct {
Containers []containerListEntry
}
// New is a factory function creating a new List instance
@ -59,11 +57,11 @@ func (handle *Handler) HandleGet(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(err.Error()))
}
data := ListResponse{Containers: []ContainerListEntry{}}
data := listResponse{Containers: []containerListEntry{}}
for _, c := range containers {
data.Containers = append(data.Containers, ContainerListEntry{
ContainerId: c.ID().ShortID(),
data.Containers = append(data.Containers, containerListEntry{
ContainerID: c.ID().ShortID(),
ContainerName: c.Name()[1:],
ImageName: c.ImageName(),
ImageNameShort: strings.Split(c.ImageName(), ":")[0],