From 91060915d061674def0bbf5d3c6464c62c9eba68 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 23 Dec 2024 01:34:56 -0600 Subject: [PATCH 1/4] maybe use cloudflare R2 Signed-off-by: Jeff Carr --- .gitignore | 1 + cf-r2-awsapi/main.go | 99 ++++++++++++++++++++++++++++++++++++++++++++ cf-r2/main.go | 26 ++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 cf-r2-awsapi/main.go create mode 100644 cf-r2/main.go diff --git a/.gitignore b/.gitignore index 34a0698..2b26862 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ go-clone-test/go-clone-test go-clone-test/goclonetest go-clone-test/goclonetesttest going2git/going2git +cf-r2/cf-r2 diff --git a/cf-r2-awsapi/main.go b/cf-r2-awsapi/main.go new file mode 100644 index 0000000..bea845d --- /dev/null +++ b/cf-r2-awsapi/main.go @@ -0,0 +1,99 @@ +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/aws-sdk-go-v2/service/s3/types" +) + +const ( + r2AccountID = "" // Replace with your Cloudflare R2 Account ID + r2AccessKey = "" // Replace with your Access Key ID + r2SecretKey = "" // Replace with your Secret Access Key + r2BucketName = "example-bucket" // Replace with your bucket name + r2Endpoint = "https://.r2.cloudflarestorage.com" // Replace with your endpoint +) + +func main() { + // Initialize the AWS Config for R2 + cfg, err := config.LoadDefaultConfig(context.TODO(), + config.WithCredentialsProvider(aws.NewCredentialsCache( + aws.NewStaticCredentialsProvider(r2AccessKey, r2SecretKey, ""), + )), + config.WithEndpointResolver(aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) { + return aws.Endpoint{URL: r2Endpoint}, nil + })), + ) + if err != nil { + log.Fatalf("Unable to load AWS config: %v", err) + } + + // Create the S3 client + client := s3.NewFromConfig(cfg) + + // Upload a file + err = uploadFile(client, "example.txt", "example.txt") + if err != nil { + log.Fatalf("Upload failed: %v", err) + } + + // Download the file + err = downloadFile(client, "example.txt", "downloaded-example.txt") + if err != nil { + log.Fatalf("Download failed: %v", err) + } +} + +// Upload a file to R2 +func uploadFile(client *s3.Client, objectKey, filePath string) error { + file, err := os.Open(filePath) + if err != nil { + return fmt.Errorf("failed to open file %s: %w", filePath, err) + } + defer file.Close() + + _, err = client.PutObject(context.TODO(), &s3.PutObjectInput{ + Bucket: aws.String(r2BucketName), + Key: aws.String(objectKey), + Body: file, + ACL: types.ObjectCannedACLPublicRead, // Optional: Make the file public + }) + if err != nil { + return fmt.Errorf("failed to upload file to R2: %w", err) + } + + fmt.Printf("File %s uploaded to R2 as %s\n", filePath, objectKey) + return nil +} + +// Download a file from R2 +func downloadFile(client *s3.Client, objectKey, destPath string) error { + resp, err := client.GetObject(context.TODO(), &s3.GetObjectInput{ + Bucket: aws.String(r2BucketName), + Key: aws.String(objectKey), + }) + if err != nil { + return fmt.Errorf("failed to download file from R2: %w", err) + } + defer resp.Body.Close() + + outFile, err := os.Create(destPath) + if err != nil { + return fmt.Errorf("failed to create file %s: %w", destPath, err) + } + defer outFile.Close() + + _, err = outFile.ReadFrom(resp.Body) + if err != nil { + return fmt.Errorf("failed to write file to %s: %w", destPath, err) + } + + fmt.Printf("File %s downloaded from R2 and saved as %s\n", objectKey, destPath) + return nil +} diff --git a/cf-r2/main.go b/cf-r2/main.go new file mode 100644 index 0000000..ce3c910 --- /dev/null +++ b/cf-r2/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +func main() { + endpoint := "play.min.io" + accessKeyID := "Q3AM3UQ867SPQQA43P2F" + secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" + useSSL := true + + // Initialize minio client object. + minioClient, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), + Secure: useSSL, + }) + if err != nil { + log.Fatalln(err) + } + + log.Printf("%#v\n", minioClient) // minioClient is now set up +} From 1df95aec09c5ac799d84367ad3721e784eb76f30 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 23 Dec 2024 01:41:48 -0600 Subject: [PATCH 2/4] more examples Signed-off-by: Jeff Carr --- .gitignore | 2 ++ cf-r2/download.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ cf-r2/upload.go | 58 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 cf-r2/download.go create mode 100644 cf-r2/upload.go diff --git a/.gitignore b/.gitignore index 2b26862..b243d3e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ go-clone-test/goclonetest go-clone-test/goclonetesttest going2git/going2git cf-r2/cf-r2 +cf-r2/download +cf-r2/upload diff --git a/cf-r2/download.go b/cf-r2/download.go new file mode 100644 index 0000000..b2f4184 --- /dev/null +++ b/cf-r2/download.go @@ -0,0 +1,66 @@ +package main + +import ( + "context" + "fmt" + "log" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +const ( + r2Endpoint = ".r2.cloudflarestorage.com" // Replace with your R2 endpoint + r2AccessKey = "" // Replace with your Access Key + r2SecretKey = "" // Replace with your Secret Key + r2BucketName = "example-bucket" // Replace with your bucket name +) + +func main() { + // Initialize MinIO client for Cloudflare R2 + client, err := minio.New(r2Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(r2AccessKey, r2SecretKey, ""), + Secure: true, // Use HTTPS + }) + if err != nil { + log.Fatalf("Failed to initialize client: %v", err) + } + + // Upload a file + err = uploadFile(client, "example.txt", "example.txt") + if err != nil { + log.Fatalf("Upload failed: %v", err) + } + + // Download the file + err = downloadFile(client, "example.txt", "downloaded-example.txt") + if err != nil { + log.Fatalf("Download failed: %v", err) + } +} + +// UploadFile uploads a file to the R2 bucket +func uploadFile(client *minio.Client, objectName, filePath string) error { + ctx := context.Background() + _, err := client.FPutObject(ctx, r2BucketName, objectName, filePath, minio.PutObjectOptions{ + ContentType: "application/octet-stream", // Adjust MIME type if needed + }) + if err != nil { + return fmt.Errorf("failed to upload file: %w", err) + } + + fmt.Printf("Successfully uploaded %s as %s\n", filePath, objectName) + return nil +} + +// DownloadFile downloads a file from the R2 bucket +func downloadFile(client *minio.Client, objectName, destPath string) error { + ctx := context.Background() + err := client.FGetObject(ctx, r2BucketName, objectName, destPath, minio.GetObjectOptions{}) + if err != nil { + return fmt.Errorf("failed to download file: %w", err) + } + + fmt.Printf("Successfully downloaded %s to %s\n", objectName, destPath) + return nil +} diff --git a/cf-r2/upload.go b/cf-r2/upload.go new file mode 100644 index 0000000..399276b --- /dev/null +++ b/cf-r2/upload.go @@ -0,0 +1,58 @@ +// FileUploader.go MinIO example +package main + +import ( + "context" + "log" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +func main() { + ctx := context.Background() + endpoint := "play.min.io" + accessKeyID := "Q3AM3UQ867SPQQA43P2F" + secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" + useSSL := true + + // Initialize minio client object. + minioClient, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), + Secure: useSSL, + }) + if err != nil { + log.Fatalln(err) + } + + // Make a new bucket called testbucket. + bucketName := "testbucket" + location := "us-east-1" + + err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location}) + if err != nil { + // Check to see if we already own this bucket (which happens if you run this twice) + exists, errBucketExists := minioClient.BucketExists(ctx, bucketName) + if errBucketExists == nil && exists { + log.Printf("We already own %s\n", bucketName) + } else { + log.Fatalln(err) + } + } else { + log.Printf("Successfully created %s\n", bucketName) + } + + // Upload the test file + // Change the value of filePath if the file is in another location + objectName := "testdata" + filePath := "/tmp/testdata" + contentType := "application/octet-stream" + + // Upload the test file with FPutObject + info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType}) + if err != nil { + log.Fatalln(err) + } + + log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size) +} From cff4af7935de4bcbb48f9b8b4c028be7e4e5e890 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 31 Dec 2024 13:26:01 -0600 Subject: [PATCH 3/4] make going2git a seperate app --- going2git/Makefile | 28 ---------------------------- going2git/message.go | 23 ----------------------- 2 files changed, 51 deletions(-) delete mode 100644 going2git/Makefile delete mode 100644 going2git/message.go diff --git a/going2git/Makefile b/going2git/Makefile deleted file mode 100644 index 1e8c9f8..0000000 --- a/going2git/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -VERSION = $(shell git describe --tags) -GUIVERSION = $(shell git describe --tags) -BUILDTIME = $(shell date +%s) - -all: build - -build: goimports - GO111MODULE=off go build \ - -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" - -vet: - GO111MODULE=off go vet - -goimports: - goimports -w *.go - # // to globally reset paths: - # // gofmt -w -r '"go.wit.com/gui/gadgets" -> "go.wit.com/lib/gadgets"' *.go - -gocui: build - reset - ./go-clone-test --gui gocui - -install: goimports - GO111MODULE=off go install \ - -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" - -force: build - ./go-clone-test --force diff --git a/going2git/message.go b/going2git/message.go deleted file mode 100644 index 817b5f2..0000000 --- a/going2git/message.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import ( - "fmt" - - git "go.wit.com/lib/libgit2" - "go.wit.com/log" -) - -func main() { - var input git.Trailer - - input.Key = "Co-authored-by" - input.Value = "Alice " - /* - git2go.Trailer - git2go.Trailer{Key: "Signed-off-by", Value: "Bob "}} - */ - - fmt.Printf("%s", input) - actual, err := git.MessageTrailers(input.Key) - log.Info("actual", actual, err) -} From 98b0d445bc513c5439421d1ec0d32874fc1350f9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 5 Jan 2025 18:40:31 -0600 Subject: [PATCH 4/4] allow setting 'master' and 'devel' branches --- forgeConfig/Makefile | 4 +- forgeConfig/argv.go | 3 ++ forgeConfig/main.go | 111 ++++++++++++++----------------------------- 3 files changed, 40 insertions(+), 78 deletions(-) diff --git a/forgeConfig/Makefile b/forgeConfig/Makefile index 89ab023..b918330 100644 --- a/forgeConfig/Makefile +++ b/forgeConfig/Makefile @@ -1,7 +1,7 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d) -build: +build: goimports GO111MODULE=off go build \ -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" @@ -10,7 +10,7 @@ test2: FORGE_HOME=/tmp/forge ./forgeConfig FORGE_HOME=/tmp/forge ./forgeConfig --list -install: +install: goimports GO111MODULE=off go install \ -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" diff --git a/forgeConfig/argv.go b/forgeConfig/argv.go index 635989f..a39509f 100644 --- a/forgeConfig/argv.go +++ b/forgeConfig/argv.go @@ -21,6 +21,9 @@ type args struct { Favorite bool `arg:"--favorite" default:"false" help:"forge will always go-clone or git clone this"` Private bool `arg:"--private" default:"false" help:"repo can not be published"` Interesting bool `arg:"--interesting" default:"false" help:"something you decided was cool"` + Master string `arg:"--master" help:"the git 'master' or 'main' branch name"` + Devel string `arg:"--devel" help:"the git devel branch name"` + User string `arg:"--user" help:"the git user branch name"` } func (a args) Description() string { diff --git a/forgeConfig/main.go b/forgeConfig/main.go index 36b0b71..d6fabba 100644 --- a/forgeConfig/main.go +++ b/forgeConfig/main.go @@ -11,26 +11,29 @@ import ( var VERSION string func main() { - f := forgepb.Init() + var f *forgepb.Forge + f = forgepb.Init() if argv.List { f.ConfigPrintTable() - loop := f.Config.SortByGoPath() // get the list of forge configs - for loop.Scan() { - r := loop.Next() - log.Info("repo:", r.GoPath) - } + /* + loop := f.Config.SortByGoPath() // get the list of forge configs + for loop.Scan() { + r := loop.Next() + log.Info("repo:", r.GoPath) + } + */ os.Exit(0) } // try to delete, then save config and exit if argv.Delete { - if f.Config.DeleteByGoPath(argv.GoPath) { - log.Info("deleted", argv.GoPath, "did not exist. did nothing") + if deleteGoPath(f, argv.GoPath) { + log.Info("deleted", argv.GoPath, "ok") + f.ConfigSave() os.Exit(0) } - log.Info("deleted", argv.GoPath, "ok") - f.ConfigSave() + log.Info("deleted", argv.GoPath, "did not exist. did nothing") os.Exit(0) } @@ -48,14 +51,18 @@ func main() { // try to add, then save config and exit if argv.Add { log.Info("going to add a new repo", argv.GoPath) + deleteGoPath(f, argv.GoPath) new1 := forgepb.ForgeConfig{ - GoPath: argv.GoPath, - Writable: argv.Writable, - ReadOnly: argv.ReadOnly, - Private: argv.Private, - Directory: argv.Directory, - Favorite: argv.Favorite, - Interesting: argv.Interesting, + GoPath: argv.GoPath, + Writable: argv.Writable, + ReadOnly: argv.ReadOnly, + Private: argv.Private, + Directory: argv.Directory, + Favorite: argv.Favorite, + Interesting: argv.Interesting, + MasterBranchName: argv.Master, + DevelBranchName: argv.Devel, + UserBranchName: argv.User, } if f.Config.Append(&new1) { @@ -68,66 +75,18 @@ func main() { os.Exit(0) } - // testMemoryCorruption(f) - f.ConfigSave() } -/* -// this fucks shit up -func testMemoryCorruption(all *forgepb.Repos) *forgepb.Repos { - new1 := new(forgepb.Repo) - new1.Name = "bash1" - new1.Version = "5.2.21" - if all.Append(new1) { - log.Info("added", new1.Name, "ok") - } else { - log.Info("added", new1.Name, "failed") +func deleteGoPath(f *forgepb.Forge, gopath string) bool { + var deleted bool = false + for { + if f.Config.DeleteByGoPath(gopath) { + log.Info("deleted ok", gopath) + deleted = true + } else { + log.Info("did not delete", gopath) + break + } } - - new1 = new(forgepb.Repo) - new1.Name = "zookeeper1" - new1.Debname = "zookeeper-go" - if all.Append(new1) { - log.Info("added", new1.Name, "ok") - } else { - log.Info("added", new1.Name, "failed") - } - - new1 = new(forgepb.Repo) - new1.Name = "wit-package" - new1.Private = true - if all.Append(new1) { - log.Info("added", new1.Name, "ok") - } else { - log.Info("added", new1.Name, "failed") - } - - new1 = new(forgepb.Repo) - new1.Name = "networkQuality" - new1.Debname = "networkquality" - new1.Readonly = true - if all.Append(new1) { - log.Info("added", new1.Name, "ok") - } else { - log.Info("added", new1.Name, "failed") - } - - new2 := new(forgepb.Repo) - new2.Name = "go-clone" - new2.Version = "0.6.8" // good version of the macos - if all.Append(new2) { - log.Info("added", new2.Name, "ok") - } else { - log.Info("added", new2.Name, "failed") - } - - if all.Append(new2) { - log.Info("added", new2.Name, "ok (this is bad)") - } else { - log.Info("added", new2.Name, "failed (but ok)") - } - - fmt.Println("packages are:", len(all.Repos)) - return all + return deleted } -*/