File: copy_wait.go

package info (click to toggle)
golang-github-tombuildsstuff-giovanni 0.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,908 kB
  • sloc: makefile: 3
file content (55 lines) | stat: -rw-r--r-- 1,292 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package files

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/Azure/go-autorest/autorest"
)

type CopyAndWaitResult struct {
	autorest.Response

	CopyID string
}

const DefaultCopyPollDuration = 15 * time.Second

// CopyAndWait is a convenience method which doesn't exist in the API, which copies the file and then waits for the copy to complete
func (client Client) CopyAndWait(ctx context.Context, accountName, shareName, path, fileName string, input CopyInput, pollDuration time.Duration) (result CopyResult, err error) {
	copy, e := client.Copy(ctx, accountName, shareName, path, fileName, input)
	if err != nil {
		result.Response = copy.Response
		err = fmt.Errorf("Error copying: %s", e)
		return
	}

	result.CopyID = copy.CopyID

	// since the API doesn't return a LRO, this is a hack which also polls every 10s, but should be sufficient
	for true {
		props, e := client.GetProperties(ctx, accountName, shareName, path, fileName)
		if e != nil {
			result.Response = copy.Response
			err = fmt.Errorf("Error waiting for copy: %s", e)
			return
		}

		switch strings.ToLower(props.CopyStatus) {
		case "pending":
			time.Sleep(pollDuration)
			continue

		case "success":
			return

		default:
			err = fmt.Errorf("Unexpected CopyState %q", e)
			return
		}
	}

	return
}