-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2fbaef
commit 707e904
Showing
13 changed files
with
578 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package bindings | ||
|
||
import ( | ||
"io/fs" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package bindings | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"reflect" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/balazsgrill/potatodrive/filesystem" | ||
"github.com/spf13/afero" | ||
"golang.org/x/sys/windows/registry" | ||
) | ||
|
||
func ConfigToFlags(config any) { | ||
structPtrValue := reflect.ValueOf(config) | ||
structValue := structPtrValue.Elem() | ||
structType := structValue.Type() | ||
for i := 0; i < structType.NumField(); i++ { | ||
field := structType.Field(i) | ||
|
||
tagstr := field.Tag.Get("flag") | ||
if tagstr != "" { | ||
tagdata := strings.Split(tagstr, ",") | ||
tag := tagdata[0] | ||
msg := tagdata[1] | ||
switch field.Type.Kind() { | ||
case reflect.String: | ||
flag.StringVar((*string)(structValue.Field(i).Addr().UnsafePointer()), tag, "", msg) | ||
case reflect.Bool: | ||
flag.BoolVar((*bool)(structValue.Field(i).Addr().UnsafePointer()), tag, false, msg) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func ReadConfigFromRegistry(key registry.Key, config any) error { | ||
structPtrValue := reflect.ValueOf(config) | ||
structValue := structPtrValue.Elem() | ||
structType := structValue.Type() | ||
for i := 0; i < structType.NumField(); i++ { | ||
field := structType.Field(i) | ||
fieldvalue := structValue.Field(i) | ||
tag := field.Tag.Get("reg") | ||
if tag != "" { | ||
switch field.Type.Kind() { | ||
case reflect.String: | ||
value, _, err := key.GetStringValue(tag) | ||
if os.IsNotExist(err) { | ||
continue | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
fieldvalue.SetString(value) | ||
case reflect.Bool: | ||
value, _, err := key.GetIntegerValue(tag) | ||
if os.IsNotExist(err) { | ||
continue | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
fieldvalue.SetBool(value != 0) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func BindVirtualizationInstance(localpath string, remotefs afero.Fs) error { | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
closer, err := filesystem.StartProjecting(localpath, remotefs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t := time.NewTicker(30 * time.Second) | ||
go func() { | ||
for range t.C { | ||
err = closer.PerformSynchronization() | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
}() | ||
|
||
<-c | ||
t.Stop() | ||
closer.Close() | ||
os.Exit(1) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package s3 | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/balazsgrill/potatodrive/bindings" | ||
s3 "github.com/fclairamb/afero-s3" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
type Config struct { | ||
LocalPath string `flag:"localpath,Local folder" reg:"LocalPath"` | ||
Endpoint string `flag:"endpoint,S3 endpoint" reg:"Endpoint"` | ||
Region string `flag:"region,Region" reg:"Region"` | ||
Bucket string `flag:"bucket,Bucket" reg:"Bucket"` | ||
KeyId string `flag:"keyid,Access Key ID" reg:"KeyID"` | ||
KeySecret string `flag:"secret,Access Key Secret" reg:"KeySecret"` | ||
UseSSL bool `flag:"useSSL,Use SSL encryption for S3 connection" reg:"UseSSL"` | ||
} | ||
|
||
func (c *Config) Validate() error { | ||
if c.Endpoint == "" { | ||
return errors.New("endpoint is mandatory") | ||
} | ||
if c.LocalPath == "" { | ||
return errors.New("localpath is mandatory") | ||
} | ||
if c.Region == "" { | ||
return errors.New("region is mandatory") | ||
} | ||
if c.Bucket == "" { | ||
return errors.New("bucket is mandatory") | ||
} | ||
if c.KeyId == "" { | ||
return errors.New("keyid is mandatory") | ||
} | ||
if c.KeySecret == "" { | ||
return errors.New("secret is mandatory") | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Config) ToFileSystem() (afero.Fs, error) { | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String(c.Region), | ||
Endpoint: aws.String(c.Endpoint), | ||
DisableSSL: aws.Bool(!c.UseSSL), | ||
S3ForcePathStyle: aws.Bool(true), | ||
Credentials: credentials.NewStaticCredentials(c.KeyId, c.KeySecret, ""), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fs := s3.NewFs(c.Bucket, sess) | ||
fs.MkdirAll("root", 0777) | ||
rootfs := bindings.NewBasePathFs(fs, "root") | ||
return rootfs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Golang bingings for [Windows Projected File System API](https://learn.microsoft.com/en-us/windows/win32/projfs/projected-file-system). | ||
It can be used to implement user-space filesystem virtualization mounted to a local directory with local cache not unlike OneDrive. |
Oops, something went wrong.