nats-upload: support just cleaning
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,3 +27,4 @@ go.work.sum
|
|||||||
|
|
||||||
./bin
|
./bin
|
||||||
bin
|
bin
|
||||||
|
nats-upload
|
||||||
|
|||||||
91
main.go
91
main.go
@@ -28,11 +28,12 @@ func main() {
|
|||||||
skipNotify = flag.Bool("skip-notify", getEnvBool("INPUT_SKIP_NOTIFY", false), "Skip publishing update notification")
|
skipNotify = flag.Bool("skip-notify", getEnvBool("INPUT_SKIP_NOTIFY", false), "Skip publishing update notification")
|
||||||
cleanup = flag.Int("cleanup", getEnvInt("INPUT_CLEANUP", 0), "Keep only N most recent versions (0 disables cleanup)")
|
cleanup = flag.Int("cleanup", getEnvInt("INPUT_CLEANUP", 0), "Keep only N most recent versions (0 disables cleanup)")
|
||||||
cleanupAll = flag.Bool("cleanup-all", getEnvBool("INPUT_CLEANUP_ALL", false), "Cleanup all binaries, not just current one")
|
cleanupAll = flag.Bool("cleanup-all", getEnvBool("INPUT_CLEANUP_ALL", false), "Cleanup all binaries, not just current one")
|
||||||
|
justClean = flag.Bool("just-clean", getEnvBool("INPUT_JUST_CLEAN", false), "Dont upload, just cleanup old versions")
|
||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *directory == "" {
|
if *directory == "" && *cleanup == 0 && *justClean == false {
|
||||||
log.Fatal("Directory path is required")
|
log.Fatal("Directory path is required or cleanup must be enabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -60,56 +61,58 @@ func main() {
|
|||||||
log.Printf("Created object store: %s", *bucketName)
|
log.Printf("Created object store: %s", *bucketName)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = filepath.Walk(*directory, func(path string, info os.FileInfo, err error) error {
|
if *directory != "" && *justClean == false {
|
||||||
if err != nil {
|
err = filepath.Walk(*directory, func(path string, info os.FileInfo, err error) error {
|
||||||
return err
|
if err != nil {
|
||||||
}
|
return err
|
||||||
|
|
||||||
if info.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to read %s: %w", path, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
relPath, err := filepath.Rel(*directory, path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get relative path: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
objectKey := relPath
|
|
||||||
if *prefix != "" {
|
|
||||||
objectKey = strings.TrimPrefix(relPath, *prefix)
|
|
||||||
}
|
|
||||||
|
|
||||||
objectKey = filepath.ToSlash(objectKey)
|
|
||||||
|
|
||||||
if *binaryName == "" {
|
|
||||||
parts := strings.Split(objectKey, "/")
|
|
||||||
if len(parts) >= 2 {
|
|
||||||
*binaryName = parts[0]
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Uploading %s as %s (%d bytes)", path, objectKey, len(data))
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read %s: %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath, err := filepath.Rel(*directory, path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get relative path: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
objectKey := relPath
|
||||||
|
if *prefix != "" {
|
||||||
|
objectKey = strings.TrimPrefix(relPath, *prefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
objectKey = filepath.ToSlash(objectKey)
|
||||||
|
|
||||||
|
if *binaryName == "" {
|
||||||
|
parts := strings.Split(objectKey, "/")
|
||||||
|
if len(parts) >= 2 {
|
||||||
|
*binaryName = parts[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Uploading %s as %s (%d bytes)", path, objectKey, len(data))
|
||||||
|
|
||||||
|
_, err = store.PutBytes(ctx, objectKey, data)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to upload %s: %w", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("✓ Uploaded %s", objectKey)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
_, err = store.PutBytes(ctx, objectKey, data)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to upload %s: %w", path, err)
|
log.Fatalf("Failed to upload files: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("✓ Uploaded %s", objectKey)
|
log.Printf("Successfully uploaded all files from %s to NATS object store '%s'", *directory, *bucketName)
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to upload files: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Successfully uploaded all files from %s to NATS object store '%s'", *directory, *bucketName)
|
|
||||||
|
|
||||||
if *cleanup > 0 {
|
if *cleanup > 0 {
|
||||||
log.Printf("Cleaning up old versions, keeping %d most recent", *cleanup)
|
log.Printf("Cleaning up old versions, keeping %d most recent", *cleanup)
|
||||||
err = cleanupOldVersions(ctx, store, *binaryName, *cleanup, *cleanupAll)
|
err = cleanupOldVersions(ctx, store, *binaryName, *cleanup, *cleanupAll)
|
||||||
|
|||||||
Reference in New Issue
Block a user