|
1 | 1 | package secrethub |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "fmt" |
6 | | - "io" |
7 | 5 | "io/ioutil" |
| 6 | + "os" |
8 | 7 | "regexp" |
9 | 8 | "strings" |
10 | 9 |
|
@@ -36,23 +35,28 @@ func (cmd *MigrateConfigTemplatesCommand) Run() error { |
36 | 35 | return ErrReadFile(filepath, err) |
37 | 36 | } |
38 | 37 |
|
39 | | - replaceCount, err := migrateTemplateTags(bytes.NewBuffer(inFileContents), filepath, refMapping, "{{ %s }}") |
| 38 | + output, replaceCount, err := migrateTemplateTags(string(inFileContents), refMapping, "{{ %s }}") |
40 | 39 | if err != nil { |
41 | 40 | return err |
42 | 41 | } |
43 | 42 |
|
44 | | - fmt.Fprintf(cmd.io.Output(), "Updated %s with %d op:// references\n", filepath, len(replaceCount)) |
| 43 | + inFileInfo, err := os.Stat(filepath) |
| 44 | + if err != nil { |
| 45 | + return ErrReadFile(filepath, err) |
| 46 | + } |
| 47 | + |
| 48 | + err = ioutil.WriteFile(filepath, []byte(output), inFileInfo.Mode()) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + fmt.Fprintf(cmd.io.Output(), "Updated %s with %d op:// references\n", filepath, replaceCount) |
45 | 54 | } |
46 | 55 |
|
47 | 56 | return nil |
48 | 57 | } |
49 | 58 |
|
50 | | -func migrateTemplateTags(inFile io.Reader, outFile string, mapping referenceMapping, formatString string) ([]string, error) { |
51 | | - raw, err := ioutil.ReadAll(inFile) |
52 | | - if err != nil { |
53 | | - return nil, ErrReadFile(inFile, err) |
54 | | - } |
55 | | - |
| 59 | +func migrateTemplateTags(inFileContents string, mapping referenceMapping, formatString string) (string, int, error) { |
56 | 60 | var hits, misses []string |
57 | 61 | output := regexpSecretTemplateTags.ReplaceAllStringFunc(string(raw), func(templateTag string) string { |
58 | 62 | path := regexpSecretTemplateTags.FindStringSubmatch(templateTag)[1] |
@@ -81,15 +85,10 @@ func migrateTemplateTags(inFile io.Reader, outFile string, mapping referenceMapp |
81 | 85 | errMsg += "\nDid you specify every possible value for your template variables? E.g. --var varname1=a,b,c,d --var varname2=x,y,z" |
82 | 86 | } |
83 | 87 |
|
84 | | - return nil, fmt.Errorf(errMsg) |
85 | | - } |
86 | | - |
87 | | - err = ioutil.WriteFile(outFile, []byte(output), 0666) |
88 | | - if err != nil { |
89 | | - return nil, err |
| 88 | + return "", 0, fmt.Errorf(errMsg) |
90 | 89 | } |
91 | 90 |
|
92 | | - return hits, nil |
| 91 | + return output, len(hits), nil |
93 | 92 | } |
94 | 93 |
|
95 | 94 | type MigrateConfigTemplatesCommand struct { |
|
0 commit comments