Skip to content

Commit

Permalink
Add mode to run for a single service in repo root
Browse files Browse the repository at this point in the history
  • Loading branch information
andresterba committed Jun 20, 2022
1 parent 680bef8 commit 6995e13
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 39 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ It only supports `git`.

I wrote this tool to support my personal container infrastructure, which is completely managed via a single
git repository.
There are two different modes:

- single directory
- Subdiretorie per service

## Single directory

The target repository could look like this:

```sh
.
├── .upstream-watch.yaml
├── README.md
├── .update-hooks.yaml
├── docker-compose.yml
└── upstream-watch
```

All configuration files are in a single directory, which must be the root of the git directory.
Use this mode by setting `single_directory_mode: true`.
The rest of the needed configuration is identical to the subdirectory documentation.

## Use subdirectories for different services

The target repository could look like this:

```sh
Expand All @@ -26,6 +50,7 @@ The `config.yaml` is the main configuration file for this instance of `upstream-
You can set the retry interval (in seconds) and folders that should be ignored.

```sh
single_directory_mode: false
retry_intervall: 10
ignore_folders: [".git", ".test"]
```
Expand Down
109 changes: 75 additions & 34 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,91 @@ import (
"github.com/andresterba/upstream-watch/internal/updater"
)

func main() {
for {
loadedConfig, err := config.GetConfig(".upstream-watch.yaml")
func pullUpstreamRepository() {
runCommand := exec.Command("git", "pull")
output, err := runCommand.CombinedOutput()
if err != nil {
log.Fatalf("Failed to pull upstream repository\n%s\n", output)
}

log.Print("Successfully pulled upstream repository")
}

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

ds := files.NewDirectoryScanner(loadedConfig.IgnoreFolders)
directories, err := ds.ListDirectories()
if err != nil {
log.Fatalf("failed to list directories\n%s\n", err)
}

for _, subdirectory := range directories {
updateConfig, err := config.GetUpdateConfig(subdirectory + "/.update-hooks.yaml")
if err != nil {
log.Fatal(err)
log.Printf("Failed to update submodule %s: %+v", subdirectory, err)
}

runCommand := exec.Command("git", "pull")
output, err := runCommand.CombinedOutput()
updater := updater.NewUpdater(
subdirectory,
updateConfig.PreUpdateCommands,
updateConfig.PostUpdateCommands,
db,
)
err = updater.Update()
if err != nil {
log.Fatalf("Failed to pull upstream repository\n%s\n", output)
log.Printf("Failed to update submodule %s: %+v", subdirectory, err)
continue
}

log.Print("Successfully pulled upstream repository")
log.Printf("Successfully updated submodule %s", subdirectory)
}

<-time.After(loadedConfig.RetryIntervall * time.Second)
}

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

ds := files.NewDirectoryScanner(loadedConfig.IgnoreFolders)
directories, err := ds.ListDirectories()
subdirectory := "."
updateConfig, err := config.GetUpdateConfig(subdirectory + "/.update-hooks.yaml")
if err != nil {
log.Printf("Failed to update root: %+v", err)
}

updater := updater.NewUpdater(
subdirectory,
updateConfig.PreUpdateCommands,
updateConfig.PostUpdateCommands,
db,
)
err = updater.Update()
if err != nil {
log.Printf("Failed to update root: %+v", err)
} else {
log.Printf("Successfully updated root")
}

<-time.After(loadedConfig.RetryIntervall * time.Second)
}

func main() {
for {
loadedConfig, err := config.GetConfig(".upstream-watch.yaml")
if err != nil {
log.Fatalf("failed to list directories\n%s\n", output)
log.Fatal(err)
}

db := updater.NewDatabase()

for _, subdirectory := range directories {
updateConfig, err := config.GetUpdateConfig(subdirectory + "/.update-hooks.yaml")
if err != nil {
log.Printf("Failed to update submodule %s: %+v", subdirectory, err)
}

updater := updater.NewUpdater(
subdirectory,
updateConfig.PreUpdateCommands,
updateConfig.PostUpdateCommands,
db,
)
err = updater.Update()
if err != nil {
log.Printf("Failed to update submodule %s: %+v", subdirectory, err)
continue
}

log.Printf("Successfully updated submodule %s", subdirectory)
}
updateDb := updater.NewDatabase()

<-time.After(loadedConfig.RetryIntervall * time.Second)
rootDirectoryeMode := loadedConfig.SingleDirectoryMode

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

case false:
updateSubdirectories(loadedConfig, updateDb)
}
}
}
5 changes: 3 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

type Config struct {
RetryIntervall time.Duration `yaml:"retry_intervall,omitempty"`
IgnoreFolders []string `yaml:"ignore_folders,omitempty"`
SingleDirectoryMode bool `yaml:"single_directory_mode,omitempty"`
RetryIntervall time.Duration `yaml:"retry_intervall,omitempty"`
IgnoreFolders []string `yaml:"ignore_folders,omitempty"`
}

func GetConfig(path string) (*Config, error) {
Expand Down
19 changes: 16 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ func TestGetConfig(t *testing.T) {
{
name: "should parse config",
args: args{
path: "testdata/.upstream-watch.yaml",
path: "testdata/.upstream-watch-1.yaml",
},
want: &Config{
RetryIntervall: 1337,
IgnoreFolders: []string{".git", ".test"},
SingleDirectoryMode: false,
RetryIntervall: 1337,
IgnoreFolders: []string{".git", ".test"},
},
wantErr: false,
},
Expand All @@ -34,6 +35,18 @@ func TestGetConfig(t *testing.T) {
want: nil,
wantErr: true,
},
{
name: "should parse config",
args: args{
path: "testdata/.upstream-watch-2.yaml",
},
want: &Config{
SingleDirectoryMode: true,
RetryIntervall: 1337,
IgnoreFolders: []string{".git", ".test"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions internal/config/testdata/.upstream-watch-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
single_directory_mode: false
retry_intervall: 1337
ignore_folders: [".git", ".test"]
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
single_directory_mode: true
retry_intervall: 1337
ignore_folders: [".git", ".test"]

0 comments on commit 6995e13

Please sign in to comment.