Skip to content

Commit

Permalink
redesign: session mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
arshamalh committed Nov 23, 2023
1 parent 57b063c commit 6f8a485
Show file tree
Hide file tree
Showing 24 changed files with 299 additions and 322 deletions.
4 changes: 2 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/arshamalh/dockeroller/docker"
"github.com/arshamalh/dockeroller/log"
"github.com/arshamalh/dockeroller/repo/ephemeral"
"github.com/arshamalh/dockeroller/session"
tpkg "github.com/arshamalh/dockeroller/telegram"
"github.com/arshamalh/dockeroller/telegram/handlers"
"github.com/joho/godotenv"
Expand Down Expand Up @@ -60,7 +60,7 @@ func startTelegram(docker docker.Docker, token string, whitelistedIDs []int64) {
if err != nil {
log.Gl.Error(err.Error())
}
session := ephemeral.New()
session := session.New()
handlers.Register(bot, docker, session)
// Middlewares
bot.Use(middleware.Whitelist(whitelistedIDs...))
Expand Down
16 changes: 8 additions & 8 deletions docker/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import (
"context"
"io"

"github.com/arshamalh/dockeroller/models"
"github.com/arshamalh/dockeroller/entities"
"github.com/docker/docker/api/types"
)

func (d *docker) ContainersList() (containers []*models.Container) {
func (d *docker) ContainersList() (containers []*entities.Container) {
raw_containers, _ := d.cli.ContainerList(context.TODO(), types.ContainerListOptions{All: true})
for _, raw_cont := range raw_containers {
containers = append(containers, &models.Container{
containers = append(containers, &entities.Container{
ID: raw_cont.ID,
Name: raw_cont.Names[0],
Image: raw_cont.Image,
Status: raw_cont.Status,
State: models.ContainerState(raw_cont.State),
State: entities.ContainerState(raw_cont.State),
})
}
return
}

func (d *docker) GetContainer(containerID string) (*models.Container, error) {
func (d *docker) GetContainer(containerID string) (*entities.Container, error) {
container, err := d.cli.ContainerInspect(context.TODO(), containerID)
if err != nil {
return nil, err
}
return &models.Container{
return &entities.Container{
ID: container.ID,
Name: container.Name,
Image: container.Image,
Status: container.State.Status,
State: models.ContainerState(container.State.Status),
State: entities.ContainerState(container.State.Status),
}, nil
}

Expand All @@ -59,7 +59,7 @@ func (d *docker) ContainerStop(containerID string) error {
return d.cli.ContainerStop(context.TODO(), containerID, nil)
}

func (d *docker) ContainerRemove(containerID string, removeForm *models.ContainerRemoveForm) error {
func (d *docker) ContainerRemove(containerID string, removeForm *entities.ContainerRemoveForm) error {
return d.cli.ContainerRemove(context.TODO(), containerID, types.ContainerRemoveOptions{
RemoveVolumes: removeForm.RemoveVolumes,
Force: removeForm.Force,
Expand Down
10 changes: 5 additions & 5 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import (
"context"
"io"

"github.com/arshamalh/dockeroller/entities"
"github.com/arshamalh/dockeroller/log"
"github.com/arshamalh/dockeroller/models"
"github.com/moby/moby/client"
)

type Docker interface {
GetContainer(containerID string) (*models.Container, error)
ContainersList() []*models.Container
GetContainer(containerID string) (*entities.Container, error)
ContainersList() []*entities.Container
ContainerLogs(containerID string) (io.ReadCloser, error)
ContainerStats(containerID string) (io.ReadCloser, error)
ContainerStart(containerID string) error
ContainerStop(containerID string) error
ContainerRemove(containerID string, removeForm *models.ContainerRemoveForm) error
ContainerRemove(containerID string, removeForm *entities.ContainerRemoveForm) error
ContainerRename(containerID, newName string) error

ImagesList() []*models.Image
ImagesList() []*entities.Image
ImageTag(ctx context.Context, imageID, newTag string) error
ImageRemove(ctx context.Context, imageID string, force, pruneChildren bool) error
}
Expand Down
14 changes: 7 additions & 7 deletions docker/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"fmt"
"time"

"github.com/arshamalh/dockeroller/models"
"github.com/arshamalh/dockeroller/entities"
"github.com/docker/docker/api/types"
)

func (d *docker) ImagesList() (images []*models.Image) {
func (d *docker) ImagesList() (images []*entities.Image) {
raw_images, _ := d.cli.ImageList(context.TODO(), types.ImageListOptions{All: true})
for _, raw_img := range raw_images {
status := d.getImageStatus(context.TODO(), raw_img)
images = append(images, &models.Image{
images = append(images, &entities.Image{
ID: raw_img.ID,
Size: raw_img.Size,
Tags: raw_img.RepoTags,
Status: models.ImageStatus(status),
Status: entities.ImageStatus(status),
CreatedAt: fmt.Sprint(time.Unix(raw_img.Created, 0).Format("2006-01-02 15:04:05")),
})
}
Expand All @@ -40,14 +40,14 @@ func (d *docker) ImageRemove(ctx context.Context, imageID string, force, pruneCh

func (d *docker) getImageStatus(ctx context.Context, image types.ImageSummary) (status string) {
if len(image.RepoTags) == 0 {
status = string(models.ImageStatusUnUsedDangling)
status = string(entities.ImageStatusUnUsedDangling)
}

containers, _ := d.cli.ContainerList(ctx, types.ContainerListOptions{})
newImgs := make(map[string][]string)
for _, cont := range containers {
if cont.ImageID != image.ID {
status = string(models.ImageStatusUnUsed)
status = string(entities.ImageStatusUnUsed)
} else {
newSlice := newImgs[image.ID]
if newSlice == nil {
Expand All @@ -56,7 +56,7 @@ func (d *docker) getImageStatus(ctx context.Context, image types.ImageSummary) (
newSlice = append(newSlice, cont.ID)
newImgs[image.ID] = newSlice

status = string(models.ImageStatusInUse)
status = string(entities.ImageStatusInUse)
}
}

Expand Down
2 changes: 1 addition & 1 deletion models/container.go → entities/container.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

import "fmt"

Expand Down
8 changes: 4 additions & 4 deletions models/userdata.go → entities/forms.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package models
package entities

type UserData struct {
ContainerRemoveForm *ContainerRemoveForm
ImageRemoveForm *ImageRemoveForm
type Forms struct {
ContainerRemove *ContainerRemoveForm
ImageRemove *ImageRemoveForm
}

type ContainerRemoveForm struct {
Expand Down
2 changes: 1 addition & 1 deletion models/image.go → entities/image.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

type ImageStatus string

Expand Down
34 changes: 15 additions & 19 deletions models/queue.go → entities/queue.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

import "fmt"

Expand All @@ -25,19 +25,6 @@ func NewQueue() *Queue {
return &Queue{}
}

func (q *Queue) isZeroOrOneNode() (bool, *Node) {
if q.Length == 0 {
return true, nil
} else if q.Length == 1 {
value := q.head
q.head = nil
q.tail = nil
q.Length--
return true, value
}
return false, nil
}

func (q *Queue) Push(value string) *Queue {
new_node := newNode(value, nil)
if q.Length == 0 {
Expand All @@ -50,14 +37,23 @@ func (q *Queue) Push(value string) *Queue {
return q
}

func (q *Queue) Pop() *Node {
if ok, node := q.isZeroOrOneNode(); ok {
return node
// Removes a Node from the queue and returns it,
// Returns an error if there is no available node
func (q *Queue) Pop() (string, error) {
if q.Length == 0 {
return "", fmt.Errorf("no node available to pop")
}

node := q.head
q.head = q.head.Next
if q.Length == 1 {
q.head = nil
q.tail = nil
} else {
q.head = q.head.Next
}

q.Length--
return node
return node.Value, nil
}

func (q *Queue) String() string {
Expand Down
2 changes: 1 addition & 1 deletion models/scene.go → entities/scene.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

type Scene int

Expand Down
2 changes: 1 addition & 1 deletion models/stats.go → entities/stats.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package entities

import "time"

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4 // indirect
go.uber.org/zap v1.26.0
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/sys v0.1.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
Expand Down
Loading

0 comments on commit 6f8a485

Please sign in to comment.