forked from maxjahn/staci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (107 loc) · 2.84 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/apoorvam/goterminal"
"github.com/common-nighthawk/go-figure"
"github.com/oracle/oci-go-sdk/common"
"github.com/oracle/oci-go-sdk/objectstorage"
)
func main() {
sourceDir := flag.String("source", "", "source directoy")
targetBucket := flag.String("target", "", "target bucket")
flag.Parse()
myFigure := figure.NewFigure("staci", "shadow", true)
myFigure.Print()
fmt.Println("\nOCI STAtic Content Importer")
if *sourceDir == "" || *targetBucket == "" {
log.Fatalln("Required flags: -source -target")
}
osClient, err := objectstorage.NewObjectStorageClientWithConfigurationProvider(common.DefaultConfigProvider())
if err != nil {
log.Println("Error: ", err)
os.Exit(1)
}
nsRequest := objectstorage.GetNamespaceRequest{}
nsResp, err := osClient.GetNamespace(context.Background(), nsRequest)
if err != nil {
log.Println("Error: ", err)
os.Exit(1)
}
osNamespace := *nsResp.Value
bucketResp, err := osClient.GetBucket(context.Background(), objectstorage.GetBucketRequest{NamespaceName: &osNamespace, BucketName: targetBucket})
if err != nil {
log.Println("Error: ", err)
os.Exit(1)
}
bucketOCID := *bucketResp.Id
fmt.Printf("Uploading to bucket %v with OCID %v\b", *targetBucket, bucketOCID)
writer := goterminal.New(os.Stdout)
bar := "."
err = filepath.Walk(*sourceDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("Error listing source directory %q: %v\n", path, err)
os.Exit(1)
}
if info.IsDir() {
return nil
}
rel, _ := filepath.Rel(*sourceDir, path)
rel = filepath.ToSlash(rel)
file, err := os.Open(filepath.Clean(path))
if err != nil {
log.Fatal(err)
}
defer file.Close()
header := make([]byte, 512)
_, err = file.Read(header)
contentType := http.DetectContentType(header)
// hack for css
if strings.HasSuffix(strings.ToLower(path), ".css") {
contentType = "text/css"
}
// hack for css
if strings.HasSuffix(strings.ToLower(path), ".js") {
contentType = "application/javascript"
}
file.Seek(0, 0)
reader := bufio.NewReader(file)
buf := &bytes.Buffer{}
objLen, err := io.Copy(buf, reader)
if err != nil {
log.Println(err)
os.Exit(1)
}
request := objectstorage.PutObjectRequest{
NamespaceName: nsResp.Value,
BucketName: targetBucket,
ObjectName: &rel,
ContentLength: &objLen,
ContentType: &contentType,
PutObjectBody: ioutil.NopCloser(buf),
OpcMeta: nil,
}
bar += "."
fmt.Fprintf(writer, "Upload %q\n%v\n", rel, bar)
writer.Print()
_, err = osClient.PutObject(context.Background(), request)
if err != nil {
log.Println(err)
os.Exit(1)
}
writer.Clear()
return nil
})
writer.Reset()
fmt.Println("\nFinished file upload.")
}