|
1 | 1 | package clu2adv |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 |
|
| 7 | + "github.com/fsnotify/fsnotify" |
6 | 8 | "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/convert" |
7 | 9 | "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/file" |
8 | 10 | "github.com/spf13/afero" |
9 | 11 | ) |
10 | 12 |
|
11 | 13 | type opts struct { |
12 | | - fs afero.Fs |
13 | | - file string |
14 | | - output string |
15 | | - overwriteOutput bool |
| 14 | + fs afero.Fs |
| 15 | + file string |
| 16 | + output string |
| 17 | + replaceOutput bool |
| 18 | + watch bool |
16 | 19 | } |
17 | 20 |
|
18 | 21 | func (o *opts) PreRun() error { |
19 | 22 | if err := file.MustExist(o.fs, o.file); err != nil { |
20 | 23 | return err |
21 | 24 | } |
22 | | - if !o.overwriteOutput { |
| 25 | + if !o.replaceOutput { |
23 | 26 | return file.MustNotExist(o.fs, o.output) |
24 | 27 | } |
25 | 28 | return nil |
26 | 29 | } |
27 | 30 |
|
28 | 31 | func (o *opts) Run() error { |
| 32 | + if err := o.generateFile(false); err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + if o.watch { |
| 36 | + return o.watchFile() |
| 37 | + } |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +func (o *opts) generateFile(allowParseErrors bool) error { |
29 | 42 | inConfig, err := afero.ReadFile(o.fs, o.file) |
30 | 43 | if err != nil { |
31 | 44 | return fmt.Errorf("failed to read file %s: %w", o.file, err) |
32 | 45 | } |
33 | 46 | outConfig, err := convert.ClusterToAdvancedCluster(inConfig) |
34 | 47 | if err != nil { |
35 | | - return err |
| 48 | + if allowParseErrors { |
| 49 | + outConfig = []byte("# CONVERT ERROR: " + err.Error() + "\n\n") |
| 50 | + outConfig = append(outConfig, inConfig...) |
| 51 | + } else { |
| 52 | + return err |
| 53 | + } |
36 | 54 | } |
37 | 55 | if err := afero.WriteFile(o.fs, o.output, outConfig, 0o600); err != nil { |
38 | 56 | return fmt.Errorf("failed to write file %s: %w", o.output, err) |
39 | 57 | } |
40 | 58 | return nil |
41 | 59 | } |
| 60 | + |
| 61 | +func (o *opts) watchFile() error { |
| 62 | + watcher, err := fsnotify.NewWatcher() |
| 63 | + if err != nil { |
| 64 | + return nil |
| 65 | + } |
| 66 | + defer watcher.Close() |
| 67 | + if err := watcher.Add(o.file); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + for { |
| 71 | + if err := o.waitForFileEvent(watcher); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (o *opts) waitForFileEvent(watcher *fsnotify.Watcher) error { |
| 78 | + watcherError := errors.New("watcher has been closed") |
| 79 | + select { |
| 80 | + case event, ok := <-watcher.Events: |
| 81 | + if !ok { |
| 82 | + return watcherError |
| 83 | + } |
| 84 | + if event.Has(fsnotify.Write) { |
| 85 | + if err := o.generateFile(true); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | + case err, ok := <-watcher.Errors: |
| 90 | + if !ok { |
| 91 | + return watcherError |
| 92 | + } |
| 93 | + return err |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
0 commit comments