Skip to content

Commit

Permalink
Ability to handle multiple projections in a process
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgrill committed Aug 4, 2024
1 parent 707e904 commit 9abfd30
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 12 deletions.
3 changes: 3 additions & 0 deletions bindings/basepathfs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
This file is a fork of https://github.com/spf13/afero/blob/master/basepath.go modified to always use "/" as separator regardless of the host system
*/
package bindings

import (
Expand Down
31 changes: 23 additions & 8 deletions bindings/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bindings

import (
"flag"
"io"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -71,27 +72,41 @@ func ReadConfigFromRegistry(key registry.Key, config any) error {
return nil
}

func BindVirtualizationInstance(localpath string, remotefs afero.Fs) error {
func CloseOnSigTerm(closers ...io.Closer) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

<-c
for _, closer := range closers {
closer.Close()
}
os.Exit(1)
}

type closerFunc func() error

func (f closerFunc) Close() error {
return f()
}

func BindVirtualizationInstance(localpath string, remotefs afero.Fs) (io.Closer, error) {
closer, err := filesystem.StartProjecting(localpath, remotefs)
if err != nil {
return err
return nil, err
}

t := time.NewTicker(30 * time.Second)
go func() {
for range t.C {
err = closer.PerformSynchronization()
if err != nil {
log.Panic(err)
log.Println(err)
}
}
}()

<-c
t.Stop()
closer.Close()
os.Exit(1)
return nil
return (closerFunc)(func() error {
t.Stop()
return closer.Close()
}), nil
}
57 changes: 57 additions & 0 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"io"
"log"

"github.com/balazsgrill/potatodrive/bindings"
cs3 "github.com/balazsgrill/potatodrive/bindings/s3"
"golang.org/x/sys/windows/registry"
)

func main() {
parentkey, err := registry.OpenKey(registry.LOCAL_MACHINE, "SOFTWARE\\PotatoDrive", registry.QUERY_VALUE|registry.READ)
if err != nil {
panic(err)
}

keys, err := parentkey.ReadSubKeyNames(0)
if err != nil {
panic(err)
}

var instances []io.Closer

for _, keyname := range keys {
config := &cs3.Config{}
key, err := registry.OpenKey(parentkey, keyname, registry.QUERY_VALUE)
if err != nil {
log.Printf("Open key: %v", err)
continue
}
bindings.ReadConfigFromRegistry(key, config)
err = config.Validate()
if err != nil {
log.Printf("Validate config: %v", err)
continue
}
fs, err := config.ToFileSystem()
if err != nil {
log.Printf("Create file system: %v", err)
continue
}

log.Printf("Starting %s on %s", keyname, config.LocalPath)
c, err := bindings.BindVirtualizationInstance(config.LocalPath, fs)
if err != nil {
log.Println(err)
}
log.Printf("%s ended", keyname)
instances = append(instances, c)

}

bindings.CloseOnSigTerm(instances...)

select {}
}
7 changes: 4 additions & 3 deletions cmd/s3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

func main() {
regkey := flag.String("regkey", "", "Registry key that holds configuration")
regkey := flag.String("regkey", "", "Registry key that holds configuration. If set, all other arguments are ignored")
config := &cs3.Config{}
bindings.ConfigToFlags(config)
flag.Parse()

if *regkey != "" {
key, err := registry.OpenKey(registry.CURRENT_USER, *regkey, registry.QUERY_VALUE)
key, err := registry.OpenKey(registry.LOCAL_MACHINE, *regkey, registry.QUERY_VALUE)
if err != nil {
log.Panic(err)
}
Expand All @@ -33,8 +33,9 @@ func main() {
log.Panic(err)
}

err = bindings.BindVirtualizationInstance(config.LocalPath, fs)
closer, err := bindings.BindVirtualizationInstance(config.LocalPath, fs)
if err != nil {
log.Panic(err)
}
bindings.CloseOnSigTerm(closer)
}
7 changes: 6 additions & 1 deletion filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ func (instance *VirtualizationInstance) start(rootPath string, filesystem afero.
log.Printf("Error starting virtualization: %s", err)
return err
}
return instance.syncRemoteToLocal()
err = instance.syncRemoteToLocal()
if err != nil {
log.Printf("Initial sync failed: %s", err)
return nil
}
return nil
}

func (instance *VirtualizationInstance) path_localToRemote(path string) string {
Expand Down

0 comments on commit 9abfd30

Please sign in to comment.