Merge branch 'jcarr' of gitea.wit.com:jcarr/wit-utils into jcarr

This commit is contained in:
Jeff Carr 2025-01-06 03:05:47 -06:00
commit 35bec795e7
9 changed files with 292 additions and 101 deletions

3
.gitignore vendored
View File

@ -15,3 +15,6 @@ go-clone-test/goclonetest
go-clone-test/goclonetesttest
going2git/going2git
blobby/blobby
cf-r2/cf-r2
cf-r2/download
cf-r2/upload

99
cf-r2-awsapi/main.go Normal file
View File

@ -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 = "<your-account-id>" // Replace with your Cloudflare R2 Account ID
r2AccessKey = "<your-access-key-id>" // Replace with your Access Key ID
r2SecretKey = "<your-secret-access-key>" // Replace with your Secret Access Key
r2BucketName = "example-bucket" // Replace with your bucket name
r2Endpoint = "https://<account-id>.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
}

66
cf-r2/download.go Normal file
View File

@ -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 = "<account-id>.r2.cloudflarestorage.com" // Replace with your R2 endpoint
r2AccessKey = "<your-access-key>" // Replace with your Access Key
r2SecretKey = "<your-secret-key>" // 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
}

26
cf-r2/main.go Normal file
View File

@ -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
}

58
cf-r2/upload.go Normal file
View File

@ -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)
}

View File

@ -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}"

View File

@ -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 {

View File

@ -11,28 +11,31 @@ 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)
}
*/
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")
os.Exit(0)
}
if deleteGoPath(f, argv.GoPath) {
log.Info("deleted", argv.GoPath, "ok")
f.ConfigSave()
os.Exit(0)
}
log.Info("deleted", argv.GoPath, "did not exist. did nothing")
os.Exit(0)
}
// try to update, then save config and exit
if argv.Update {
@ -48,6 +51,7 @@ 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,
@ -56,6 +60,9 @@ func main() {
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")
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("added", new1.Name, "failed")
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
}
*/

View File

@ -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 <alice@example.com>"
/*
git2go.Trailer
git2go.Trailer{Key: "Signed-off-by", Value: "Bob <bob@example.com>"}}
*/
fmt.Printf("%s", input)
actual, err := git.MessageTrailers(input.Key)
log.Info("actual", actual, err)
}