generated from ZEISS/template-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (78 loc) · 1.88 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"io/fs"
"mime"
"os"
"path/filepath"
"template/internal/cfg"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/sethvargo/go-githubactions"
"github.com/zeiss/pkg/cast"
"github.com/zeiss/pkg/utilx"
)
// GetContentType ...
func GetContentType(file string) string {
ext := filepath.Ext(file)
switch ext {
case ".htm", ".html":
return "text/html"
case ".css":
return "text/css"
case ".js":
return "application/javascript"
default:
return mime.TypeByExtension(ext)
}
}
// nolint:gocyclo
func main() {
ctx := context.Background()
action := githubactions.New()
cfg, err := cfg.NewFromInput(action)
if err != nil {
githubactions.Fatalf("error: %s", err)
}
credentials, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
githubactions.Fatalf("error: %s", err)
}
client, err := azblob.NewClient(cfg.AccountURL, credentials, nil)
if err != nil {
githubactions.Fatalf("error: %s", err)
}
err = filepath.WalkDir(cfg.Path, func(path string, d fs.DirEntry, errr error) error {
root := filepath.Clean(cfg.Path)
path = filepath.Clean(path)
if utilx.Or(root == path, d.IsDir()) {
return nil
}
file, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return err
}
defer file.Close()
p, err := filepath.Rel(root, path)
if err != nil {
return err
}
ct := GetContentType(path)
opts := &azblob.UploadFileOptions{
HTTPHeaders: &blob.HTTPHeaders{
BlobContentType: cast.Ptr(ct),
},
}
githubactions.Infof("uploading %s (%s)", p, ct)
_, err = client.UploadFile(ctx, cfg.ContainerName, p, file, opts)
if err != nil {
return err
}
return nil
})
if err != nil {
githubactions.Fatalf("error: %s", err)
}
githubactions.Infof("upload successful")
}