@@ -3,18 +3,51 @@ package cluster
33import (
44 "fmt"
55
6+ "github.com/spf13/afero"
67 "github.com/spf13/cobra"
78)
89
910func Builder () * cobra.Command {
11+ opts := & Opts {fs : afero .NewOsFs ()}
1012 cmd := & cobra.Command {
1113 Use : "cluster_to_advanced" ,
1214 Short : "Upgrade cluster to advanced_cluster" ,
13- Long : "WIP - Long description for upgrade cluster to advanced_cluster " ,
15+ Long : "Upgrade Terraform config from mongodbatlas_cluster to mongodbatlas_advanced_cluster " ,
1416 Aliases : []string {"clu2adv" },
15- Run : func (_ * cobra.Command , _ []string ) {
16- fmt .Println ("WIP - Upgrade cluster to advanced_cluster" )
17+ 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
25+ },
26+ RunE : func (_ * cobra.Command , _ []string ) error {
27+ return opts .Run ()
1728 },
1829 }
30+ cmd .Flags ().StringVarP (& opts .file , "file" , "f" , "" , "input file" )
31+ cmd .Flags ().StringVarP (& opts .output , "output" , "o" , "" , "output file" )
32+ _ = cmd .MarkFlagFilename ("file" )
33+ _ = cmd .MarkFlagRequired ("file" )
34+ _ = cmd .MarkFlagRequired ("output" )
1935 return cmd
2036}
37+
38+ type Opts struct {
39+ fs afero.Fs
40+ file string
41+ output string
42+ }
43+
44+ func (o * Opts ) Run () error {
45+ content , err := afero .ReadFile (o .fs , o .file )
46+ if err != nil {
47+ return fmt .Errorf ("failed to read file %s: %w" , o .file , err )
48+ }
49+ if err := afero .WriteFile (o .fs , o .output , content , 0o600 ); err != nil {
50+ return fmt .Errorf ("failed to write file %s: %w" , o .output , err )
51+ }
52+ return nil
53+ }
0 commit comments