-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfgreader.go
89 lines (75 loc) · 2.4 KB
/
cfgreader.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
package veracity
import (
"fmt"
"strings"
"github.com/datatrails/go-datatrails-common/azblob"
"github.com/urfave/cli/v2"
)
const (
AzureBlobURLFmt = "https://%s.blob.core.windows.net"
AzuriteStorageAccount = "devstoreaccount1"
DefaultContainer = "merklelogs"
)
// cfgReader establishes the blob read only data accessor
// only azure blob storage is supported. Both emulated and production.
func cfgReader(cmd *CmdCtx, cCtx *cli.Context, forceProdUrl bool) (azblob.Reader, error) {
var err error
var reader azblob.Reader
if cmd.log == nil {
if err = cfgLogging(cmd, cCtx); err != nil {
return nil, err
}
}
// We prefer loading this from the command line argument, but if upstream code requests we default
// to the production URL we inject that here.
url := cCtx.String("data-url")
if forceProdUrl {
url = DefaultRemoteMassifURL
}
// These values are relevant for direct connection to Azure blob store (or emulator), but are
// harmlessly irrelevant for standard remote connections that connect via public proxy. Potential
// to simplify this function in future.
container := cCtx.String("container")
account := cCtx.String("account")
envAuth := cCtx.Bool("envauth")
if account == "" && url == "" {
account = AzuriteStorageAccount
cmd.log.Infof("defaulting to the emulator account %s", account)
}
if container == "" {
container = DefaultContainer
cmd.log.Infof("defaulting to the standard container %s", container)
}
if account == AzuriteStorageAccount {
cmd.log.Infof("using the emulator and authorizing with the well known private key (for production no authorization is required)")
// reader, err := azblob.NewAzurite(url, container)
devCfg := azblob.NewDevConfigFromEnv()
cmd.readerURL = devCfg.URL
reader, err = azblob.NewDev(devCfg, container)
if err != nil {
return nil, err
}
return reader, nil
}
if url == "" {
url = fmt.Sprintf(AzureBlobURLFmt, account)
}
if !strings.HasSuffix(url, "/") {
url = url + "/"
}
if envAuth {
devCfg := azblob.NewDevConfigFromEnv()
cmd.readerURL = devCfg.URL
reader, err = azblob.NewDev(devCfg, container)
if err != nil {
return nil, err
}
return reader, nil
}
cmd.readerURL = url
reader, err = azblob.NewReaderNoAuth(url, azblob.WithContainer(container), azblob.WithAccountName(account))
if err != nil {
return nil, fmt.Errorf("failed to connect to blob store: %v", err)
}
return reader, nil
}