Skip to content

Commit ca8fc6d

Browse files
committed
overwriteOutput flag
1 parent 00651b5 commit ca8fc6d

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

internal/cli/cluster/cluster.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,51 @@ package cluster
33
import (
44
"fmt"
55

6+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/file"
67
"github.com/spf13/afero"
78
"github.com/spf13/cobra"
89
)
910

1011
func Builder() *cobra.Command {
11-
opts := &Opts{fs: afero.NewOsFs()}
12+
o := &opts{fs: afero.NewOsFs()}
1213
cmd := &cobra.Command{
1314
Use: "cluster_to_advanced",
1415
Short: "Upgrade cluster to advanced_cluster",
1516
Long: "Upgrade Terraform config from mongodbatlas_cluster to mongodbatlas_advanced_cluster",
1617
Aliases: []string{"clu2adv"},
1718
PreRunE: func(cmd *cobra.Command, args []string) error {
18-
if exists, err := afero.Exists(opts.fs, opts.file); !exists || err != nil {
19-
return fmt.Errorf("input file not found: %s", opts.file)
20-
}
21-
if exists, err := afero.Exists(opts.fs, opts.output); exists || err != nil {
22-
return fmt.Errorf("output file can't exist: %s", opts.output)
23-
}
24-
return nil
19+
return o.PreRun()
2520
},
2621
RunE: func(_ *cobra.Command, _ []string) error {
27-
return opts.Run()
22+
return o.Run()
2823
},
2924
}
30-
cmd.Flags().StringVarP(&opts.file, "file", "f", "", "input file")
31-
cmd.Flags().StringVarP(&opts.output, "output", "o", "", "output file")
32-
_ = cmd.MarkFlagFilename("file")
25+
cmd.Flags().StringVarP(&o.file, "file", "f", "", "input file")
3326
_ = cmd.MarkFlagRequired("file")
27+
cmd.Flags().StringVarP(&o.output, "output", "o", "", "output file")
3428
_ = cmd.MarkFlagRequired("output")
29+
cmd.Flags().BoolVarP(&o.overwriteOutput, "overwriteOutput", "w", false, "overwrite output file if exists")
3530
return cmd
3631
}
3732

38-
type Opts struct {
39-
fs afero.Fs
40-
file string
41-
output string
33+
type opts struct {
34+
fs afero.Fs
35+
file string
36+
output string
37+
overwriteOutput bool
4238
}
4339

44-
func (o *Opts) Run() error {
40+
func (o *opts) PreRun() error {
41+
if err := file.MustExist(o.fs, o.file); err != nil {
42+
return err
43+
}
44+
if !o.overwriteOutput {
45+
return file.MustNotExist(o.fs, o.output)
46+
}
47+
return nil
48+
}
49+
50+
func (o *opts) Run() error {
4551
content, err := afero.ReadFile(o.fs, o.file)
4652
if err != nil {
4753
return fmt.Errorf("failed to read file %s: %w", o.file, err)

internal/file/file.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package file
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/afero"
7+
)
8+
9+
func Exists(fs afero.Fs, filename string) (exists bool, err error) {
10+
exists, err = afero.Exists(fs, filename)
11+
if err != nil {
12+
return false, newError(err, filename)
13+
}
14+
return
15+
}
16+
17+
func MustExist(fs afero.Fs, filename string) error {
18+
exists, err := Exists(fs, filename)
19+
if err != nil {
20+
return err
21+
}
22+
if !exists {
23+
return fmt.Errorf("file must exist: %s", filename)
24+
}
25+
return nil
26+
}
27+
28+
func MustNotExist(fs afero.Fs, filename string) error {
29+
exists, err := Exists(fs, filename)
30+
if err != nil {
31+
return err
32+
}
33+
if exists {
34+
return fmt.Errorf("file must not exist: %s", filename)
35+
}
36+
return nil
37+
}
38+
39+
func newError(err error, filename string) error {
40+
return fmt.Errorf("error in file %s: %w", filename, err)
41+
}

0 commit comments

Comments
 (0)