Compare commits

..

No commits in common. "ad77710ad1e9adf5fed14bd0470a5460966c85d0" and "2abed91918aea8afe27bf7e33da87ff9831d03fc" have entirely different histories.

16 changed files with 77 additions and 103 deletions

4
.env.example Normal file
View File

@ -0,0 +1,4 @@
SCRIPT_BINARY_PATH=
WEBHOOK_SCRIPT_PATH=
FILE_TO_WATCH_PATH=
TEST_FILE_TO_WATCH_PATH=

View File

@ -1,28 +0,0 @@
FROM golang:1.22 as builder
WORKDIR /app
# Copy the local package files to the container's workspace.
COPY go.mod go.sum ./
RUN go mod download
# Copy the project code into the container
COPY . .
# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -v -o git-webhook-ci
# Use a small Alpine Linux image
FROM alpine:latest
# Set the working directory
WORKDIR /root/
# Copy the binary from the builder stage
COPY --from=builder /app/git-webhook-ci .
# Document that the service listens on port 8080.
EXPOSE 8080
# Run the binary.
CMD ["./git-webhook-ci"]

View File

@ -1,3 +0,0 @@
script_binary_path: "/bin/bash"
webhook_script_path: "./test-script.sh"
file_to_watch_path: "./test_monitor.txt"

45
cfg/config.go Normal file
View File

@ -0,0 +1,45 @@
package cfg
import (
"log"
"os/exec"
"strings"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
ScriptBinaryPath string `required:"true" split_words:"true"`
WebhookScriptPath string `required:"true" split_words:"true"`
FileToWatchPath string `required:"true" split_words:"true"`
TestFileToWatchPath string `required:"false" split_words:"true"`
}
func RootDir() string {
cmdOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
log.Fatalf("exec.Command: %s", err)
return ""
}
rootDir := strings.TrimSpace(string(cmdOut))
return rootDir
}
func NewConfig(envFilePath string) *Config {
if envFilePath != "" {
err := godotenv.Load(envFilePath)
if err != nil {
log.Fatalf("environment variable ENV is empty and an error occurred while loading the .env file\n")
}
}
cfg := &Config{}
err := envconfig.Process("", cfg)
if err != nil {
log.Fatalf("envconfig.Process: %s\n", err)
}
return cfg
}

View File

@ -1,3 +0,0 @@
script_binary_path: "/bin/bash"
webhook_script_path: "./test-script.sh"
file_to_watch_path: "./test_monitor.txt"

3
go.mod
View File

