-
Notifications
You must be signed in to change notification settings - Fork 34
/
files.go
96 lines (84 loc) · 2.76 KB
/
files.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
package main
import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"strings"
"github.com/Azure/custom-script-extension-linux/blobutil"
"github.com/Azure/custom-script-extension-linux/download"
"github.com/Azure/custom-script-extension-linux/preprocess"
"github.com/pkg/errors"
)
// downloadAndProcessURL downloads using the specified downloader and saves it to the
// specified existing directory, which must be the path to the saved file. Then
// it post-processes file based on heuristics.
func downloadAndProcessURL(url, downloadDir, storageAccountName, storageAccountKey string) error {
fn, err := urlToFileName(url)
if err != nil {
return err
}
dl, err := getDownloader(url, storageAccountName, storageAccountKey)
if err != nil {
return err
}
fp := filepath.Join(downloadDir, fn)
const mode = 0500 // we assume users download scripts to execute
if _, err := download.SaveTo(dl, fp, mode); err != nil {
return err
}
err = postProcessFile(fp)
return errors.Wrapf(err, "failed to post-process '%s'", fn)
}
// getDownloader returns a downloader for the given URL based on whether the
// storage credentials are empty or not.
func getDownloader(fileURL string, storageAccountName, storageAccountKey string) (
download.Downloader, error) {
if storageAccountName == "" || storageAccountKey == "" {
return download.NewURLDownload(fileURL), nil
}
blob, err := blobutil.ParseBlobURL(fileURL)
if err != nil {
return nil, err
}
return download.NewBlobDownload(
storageAccountName, storageAccountKey,
blob), nil
}
// urlToFileName parses given URL and returns the section after the last slash
// character of the path segment to be used as a file name. If a value is not
// found, an error is returned.
func urlToFileName(fileURL string) (string, error) {
u, err := url.Parse(fileURL)
if err != nil {
return "", errors.Wrapf(err, "unable to parse URL: %q", fileURL)
}
s := strings.Split(u.Path, "/")
if len(s) > 0 {
fn := s[len(s)-1]
if fn != "" {
return fn, nil
}
}
return "", fmt.Errorf("cannot extract file name from URL: %q", fileURL)
}
// postProcessFile determines if path is a script file based on heuristics
// and makes in-place changes to the file with some post-processing such as BOM
// and DOS-line endings fixes to make the script POSIX-friendly.
func postProcessFile(path string) error {
ok, err := preprocess.IsTextFile(path)
if err != nil {
return errors.Wrapf(err, "error determining if script file")
}
if !ok {
return nil
}
b, err := ioutil.ReadFile(path) // read the file into memory for processing
if err != nil {
return errors.Wrapf(err, "error reading file")
}
b = preprocess.RemoveBOM(b)
b = preprocess.Dos2Unix(b)
err = ioutil.WriteFile(path, b, 0) // mode is ignored
return errors.Wrapf(err, "failed to write to file")
}