Skip to content

Commit 4ae5c6a

Browse files
committed
Basic config generation and writing to files
1 parent 1accf37 commit 4ae5c6a

10 files changed

Lines changed: 759 additions & 307 deletions

File tree

cmd/plugin/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/cli/clu2adv"
99
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/cli/moduleimport"
1010
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/flags"
11-
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/log"
11+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/logger"
1212
"github.com/spf13/cobra"
1313
)
1414

@@ -21,9 +21,9 @@ func main() {
2121
Short: "Utilities for Terraform's MongoDB Atlas Provider",
2222
Aliases: []string{"tf"},
2323
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
24-
log.SetWriter(cmd.ErrOrStderr())
24+
logger.SetWriter(cmd.ErrOrStderr())
2525
if debugLevel {
26-
log.SetLevel(log.DebugLevel)
26+
logger.SetLevel(logger.DebugLevel)
2727
}
2828

2929
return nil

internal/cli/moduleimport/moduleimport.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77

88
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/flags"
9-
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/log"
9+
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/logger"
1010
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/modulegen"
1111
"github.com/mongodb/atlas-cli-core/config"
1212
"github.com/mongodb/atlas-cli-core/transport"
@@ -21,15 +21,15 @@ const (
2121
// TODO@non-spike: Support tracking plugin versions, used in UserAgent header.
2222
var Version = "dev"
2323

24-
type ModuleImportOpts struct {
24+
type Opts struct {
25+
httpClient *http.Client
2526
input string
2627
output string
27-
atlasBaseUrl string
28-
httpClient *http.Client
28+
atlasBaseURL string
2929
}
3030

3131
func Builder() *cobra.Command {
32-
opts := &ModuleImportOpts{}
32+
opts := &Opts{}
3333
cmd := &cobra.Command{
3434
Use: "module-import",
3535
Short: "Generate Terraform module configurations",
@@ -38,27 +38,33 @@ func Builder() *cobra.Command {
3838
RunE: opts.Run,
3939
}
4040

41-
cmd.Flags().StringVarP(&opts.input, flags.Input, flags.InputShort, "", "path to the input file")
41+
cmd.Flags().StringVarP(
42+
&opts.input, flags.Input, flags.InputShort, "",
43+
"path to the input file",
44+
)
4245
_ = cmd.MarkFlagRequired(flags.Input)
43-
cmd.Flags().StringVarP(&opts.output, flags.Output, flags.OutputShort, "", "path where to the directory where to generate the output files")
46+
cmd.Flags().StringVarP(
47+
&opts.output, flags.Output, flags.OutputShort, "",
48+
"path where to the directory where to generate the output files",
49+
)
4450
_ = cmd.MarkFlagRequired(flags.Output)
4551
return cmd
4652
}
4753

48-
func (opts *ModuleImportOpts) PreRun(cmd *cobra.Command, args []string) error {
49-
_, _ = log.Debugln("[module-import] PreRunE")
54+
func (opts *Opts) PreRun(cmd *cobra.Command, args []string) error {
55+
_, _ = logger.Debugln("[module-import] PreRunE")
5056

5157
profile, err := config.LoadAtlasCLIConfig()
5258
if err != nil {
5359
return err
5460
}
5561

5662
// Use user-overridden url, otherwise if gov use gov url, otherwise use default.
57-
if opts.atlasBaseUrl = profile.OpsManagerURL(); opts.atlasBaseUrl == "" {
63+
if opts.atlasBaseURL = profile.OpsManagerURL(); opts.atlasBaseURL == "" {
5864
if profile.Service() == config.CloudService {
59-
opts.atlasBaseUrl = CloudGovServiceURL
65+
opts.atlasBaseURL = CloudGovServiceURL
6066
} else {
61-
opts.atlasBaseUrl = CloudServiceURL
67+
opts.atlasBaseURL = CloudServiceURL
6268
}
6369
}
6470

@@ -78,18 +84,19 @@ func (opts *ModuleImportOpts) PreRun(cmd *cobra.Command, args []string) error {
7884
return err
7985
}
8086

81-
func (opts *ModuleImportOpts) Run(cmd *cobra.Command, args []string) error {
82-
_, _ = log.Debugln("[module-import] RunE")
87+
func (opts *Opts) Run(cmd *cobra.Command, args []string) error {
88+
_, _ = logger.Debugln("[module-import] RunE")
8389
err := modulegen.Run(
8490
cmd.Context(),
85-
&modulegen.ModuleGenArgs{
91+
&modulegen.GenArgs{
8692
InputPath: opts.input,
8793
OutputPath: opts.output,
8894
},
8995
&modulegen.AtlasClientArgs{
90-
AtlasBaseUrl: opts.atlasBaseUrl,
91-
UserAgent: config.UserAgent(Version), // TODO@non-spike: Look into differentiating the plugin's UserAgent from the cli one
92-
HttpClient: opts.httpClient,
96+
AtlasBaseURL: opts.atlasBaseURL,
97+
// TODO@non-spike: Look into differentiating the plugin's UserAgent from the cli one
98+
UserAgent: config.UserAgent(Version),
99+
HTTPClient: opts.httpClient,
93100
},
94101
)
95102
return err

internal/convert/cli_common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
type ConvertFn func(config []byte) ([]byte, error)
14+
type Fn func(config []byte) ([]byte, error)
1515

1616
// BaseOpts contains common functionality for CLI commands that convert files.
1717
type BaseOpts struct {
1818
Fs afero.Fs
19-
Convert ConvertFn
19+
Convert Fn
2020
File string
2121
Output string
2222
ReplaceOutput bool

internal/log/log_test.go

Lines changed: 0 additions & 195 deletions
This file was deleted.
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package log
15+
package logger
1616

1717
import (
1818
"fmt"
@@ -25,6 +25,7 @@ type Level int
2525
const (
2626
NoneLevel Level = iota
2727
WarningLevel
28+
InfoLevel
2829
DebugLevel
2930
)
3031

@@ -60,6 +61,10 @@ func (l *Logger) IsDebugLevel() bool {
6061
return l.level >= DebugLevel
6162
}
6263

64+
func (l *Logger) IsInfoLevel() bool {
65+
return l.level >= InfoLevel
66+
}
67+
6368
func (l *Logger) IsWarningLevel() bool {
6469
return l.level >= WarningLevel
6570
}
@@ -107,18 +112,27 @@ func (l *Logger) Warningf(format string, a ...any) (int, error) {
107112
}
108113

109114
func (l *Logger) Info(a ...any) (int, error) {
115+
if !l.IsInfoLevel() {
116+
return 0, nil
117+
}
110118
return fmt.Fprint(l.w, a...)
111119
}
112120

113121
func (l *Logger) Infoln(a ...any) (int, error) {
122+
if !l.IsInfoLevel() {
123+
return 0, nil
124+
}
114125
return fmt.Fprintln(l.w, a...)
115126
}
116127

117128
func (l *Logger) Infof(format string, a ...any) (int, error) {
129+
if !l.IsInfoLevel() {
130+
return 0, nil
131+
}
118132
return fmt.Fprintf(l.w, format, a...)
119133
}
120134

121-
var std = New(os.Stderr, WarningLevel)
135+
var std = New(os.Stderr, InfoLevel)
122136

123137
func Writer() io.Writer {
124138
return std.Writer()
@@ -136,6 +150,10 @@ func IsDebugLevel() bool {
136150
return std.IsDebugLevel()
137151
}
138152

153+
func IsInfoLevel() bool {
154+
return std.IsInfoLevel()
155+
}
156+
139157
func IsWarningLevel() bool {
140158
return std.IsWarningLevel()
141159
}

0 commit comments

Comments
 (0)