|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/fsnotify/fsnotify" |
| 8 | + "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/file" |
| 9 | + "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/flag" |
| 10 | + "github.com/spf13/afero" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +type ConvertFn func(config []byte) ([]byte, error) |
| 15 | + |
| 16 | +// BaseOpts contains common functionality for CLI commands that convert files. |
| 17 | +type BaseOpts struct { |
| 18 | + Fs afero.Fs |
| 19 | + Convert ConvertFn |
| 20 | + File string |
| 21 | + Output string |
| 22 | + ReplaceOutput bool |
| 23 | + Watch bool |
| 24 | +} |
| 25 | + |
| 26 | +// RunE is the entry point for the command. |
| 27 | +func (o *BaseOpts) RunE(cmd *cobra.Command, args []string) error { |
| 28 | + if err := o.preRun(); err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + return o.run() |
| 32 | +} |
| 33 | + |
| 34 | +// preRun validates the input and output files before running the command. |
| 35 | +func (o *BaseOpts) preRun() error { |
| 36 | + if err := file.MustExist(o.Fs, o.File); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + if !o.ReplaceOutput { |
| 40 | + return file.MustNotExist(o.Fs, o.Output) |
| 41 | + } |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// run executes the conversion and optionally watches for file changes. |
| 46 | +func (o *BaseOpts) run() error { |
| 47 | + if err := o.generateFile(false); err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + if o.Watch { |
| 51 | + return o.watchFile() |
| 52 | + } |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// generateFile reads the input file, converts it, and writes the output. |
| 57 | +func (o *BaseOpts) generateFile(allowParseErrors bool) error { |
| 58 | + inConfig, err := afero.ReadFile(o.Fs, o.File) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to read file %s: %w", o.File, err) |
| 61 | + } |
| 62 | + |
| 63 | + outConfig, err := o.Convert(inConfig) |
| 64 | + if err != nil { |
| 65 | + if allowParseErrors { |
| 66 | + outConfig = []byte("# CONVERT ERROR: " + err.Error() + "\n\n") |
| 67 | + outConfig = append(outConfig, inConfig...) |
| 68 | + } else { |
| 69 | + return err |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if err := afero.WriteFile(o.Fs, o.Output, outConfig, 0o600); err != nil { |
| 74 | + return fmt.Errorf("failed to write file %s: %w", o.Output, err) |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// watchFile watches the input file for changes and regenerates the output. |
| 80 | +func (o *BaseOpts) watchFile() error { |
| 81 | + watcher, err := fsnotify.NewWatcher() |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + defer watcher.Close() |
| 86 | + |
| 87 | + if err := watcher.Add(o.File); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + for { |
| 92 | + if err := o.waitForFileEvent(watcher); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// waitForFileEvent waits for file system events and regenerates the output file. |
| 99 | +func (o *BaseOpts) waitForFileEvent(watcher *fsnotify.Watcher) error { |
| 100 | + watcherError := errors.New("watcher has been closed") |
| 101 | + select { |
| 102 | + case event, ok := <-watcher.Events: |
| 103 | + if !ok { |
| 104 | + return watcherError |
| 105 | + } |
| 106 | + if event.Has(fsnotify.Write) { |
| 107 | + if err := o.generateFile(true); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + } |
| 111 | + case err, ok := <-watcher.Errors: |
| 112 | + if !ok { |
| 113 | + return watcherError |
| 114 | + } |
| 115 | + return err |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +// SetupCommonFlags sets up the common flags used by all commands. |
| 121 | +func SetupCommonFlags(cmd *cobra.Command, opts *BaseOpts) { |
| 122 | + cmd.Flags().StringVarP(&opts.File, flag.File, flag.FileShort, "", "input file") |
| 123 | + _ = cmd.MarkFlagRequired(flag.File) |
| 124 | + cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", "output file") |
| 125 | + _ = cmd.MarkFlagRequired(flag.Output) |
| 126 | + cmd.Flags().BoolVarP(&opts.ReplaceOutput, flag.ReplaceOutput, flag.ReplaceOutputShort, false, |
| 127 | + "replace output file if exists") |
| 128 | + cmd.Flags().BoolVarP(&opts.Watch, flag.Watch, flag.WatchShort, false, |
| 129 | + "keeps the plugin running and watches the input file for changes") |
| 130 | +} |
0 commit comments