Skip to content

Commit

Permalink
Add container
Browse files Browse the repository at this point in the history
  • Loading branch information
André Sterba authored and andresterba committed Jan 20, 2024
1 parent 25bf0c7 commit acec8d0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:1.21 AS build-stage

WORKDIR /app

COPY go.mod go.sum .
RUN go mod download

COPY . .

RUN apt-get update && \
apt-get upgrade -y

RUN apt-get install -y build-essential make

RUN make build

FROM gcr.io/distroless/base-debian12 AS build-release-stage

WORKDIR /

COPY --from=build-stage /app/upstream-watch /app/upstream-watch

USER nonroot:nonroot

ENTRYPOINT ["/app/upstream-watch", "/workdir"]
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ testcoverage:
$(GOTEST) -coverprofile coverage.out ./... && go tool cover -html=coverage.out && rm coverage.out
lint:
staticcheck -f stylish github.com/andresterba/upstream-watch/...
containerize:
docker build -t upstream-watch:test .
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git repository.
There are two different modes:

- single directory
- Subdiretorie per service
- Subdirectories per service

## Single directory

Expand Down
29 changes: 22 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"path"
"time"

"github.com/andresterba/upstream-watch/internal/config"
Expand All @@ -20,11 +23,11 @@ func pullUpstreamRepository() {
log.Print("Successfully pulled upstream repository")
}

func updateSubdirectories(loadedConfig *config.Config, db updater.Database) {
func updateSubdirectories(runPath string, loadedConfig *config.Config, db updater.Database) {
pullUpstreamRepository()

ds := files.NewDirectoryScanner(loadedConfig.IgnoreFolders)
directories, err := ds.ListDirectories()
directories, err := ds.ListDirectories(runPath)
if err != nil {
log.Fatalf("failed to list directories\n%s\n", err)
}
Expand Down Expand Up @@ -55,10 +58,10 @@ func updateSubdirectories(loadedConfig *config.Config, db updater.Database) {
<-time.After(loadedConfig.RetryInterval * time.Second)
}

func updateRootRepository(loadedConfig *config.Config, db updater.Database) {
func updateRootRepository(runPath string, loadedConfig *config.Config, db updater.Database) {
pullUpstreamRepository()

subdirectory := "."
subdirectory := path.Join(runPath, "/")
updateConfig, err := config.GetUpdateConfig(subdirectory + "/.update-hooks.yaml")
if err != nil {
log.Printf("Failed to update root: %+v", err)
Expand All @@ -81,9 +84,21 @@ func updateRootRepository(loadedConfig *config.Config, db updater.Database) {
<-time.After(loadedConfig.RetryInterval * time.Second)
}

const configName = ".upstream-watch.yaml"

func main() {
args := os.Args
if len(args) != 2 {
fmt.Printf("Please provide specify the directory to run in as arg.\n")
os.Exit(0)
}

runPath := os.Args[1]

pathToConfig := path.Join(runPath, configName)

for {
loadedConfig, err := config.GetConfig(".upstream-watch.yaml")
loadedConfig, err := config.GetConfig(pathToConfig)
if err != nil {
log.Fatal(err)
}
Expand All @@ -94,10 +109,10 @@ func main() {

switch rootDirectoryeMode {
case true:
updateRootRepository(loadedConfig, updateDb)
updateRootRepository(runPath, loadedConfig, updateDb)

case false:
updateSubdirectories(loadedConfig, updateDb)
updateSubdirectories(runPath, loadedConfig, updateDb)
}
}
}
4 changes: 2 additions & 2 deletions internal/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func NewDirectoryScanner(directoriesToIgnore []string) *DirectoryScanner {
}
}

func (ds *DirectoryScanner) ListDirectories() ([]string, error) {
func (ds *DirectoryScanner) ListDirectories(runPath string) ([]string, error) {
subdirectories := []string{}
entries, err := os.ReadDir(".")
entries, err := os.ReadDir(runPath)
if err != nil {
return nil, fmt.Errorf("failed to list directories %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestDirectoryScanner_ListDirectories(t *testing.T) {
ds := &DirectoryScanner{
directoriesToIgnore: tt.fields.directoriesToIgnore,
}
got, err := ds.ListDirectories()
got, err := ds.ListDirectories(".")
if (err != nil) != tt.wantErr {
t.Errorf("DirectoryScanner.ListDirectories() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit acec8d0

Please sign in to comment.