Skip to content

Commit 00651b5

Browse files
committed
pass parameters
1 parent de79fb8 commit 00651b5

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bin
22
bin-plugin
3+
.vscode

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ module github.com/mongodb-labs/atlas-cli-plugin-terraform
22

33
go 1.23.4
44

5-
require github.com/spf13/cobra v1.8.1
5+
require (
6+
github.com/spf13/afero v1.12.0
7+
github.com/spf13/cobra v1.8.1
8+
)
69

710
require (
811
github.com/inconshreveable/mousetrap v1.1.0 // indirect
912
github.com/spf13/pflag v1.0.5 // indirect
13+
golang.org/x/text v0.21.0 // indirect
1014
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
22
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
33
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
44
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
6+
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
57
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
68
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
79
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
810
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
11+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
12+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
913
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1014
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/cli/cluster/cluster.go

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

6+
"github.com/spf13/afero"
67
"github.com/spf13/cobra"
78
)
89

910
func 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

Comments
 (0)