2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-02 09:58:25 +00:00
restic/vendor/github.com/kurin/blazer/internal/bin/cleanup/cleanup.go
Alexander Neumann 61cb1cc6f8 Update vendored dependencies
This includes github.com/kurin/blazer 0.2.0, which resolves #1291
2017-10-01 10:13:39 +02:00

64 lines
1.0 KiB
Go

package main
import (
"context"
"fmt"
"io"
"os"
"sync"
"github.com/kurin/blazer/b2"
)
const (
apiID = "B2_ACCOUNT_ID"
apiKey = "B2_SECRET_KEY"
)
func main() {
id := os.Getenv(apiID)
key := os.Getenv(apiKey)
ctx := context.Background()
client, err := b2.NewClient(ctx, id, key)
if err != nil {
fmt.Println(err)
return
}
var wg sync.WaitGroup
for _, name := range []string{"consistobucket", "base-tests"} {
wg.Add(1)
go func(name string) {
defer wg.Done()
if err := killBucket(ctx, client, id, name); err != nil {
fmt.Println(err)
}
}(name)
}
wg.Wait()
}
func killBucket(ctx context.Context, client *b2.Client, id, name string) error {
bucket, err := client.NewBucket(ctx, id+"-"+name, nil)
if b2.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
defer bucket.Delete(ctx)
cur := &b2.Cursor{}
for {
os, c, err := bucket.ListObjects(ctx, 1000, cur)
if err != nil && err != io.EOF {
return err
}
for _, o := range os {
o.Delete(ctx)
}
if err == io.EOF {
return nil
}
cur = c
}
}