MCPcopy Create free account
hub / github.com/AdRoll/baker / openFile

Method openFile

input/list.go:89–164  ·  view source on GitHub ↗

Open a file and return the io Reader, the size, the last modification, and the path as URL. If the sizeOnly parameter is true no io Reader will be returned.

(fn string, sizeOnly bool)

Source from the content-addressed store, hash-verified

87// Open a file and return the io Reader, the size, the last modification, and the path as URL.
88// If the sizeOnly parameter is true no io Reader will be returned.
89func (s *List) openFile(fn string, sizeOnly bool) (io.ReadCloser, int64, time.Time, *url.URL, error) {
90 if fn == "-" {
91 return stdin, 0, time.Unix(0, 0), nil, nil
92 }
93
94 u, err := url.Parse(fn)
95 if err != nil {
96 // NOTE: raw paths are parsed with u.Scheme=""
97 s.setFatalErr(err)
98 return nil, 0, time.Unix(0, 0), nil, err
99 }
100
101 switch u.Scheme {
102 case "", "file":
103 path := filepath.Join(u.Host, u.Path) // On Windows Host contains the drive letter
104 fi, err := os.Stat(path)
105 if err != nil {
106 s.setFatalErr(err)
107 return nil, 0, time.Unix(0, 0), u, err
108 }
109 if sizeOnly {
110 return nil, fi.Size(), fi.ModTime(), u, nil
111 }
112 f, err := os.Open(path)
113 if err != nil {
114 s.setFatalErr(err)
115 return nil, fi.Size(), fi.ModTime(), u, err
116 }
117 return f, fi.Size(), fi.ModTime(), u, err
118 case "s3":
119 if sizeOnly {
120 resp, err := s.svc.HeadObject(&s3.HeadObjectInput{
121 Bucket: aws.String(u.Host),
122 Key: aws.String(u.Path),
123 })
124 if err != nil {
125 err := fmt.Errorf("error opening %q: %v", fn, err)
126 s.setFatalErr(err)
127 return nil, 0, time.Unix(0, 0), u, err
128 }
129 return nil, *resp.ContentLength, *resp.LastModified, u, nil
130 } else {
131 resp, err := s.svc.GetObject(&s3.GetObjectInput{
132 Bucket: aws.String(u.Host),
133 Key: aws.String(u.Path),
134 })
135 if err != nil {
136 err := fmt.Errorf("error opening %q: %v", fn, err)
137 s.setFatalErr(err)
138 return nil, 0, time.Unix(0, 0), u, err
139 }
140 return resp.Body, *resp.ContentLength, *resp.LastModified, u, nil
141 }
142 case "http", "https":
143 resp, err := httpGet(fn)
144 if err != nil {
145 s.setFatalErr(err)
146 return nil, 0, time.Unix(0, 0), u, err

Callers 1

NewListFunction · 0.95

Calls 7

setFatalErrMethod · 0.95
httpGetFunction · 0.85
GetObjectMethod · 0.80
ParseMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected