Skip to content

Commit e15fcaf

Browse files
committed
watch
1 parent e8264eb commit e15fcaf

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/mongodb-labs/atlas-cli-plugin-terraform
33
go 1.23.6
44

55
require (
6+
github.com/fsnotify/fsnotify v1.8.0
67
github.com/hashicorp/hcl/v2 v2.23.0
78
github.com/sebdah/goldie/v2 v2.5.5
89
github.com/spf13/afero v1.12.0
@@ -23,6 +24,7 @@ require (
2324
github.com/spf13/pflag v1.0.5 // indirect
2425
golang.org/x/mod v0.22.0 // indirect
2526
golang.org/x/sync v0.10.0 // indirect
27+
golang.org/x/sys v0.29.0 // indirect
2628
golang.org/x/text v0.21.0 // indirect
2729
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
2830
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
66
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
77
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
10+
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
911
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
1012
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
1113
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@@ -49,6 +51,8 @@ golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
4951
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
5052
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
5153
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
54+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
55+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5256
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
5357
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
5458
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=

internal/cli/clu2adv/clu2adv.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ func Builder() *cobra.Command {
2424
cmd.Flags().StringVarP(&o.output, "output", "o", "", "output file")
2525
_ = cmd.MarkFlagRequired("output")
2626
cmd.Flags().BoolVarP(&o.replaceOutput, "replaceOutput", "r", false, "replace output file if exists")
27+
cmd.Flags().BoolVarP(&o.watch, "watch", "w", false, "keeps the command running and watches the input file for changes")
2728
return cmd
2829
}

internal/cli/clu2adv/opts.go

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,91 @@
11
package clu2adv
22

33
import (
4+
"errors"
45
"fmt"
56

7+
"github.com/fsnotify/fsnotify"
68
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/convert"
79
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/file"
810
"github.com/spf13/afero"
911
)
1012

1113
type opts struct {
12-
fs afero.Fs
13-
file string
14-
output string
14+
fs afero.Fs
15+
file string
16+
output string
1517
replaceOutput bool
18+
watch bool
1619
}
1720

1821
func (o *opts) PreRun() error {
1922
if err := file.MustExist(o.fs, o.file); err != nil {
2023
return err
2124
}
22-
if !o.overwriteOutput {
25+
if !o.replaceOutput {
2326
return file.MustNotExist(o.fs, o.output)
2427
}
2528
return nil
2629
}
2730

2831
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 {
2942
inConfig, err := afero.ReadFile(o.fs, o.file)
3043
if err != nil {
3144
return fmt.Errorf("failed to read file %s: %w", o.file, err)
3245
}
3346
outConfig, err := convert.ClusterToAdvancedCluster(inConfig)
3447
if err != nil {
35-
return err
48+
if allowParseErrors {
49+
prefix := []byte("# CONVERT ERROR: " + err.Error() + "\n\n")
50+
outConfig = append([]byte(nil), prefix...)
51+
outConfig = append(outConfig, inConfig...)
52+
} else {
53+
return err
54+
}
3655
}
3756
if err := afero.WriteFile(o.fs, o.output, outConfig, 0o600); err != nil {
3857
return fmt.Errorf("failed to write file %s: %w", o.output, err)
3958
}
4059
return nil
4160
}
61+
62+
func (o *opts) watchFile() error {
63+
watcher, err := fsnotify.NewWatcher()
64+
if err != nil {
65+
return nil
66+
}
67+
defer watcher.Close()
68+
err = watcher.Add(o.file)
69+
if err != nil {
70+
return err
71+
}
72+
watcherError := errors.New("watcher has been closed")
73+
for {
74+
select {
75+
case event, ok := <-watcher.Events:
76+
if !ok {
77+
return watcherError
78+
}
79+
if event.Has(fsnotify.Write) {
80+
if err := o.generateFile(true); err != nil {
81+
return err
82+
}
83+
}
84+
case err, ok := <-watcher.Errors:
85+
if !ok {
86+
return watcherError
87+
}
88+
return err
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)