-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (65 loc) · 1.61 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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"github.com/mindscratch/probable-waffle/config"
"github.com/mindscratch/probable-waffle/downloaders"
)
var (
appConfig config.Config
)
func getMetadataValue(metadata_file, key string) (string, error) {
r, re_err := regexp.Compile(fmt.Sprintf("%s: (.*)", key))
if re_err != nil {
return "", fmt.Errorf("Unable to compile regex: %s\n", re_err)
}
data, err := ioutil.ReadFile(metadata_file)
if err != nil {
return "", err
}
matches := r.FindStringSubmatch(string(data))
if len(matches) != 2 {
return "", fmt.Errorf("Unable to find value where key=%s", key)
}
return matches[1], nil
}
func main() {
flag.Parse()
appConfig, err := config.New()
if err != nil {
fmt.Printf("Configuration error: %s\n", err.Error())
os.Exit(1)
}
metadata_file := "sample.data"
key := "data_uri"
retries := 3
retry_delay := "5s"
fmt.Println("CONFIG:", metadata_file, key, appConfig.Output, retries, retry_delay)
downloadUri, err := getMetadataValue(metadata_file, key)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
fmt.Printf("config: %#v\n", appConfig)
client, err := downloaders.New(appConfig, downloadUri)
fmt.Printf("client: %s\n", client)
fmt.Printf("err: %#v\n", err)
var outputWriter io.Writer = os.Stdout
if appConfig.Output != "" {
//TODO close file "f"
f, err := os.Create(appConfig.Output)
if err != nil {
fmt.Printf("Unable open %s for writing: %s\n", appConfig.Output, err.Error())
os.Exit(1)
}
outputWriter = f
}
err = client.Get(outputWriter)
if err != nil {
fmt.Printf("get err: %s\n", err.Error())
}
}