Skip to content

Commit

Permalink
refactor: multi-directory project
Browse files Browse the repository at this point in the history
  • Loading branch information
m-adawi committed Jul 16, 2024
1 parent 51a864d commit 2a660e4
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 65 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/docker-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ on:
- "go.mod"
- "go.sum"
- "Dockerfile"
branches:
- main


jobs:
docker:
Expand Down Expand Up @@ -38,14 +37,15 @@ jobs:

- name: Login to Docker registry
uses: docker/login-action@v3
if: ${{ github.ref_name == 'main' }}
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Push docker image
uses: docker/build-push-action@v6
if: ${{ steps.release.outputs.new_release_published == 'true' }}
if: ${{ github.ref_name == 'main' && steps.release.outputs.new_release_published == 'true' }}
with:
push: true
tags: ghcr.io/${{ github.repository }}:latest, ghcr.io/${{ github.repository }}:${{ steps.release.outputs.new_release_version }}
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ FROM golang:1.22.5 AS base

COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /swarm-cd
COPY cmd/ cmd/
COPY util/ util/
COPY web/ web/
COPY swarmcd/ swarmcd/
RUN CGO_ENABLED=0 GOOS=linux go build -o /swarm-cd

FROM alpine:3.2
WORKDIR /app
Expand Down
29 changes: 29 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"os"

"github.com/m-adawi/swarm-cd/swarmcd"
"github.com/m-adawi/swarm-cd/util"
"github.com/m-adawi/swarm-cd/web"
)

func init() {
err := util.LoadConfigs()
handleInitError(err)
err = swarmcd.Init()
handleInitError(err)
}

func main() {
go swarmcd.Run()
web.RunServer()
}

func handleInitError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
Expand Down
30 changes: 9 additions & 21 deletions init.go → swarmcd/init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package swarmcd

import (
"errors"
Expand All @@ -13,24 +13,19 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport/http"
)

var repos map[string]*git.Repository = make(map[string]*git.Repository)

var dockerCli *command.DockerCli

func init() {
err := initConfigs()
handleInitError(err)

func Init() (err error ) {
err = initRepos()
handleInitError(err)

if err != nil {
return err
}
initStacks()

err = initDockerCli()
handleInitError(err)
if err != nil {
return err
}
return
}


func initRepos() (err error) {
for repoName, repoConfig := range config.RepoConfigs {
repoPath := path.Join(config.ReposPath, repoName)
Expand Down Expand Up @@ -87,10 +82,3 @@ func initDockerCli() (err error) {
}
return nil
}

func handleInitError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
29 changes: 21 additions & 8 deletions main.go → swarmcd/swarmcd.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
package main
package swarmcd

import (
"fmt"
"log/slog"
"path"
"sync"
"time"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/stack"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/m-adawi/swarm-cd/util"
)

var repoLocks map[string]*sync.Mutex = make(map[string]*sync.Mutex)

var stackStatus map[string]*StackStatus = map[string]*StackStatus{}

type StackStatus struct {
Error string
Revision string
RepoURL string
}

func main() {
go runWebServer()
var repoLocks map[string]*sync.Mutex = make(map[string]*sync.Mutex)

var stackStatus map[string]*StackStatus = map[string]*StackStatus{}

var config *util.Config = &util.Configs

var logger *slog.Logger = util.Logger

var repos map[string]*git.Repository = make(map[string]*git.Repository)

var dockerCli *command.DockerCli

func Run() {
logger.Info("starting SwarmCD")
for {
var waitGroup sync.WaitGroup
Expand All @@ -36,7 +46,6 @@ func main() {
logger.Info("waiting for the update interval")
time.Sleep(time.Duration(config.UpdateInterval) * time.Second)
}

}

func updateStackThread(waitGroup *sync.WaitGroup, stackName string) {
Expand Down Expand Up @@ -113,3 +122,7 @@ func pullChanges(stackName string) (revision string, err error) {
ref, err := repo.Head()
return ref.Hash().String()[:8], err
}

func GetStackStatus() map[string]*StackStatus {
return stackStatus
}
16 changes: 8 additions & 8 deletions config.go → util/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package util

import (
"errors"
Expand Down Expand Up @@ -26,20 +26,20 @@ type Config struct {
RepoConfigs map[string]*RepoConfig `mapstructure:"repos"`
}

var config Config
var Configs Config

func initConfigs() (err error) {
func LoadConfigs() (err error) {
err = readConfig()
if err != nil {
return fmt.Errorf("could not read configuration file: %w", err)
}
if config.RepoConfigs == nil {
if Configs.RepoConfigs == nil {
err = readRepoConfigs()
if err != nil {
return fmt.Errorf("could not read repos file: %w", err)
}
}
if config.StackConfigs == nil {
if Configs.StackConfigs == nil {
err = readStackConfigs()
if err != nil {
return fmt.Errorf("could not load stacks file: %w", err)
Expand All @@ -58,7 +58,7 @@ func readConfig () (err error) {
if err != nil && !errors.As(err, &viper.ConfigFileNotFoundError{}){
return
}
err = configViper.Unmarshal(&config)
err = configViper.Unmarshal(&Configs)
if err != nil {
return
}
Expand All @@ -73,7 +73,7 @@ func readRepoConfigs() (err error) {
if err != nil {
return
}
err = reposViper.Unmarshal(&config.RepoConfigs)
err = reposViper.Unmarshal(&Configs.RepoConfigs)
if err != nil {
return
}
Expand All @@ -88,7 +88,7 @@ func readStackConfigs() (err error) {
if err != nil {
return
}
err = stacksViper.Unmarshal(&config.StackConfigs)
err = stacksViper.Unmarshal(&Configs.StackConfigs)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions log.go → util/log.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package main
package util

import (
"log/slog"
"os"
)

var logger *slog.Logger
var Logger *slog.Logger

func init() {
logOptions := &slog.HandlerOptions{
Level: slog.LevelInfo,
}

logger = slog.New(slog.NewTextHandler(os.Stderr, logOptions))
Logger = slog.New(slog.NewTextHandler(os.Stderr, logOptions))
}

20 changes: 0 additions & 20 deletions web.go

This file was deleted.

12 changes: 12 additions & 0 deletions web/controllers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package web

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/m-adawi/swarm-cd/swarmcd"
)

func getStacks(ctx *gin.Context) {
ctx.IndentedJSON(http.StatusOK, swarmcd.GetStackStatus())
}
19 changes: 19 additions & 0 deletions web/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package web

import (

"github.com/gin-gonic/gin"
"github.com/m-adawi/swarm-cd/util"
sloggin "github.com/samber/slog-gin"
)

var router *gin.Engine = gin.New()

func init(){
router.Use(sloggin.New(util.Logger))
router.GET("/stacks", getStacks)
}

func RunServer() {
router.Run("localhost:8080")
}

0 comments on commit 2a660e4

Please sign in to comment.