Skip to content

Commit

Permalink
chore: solved go linting issues
Browse files Browse the repository at this point in the history
lint issues
  • Loading branch information
yp969803 committed Sep 25, 2024
1 parent 558eb8f commit e58a2db
Show file tree
Hide file tree
Showing 23 changed files with 92 additions and 96 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ linters-settings:
sections:
- standard
- default
- prefix(github.com/prometheus-operator/prometheus-operator)
depguard:
rules:
forbid-pkg-errors:
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {

api := router.Group("/api/protected")
{
api.Use(auth.AuthMiddleWare())
api.Use(auth.MiddleWare())
routes.UserRoutes(api)
routes.ContainerRoutes(api)
routes.ImageRoutes(api)
Expand Down
2 changes: 1 addition & 1 deletion conf/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package conf

import "github.com/wharf/wharf/pkg/cache"

var Cache *cache.LRUCache = nil
var Cache *cache.LRUCache

func InitCache() {
Cache = cache.New(10)
Expand Down
2 changes: 0 additions & 2 deletions conf/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ func InitDir() {
err := os.MkdirAll(DirPath, 0755)
if err != nil {
log.Fatalf("Error creating configuration directory: %s", err)
panic(err)
}
log.Printf("Config directory created: %s", DirPath)
} else if err != nil {
log.Fatalf("Error checking config directory: %s", err)
panic(err)
} else {
log.Printf("Directory already exists: %s", DirPath)
}
Expand Down
22 changes: 11 additions & 11 deletions internal/controllers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func GetContainers() gin.HandlerFunc {
errCh := make(chan *errors.Error)
containers := []*types.Container{}
defer cancel()
go dockerContainer.List(conf.DockerClient, ctx, ch, errCh)
go dockerContainer.List(ctx, conf.DockerClient, ch, errCh)
for err := range errCh {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Err})
Expand Down Expand Up @@ -56,7 +56,7 @@ func StopContainer() gin.HandlerFunc {
}
errCh := make(chan *errors.Error)

go dockerContainer.Stop(conf.DockerClient, ctx, id, errCh)
go dockerContainer.Stop(ctx, conf.DockerClient, id, errCh)
for err := range errCh {
if err != nil {
log.Println(err)
Expand All @@ -80,7 +80,7 @@ func UnpauseContainer() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid permissions"})
return
}
err := dockerContainer.Unpause(conf.DockerClient, ctx, id)
err := dockerContainer.Unpause(ctx, conf.DockerClient, id)
if err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -131,7 +131,7 @@ func RemoveContainer() gin.HandlerFunc {
if requestBody.RemoveVolumes != nil {
opts.RemoveVolumes = *requestBody.RemoveVolumes
}
err := dockerContainer.Remove(conf.DockerClient, ctx, id, opts)
err := dockerContainer.Remove(ctx, conf.DockerClient, id, opts)

if err != nil {
if errdefs.IsNotFound(err) {
Expand All @@ -157,7 +157,7 @@ func PruneContainers() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid permissions"})
return
}
report, err := dockerContainer.Prune(conf.DockerClient, ctx)
report, err := dockerContainer.Prune(ctx, conf.DockerClient)
if err != nil {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
Expand All @@ -173,7 +173,7 @@ func ContainerStats() gin.HandlerFunc {
var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second)
id := c.Param("id")
defer cancel()
body, err := dockerContainer.Stats(conf.DockerClient, ctx, id)
body, err := dockerContainer.Stats(ctx, conf.DockerClient, id)
if err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -203,7 +203,7 @@ func ContainerLogs() gin.HandlerFunc {
return
}

body, err := dockerContainer.Logs(conf.DockerClient, ctx, id, days)
body, err := dockerContainer.Logs(ctx, conf.DockerClient, id, days)
if err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -242,7 +242,7 @@ func ContainerRename() gin.HandlerFunc {
return
}

err := dockerContainer.Rename(conf.DockerClient, ctx, id, requestBody.NewName)
err := dockerContainer.Rename(ctx, conf.DockerClient, id, requestBody.NewName)

if err != nil {
if errdefs.IsNotFound(err) {
Expand Down Expand Up @@ -340,7 +340,7 @@ func ContainerCreate() gin.HandlerFunc {
hostConfig.PortBindings = pmap
}

res, err := dockerContainer.Create(conf.DockerClient, ctx, &config, &hostConfig, requestBody.Name)
res, err := dockerContainer.Create(ctx, conf.DockerClient, &config, &hostConfig, requestBody.Name)
if err != nil {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
Expand All @@ -363,7 +363,7 @@ func ContainerPause() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid permissions"})
return
}
err := dockerContainer.Pause(conf.DockerClient, ctx, id)
err := dockerContainer.Pause(ctx, conf.DockerClient, id)
if err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand All @@ -389,7 +389,7 @@ func ContainerStart() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid permissions"})
return
}
err := dockerContainer.Start(conf.DockerClient, ctx, id)
err := dockerContainer.Start(ctx, conf.DockerClient, id)
if err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand Down
8 changes: 4 additions & 4 deletions internal/controllers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetImages() gin.HandlerFunc {
errCh := make(chan *errors.Error)
images := []*image.Summary{}
defer cancel()
go dockerImage.GetAll(conf.DockerClient, ctx, ch, errCh)
go dockerImage.GetAll(ctx, conf.DockerClient, ch, errCh)
for err := range errCh {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Err})
Expand All @@ -47,7 +47,7 @@ func PruneImages() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid premissions"})
return
}
report, err := dockerImage.Prune(conf.DockerClient, ctx)
report, err := dockerImage.Prune(ctx, conf.DockerClient)
if err != nil {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -88,7 +88,7 @@ func RemoveImage() gin.HandlerFunc {
if requestBody.PruneChildren != nil {
opts.PruneChildren = *requestBody.PruneChildren
}
report, err := dockerImage.Remove(conf.DockerClient, ctx, id, opts)
report, err := dockerImage.Remove(ctx, conf.DockerClient, id, opts)
if err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -127,7 +127,7 @@ func TagImage() gin.HandlerFunc {
return
}

err := dockerImage.Tag(conf.DockerClient, ctx, id, reqBody.Tag)
err := dockerImage.Tag(ctx, conf.DockerClient, id, reqBody.Tag)

if err != nil {
if errdefs.IsNotFound(err) {
Expand Down
12 changes: 6 additions & 6 deletions internal/controllers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetNetworks() gin.HandlerFunc {
errCh := make(chan *errors.Error)
networks := []*types.NetworkResource{}
defer cancel()
go dockerNetwork.GetAll(conf.DockerClient, ctx, ch, errCh)
go dockerNetwork.GetAll(ctx, conf.DockerClient, ch, errCh)
for err := range errCh {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Err})
Expand All @@ -47,7 +47,7 @@ func PruneNetwork() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid permissions"})
return
}
report, err := dockerNetwork.Prune(conf.DockerClient, ctx)
report, err := dockerNetwork.Prune(ctx, conf.DockerClient)
if err != nil {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
Expand All @@ -68,7 +68,7 @@ func RemoveNetwork() gin.HandlerFunc {
return
}
id := c.Param("id")
err := dockerNetwork.Remove(conf.DockerClient, ctx, id)
err := dockerNetwork.Remove(ctx, conf.DockerClient, id)
if err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -113,7 +113,7 @@ func DisconnectNetwork() gin.HandlerFunc {
forceDisconnect = *reqBody.Force
}

err := dockerNetwork.Disconnect(conf.DockerClient, ctx, id, reqBody.ContainerID, forceDisconnect)
err := dockerNetwork.Disconnect(ctx, conf.DockerClient, id, reqBody.ContainerID, forceDisconnect)
if err != nil {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -153,7 +153,7 @@ func ConnectNetwork() gin.HandlerFunc {
return
}

err := dockerNetwork.Connect(conf.DockerClient, ctx, id, reqBody.ContainerID)
err := dockerNetwork.Connect(ctx, conf.DockerClient, id, reqBody.ContainerID)
if err != nil {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -198,7 +198,7 @@ func CreateNetwork() gin.HandlerFunc {
CheckDuplicate: true,
}

res, err := dockerNetwork.Create(conf.DockerClient, ctx, reqBody.Name, options)
res, err := dockerNetwork.Create(ctx, conf.DockerClient, reqBody.Name, options)
if err != nil {
log.Println(err)
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
Expand Down
6 changes: 3 additions & 3 deletions internal/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func UpdateUser() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Request not sent by admin"})
return
}
isUser, err := store.GetUserById(id)
isUser, err := store.GetUserByID(id)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -176,7 +176,7 @@ func DeleteUser() gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": "Request not sent by admin"})
return
}
isUser, err := store.GetUserById(id)
isUser, err := store.GetUserByID(id)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
Expand Down Expand Up @@ -233,7 +233,7 @@ func GetUser() gin.HandlerFunc {
Username: *reqUser.Username,
Permission: reqUser.Permission,
IsAdmin: reqUser.IsAdmin,
Id: reqUser.ID,
ID: reqUser.ID,
}
c.JSON(http.StatusOK, res)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/controllers/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetVolumes() gin.HandlerFunc {
errCh := make(chan *errors.Error)
volumes := []*volume.Volume{}
defer cancel()
go dockerVolume.GetAll(conf.DockerClient, ctx, ch, errCh)
go dockerVolume.GetAll(ctx, conf.DockerClient, ch, errCh)
for err := range errCh {
log.Println(err.Err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Err})
Expand Down Expand Up @@ -59,7 +59,7 @@ func RemoveVolume() gin.HandlerFunc {
if reqBody.Force != nil {
forceRem = *reqBody.Force
}
if err := dockerVolume.Remove(conf.DockerClient, ctx, id, forceRem); err != nil {
if err := dockerVolume.Remove(ctx, conf.DockerClient, id, forceRem); err != nil {
if errdefs.IsNotFound(err) {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
Expand All @@ -85,7 +85,7 @@ func PruneVolumes() gin.HandlerFunc {
return
}

report, err := dockerVolume.Prune(conf.DockerClient, ctx)
report, err := dockerVolume.Prune(ctx, conf.DockerClient)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -125,7 +125,7 @@ func CreateVolume() gin.HandlerFunc {
opts.Labels = *createVolumeRequest.Labels
}

vol, err := dockerVolume.Create(conf.DockerClient, ctx, opts)
vol, err := dockerVolume.Create(ctx, conf.DockerClient, opts)
if err != nil {
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func PasswordValidation(fl validator.FieldLevel) bool {
return letter(password) && number(password)
}

func AuthMiddleWare() gin.HandlerFunc {
func MiddleWare() gin.HandlerFunc {
return func(c *gin.Context) {
clientToken := c.Request.Header.Get("token")
if clientToken == "" {
Expand All @@ -54,7 +54,7 @@ func AuthMiddleWare() gin.HandlerFunc {
}
}
}
user, err := store.GetUserById(uid)
user, err := store.GetUserByID(uid)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
c.Abort()
Expand Down
10 changes: 4 additions & 6 deletions pkg/auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

var secretKey = []byte("secret-key")

func GenerateToken(userId int) (*string, error) {
func GenerateToken(userID int) (*string, error) {
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"sub": userId, // Subject (user identifier)
"sub": userID, // Subject (user identifier)
"iss": "wharf", // Issuer// Audience (user role)
"exp": time.Now().Add(time.Hour * 10).Unix(), // Expiration time = 10Hr
"iat": time.Now().Unix(), // Issued at
Expand All @@ -24,7 +24,7 @@ func GenerateToken(userId int) (*string, error) {
}

func VerifyToken(tokenString string) (jwt.MapClaims, error) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
token, err := jwt.Parse(tokenString, func(_ *jwt.Token) (interface{}, error) {
return secretKey, nil
})
if err != nil {
Expand All @@ -35,8 +35,6 @@ func VerifyToken(tokenString string) (jwt.MapClaims, error) {
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return claims, nil

} else {
return nil, fmt.Errorf("invalid token")
}
return nil, fmt.Errorf("invalid token")
}
5 changes: 2 additions & 3 deletions pkg/cache/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ func (l *DoublyLinkedList) Remove(node *Node) *Node {
l.len--
}()

tail := l.root.prev
l.isolate(tail)
return tail
l.isolate(node)
return node
}

func (l *DoublyLinkedList) isolate(node *Node) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/lrucache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (l *LRUCache) Invalidate(key string) {
if !exists {
return
}
l.list.Remove(node)
l.list.isolate(node)
delete(l.items, key)
}

Expand Down
Loading

0 comments on commit e58a2db

Please sign in to comment.