@ -4,8 +4,9 @@ go 1.22
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/joho/godotenv v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.8.2
gopkg.in/yaml.v2 v2.4.0
)
require (

6
go.sum
View File

@ -3,6 +3,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@ -16,8 +20,6 @@ golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,3 +0,0 @@
script_binary_path: "/bin/bash"
webhook_script_path: "testdata/test-script.sh"
file_to_watch_path: "testdata/test_monitor.txt"

View File

@ -4,13 +4,13 @@ import (
"errors"
"log"
"gitea.urkob.com/urko/git-webhook-ci/pkg"
pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher"
"github.com/fsnotify/fsnotify"
)
type watcher struct {
fswatcher *fsnotify.Watcher
deploy pkg.DeployFunc
deploy pkgwatcher.DeployFunc
}
type notifier struct{}
@ -28,7 +28,7 @@ var (
errErrorsClosedChan = errors.New("errors is closed")
)
func NewWatcher(notifier pkg.NotifyIface, deploy pkg.DeployFunc) *watcher {
func NewWatcher(notifier pkgwatcher.NotifyIface, deploy pkgwatcher.DeployFunc) *watcher {
wt, err := notifier.NewWatcher()
if err != nil {
log.Printf("fsnotify.NewWatcher: %s\n", err)

View File

@ -3,12 +3,11 @@ package watcher
import (
"context"
"errors"
"fmt"
"testing"
"time"
"gitea.urkob.com/urko/git-webhook-ci/kit/config"
"gitea.urkob.com/urko/git-webhook-ci/pkg"
"gitea.urkob.com/urko/git-webhook-ci/cfg"
pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher"
"github.com/fsnotify/fsnotify"
"github.com/stretchr/testify/require"
)
@ -27,13 +26,13 @@ var (
)
var (
mockDeploy pkg.DeployFunc
mockErrorDeploy pkg.DeployFunc
mockDeploy pkgwatcher.DeployFunc
mockErrorDeploy pkgwatcher.DeployFunc
errIntentional = errors.New("intentional error")
binaryPath = ""
scriptPath = ""
executionMaxTimeout = time.Second * 2
cfg *config.Config
config *cfg.Config
events = []fsnotify.Event{
{
Name: "test event",
@ -46,7 +45,7 @@ var (
}
)
func TestMain(t *testing.M) {
func init() {
mockDeploy = func(binaryPath, scriptPath string) error {
return nil
}
@ -54,16 +53,8 @@ func TestMain(t *testing.M) {
mockErrorDeploy = func(binaryPath, scriptPath string) error {
return errIntentional
}
}
func LoadConfig(t *testing.T) {
t.Helper()
cf, err := config.LoadConfig("testdata/config.yaml")
if err != nil {
panic(fmt.Errorf("Error loading config: %w", err))
}
cfg = cf
config = cfg.NewConfig("../../.env")
}
func sendTestEvents(w *watcher) {
@ -85,7 +76,6 @@ func newWatcherWithCtorError() *watcher {
}
func Test_NewNotifier(t *testing.T) {
LoadConfig(t)
require.NotNil(t, NewNotifier())
}
@ -107,7 +97,7 @@ func Test_Close(t *testing.T) {
func Test_Monitor(t *testing.T) {
w := newWatcher()
err := w.Monitor(cfg.FileToWatchPath)
err := w.Monitor(config.TestFileToWatchPath)
require.NoError(t, err)
}

View File

@ -1,27 +0,0 @@
package config
import (
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
ScriptBinaryPath string `yaml:"script_binary_path"`
WebhookScriptPath string `yaml:"webhook_script_path"`
FileToWatchPath string `yaml:"file_to_watch_path"`
}
func LoadConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, err
}
return &config, nil
}

15
main.go
View File

@ -7,14 +7,14 @@ import (
"os/signal"
"syscall"
"gitea.urkob.com/urko/git-webhook-ci/cfg"
"gitea.urkob.com/urko/git-webhook-ci/internal/watcher"
"gitea.urkob.com/urko/git-webhook-ci/kit/config"
"gitea.urkob.com/urko/git-webhook-ci/pkg"
pkgwatcher "gitea.urkob.com/urko/git-webhook-ci/pkg/watcher"
)
var (
watcherIface pkg.WatcherIface
notifierIface pkg.NotifyIface
watcherIface pkgwatcher.WatcherIface
notifierIface pkgwatcher.NotifyIface
)
func main() {
@ -24,13 +24,10 @@ func main() {
flag.Parse()
}
config, err := config.LoadConfig(envFilePath)
if err != nil {
panic(err)
}
config := cfg.NewConfig(envFilePath)
notifierIface = watcher.NewNotifier()
watcherIface = watcher.NewWatcher(notifierIface, pkg.Deploy)
watcherIface = watcher.NewWatcher(notifierIface, pkgwatcher.Deploy)
defer func() {
if err := watcherIface.Close(); err != nil {

View File

@ -1,2 +0,0 @@
#!/bin/bash
echo "deploy script has been called"

View File

@ -1,4 +1,4 @@
package pkg
package watcher
import (
"fmt"

View File

@ -1,14 +1,15 @@
package pkg
package watcher
import (
"testing"
"gitea.urkob.com/urko/git-webhook-ci/cfg"
"github.com/stretchr/testify/require"
)
const (
var (
binaryPath = "/bin/bash"
scriptPath = "testdata/test-script.sh"
scriptPath = cfg.RootDir() + "/test-script.sh"
)
func TestDeploy(t *testing.T) {