@@ -3,45 +3,51 @@ package cluster
33import (
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
1011func 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 )
0 commit comments