forked from PacktPublishing/Go-for-DevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
47 lines (38 loc) · 1.04 KB
/
helpers.go
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
package helpers
import (
"context"
"log"
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
// const (
// DefaultPollingFreq = 10 * time.Second
// )
type ClientBuilderFunc[T any] func(string, azcore.TokenCredential, *arm.ClientOptions) (*T, error)
func MustGetenv(key string) string {
val := os.Getenv(key)
if val == "" {
log.Fatalf("please add your %s to the .env file", key)
}
return val
}
func BuildClient[T any](subID string, cred *azidentity.DefaultAzureCredential, builderFunc ClientBuilderFunc[T]) *T {
return HandleErrWithResult(builderFunc(subID, cred, nil))
}
func HandleErrPoller[T any](ctx context.Context, poller *runtime.Poller[T]) T {
res, err := poller.PollUntilDone(ctx, nil)
HandleErr(err)
return res
}
func HandleErrWithResult[T any](result T, err error) T {
HandleErr(err)
return result
}
func HandleErr(err error) {
if err != nil {
panic(err)
}
}