Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option: --hcl-only to only keep the .tf files in the output directory, and removing the others #246

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type CommonConfig struct {
Parallelism int
PlainUI bool
GenerateMappingFile bool
HCLOnly bool
}

type Config struct {
Expand Down
41 changes: 41 additions & 0 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type BaseMeta interface {
GenerateCfg(ImportList) error
ExportSkippedResources(l ImportList) error
ExportResourceMapping(ImportList) error
CleanUpWorkspace() error
}

var _ BaseMeta = &baseMeta{}
Expand All @@ -50,6 +51,7 @@ type baseMeta struct {
backendConfig []string
fullConfig bool
parallelism int
hclOnly bool

// Use a safer name which is less likely to conflicts with users' existing files.
// This is mainly used for the --append option.
Expand Down Expand Up @@ -152,6 +154,7 @@ The output directory is not empty. Please choose one of actions below:
parallelism: cfg.Parallelism,
useSafeFilename: cfg.Append,
empty: empty,
hclOnly: cfg.HCLOnly,
}

return meta, nil
Expand Down Expand Up @@ -264,6 +267,44 @@ func (meta baseMeta) ExportSkippedResources(l ImportList) error {
return nil
}

func (meta baseMeta) CleanUpWorkspace() error {
// Do nothing if not HCL only... Otherwise, clean up the workspace to only keep the HCL files.
if !meta.hclOnly {
return nil
}

tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
defer func() {
os.RemoveAll(tmpDir)
}()

tmpMainCfg := filepath.Join(tmpDir, meta.filenameMainCfg())
tmpProviderCfg := filepath.Join(tmpDir, meta.filenameProviderSetting())

if err := os.Rename(filepath.Join(meta.Workspace(), meta.filenameMainCfg()), tmpMainCfg); err != nil {
return err
}
if err := os.Rename(filepath.Join(meta.Workspace(), meta.filenameProviderSetting()), tmpProviderCfg); err != nil {
return err
}

if err := removeEverythingUnder(meta.Workspace()); err != nil {
return err
}

if err := os.Rename(tmpMainCfg, filepath.Join(meta.Workspace(), meta.filenameMainCfg())); err != nil {
return err
}
if err := os.Rename(tmpProviderCfg, filepath.Join(meta.Workspace(), meta.filenameProviderSetting())); err != nil {
return err
}

return nil
}

func (meta baseMeta) generateCfg(l ImportList, cfgTrans ...TFConfigTransformer) error {
ctx := context.TODO()

Expand Down
5 changes: 5 additions & 0 deletions internal/meta/meta_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ func (m MetaGroupDummy) ExportSkippedResources(l ImportList) error {
time.Sleep(500 * time.Millisecond)
return nil
}

func (m MetaGroupDummy) CleanUpWorkspace() error {
time.Sleep(500 * time.Millisecond)
return nil
}
5 changes: 5 additions & 0 deletions internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func BatchImport(cfg config.Config) error {
return fmt.Errorf("generating Terraform configuration: %v", err)
}

msg.SetStatus("Cleaning up the output directory to only keep the .tf files...")
if err := c.CleanUpWorkspace(); err != nil {
return fmt.Errorf("cleaning up the output directory to only keep the .tf files: %v", err)
}

return nil
}

Expand Down
11 changes: 11 additions & 0 deletions internal/ui/aztfyclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type ExportSkippedResourcesDoneMsg struct {

type GenerateCfgDoneMsg struct{}

type WorkspaceCleanupDoneMsg struct{}

type QuitMsg struct{}

type CleanTFStateMsg struct {
Expand Down Expand Up @@ -117,6 +119,15 @@ func GenerateCfg(c meta.Meta, l meta.ImportList) tea.Cmd {
}
}

func CleanUpWorkspace(c meta.Meta) tea.Cmd {
return func() tea.Msg {
if err := c.CleanUpWorkspace(); err != nil {
return ErrMsg(err)
}
return WorkspaceCleanupDoneMsg{}
}
}

func ExportResourceMapping(c meta.Meta, l meta.ImportList) tea.Cmd {
return func() tea.Msg {
if err := c.ExportResourceMapping(l); err != nil {
Expand Down
11 changes: 9 additions & 2 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
statusImporting
statusImportErrorMsg
statusGeneratingCfg
statusCleaningUpWorkspaceCfg
statusExportResourceMapping
statusExportSkippedResources
statusSummary
Expand All @@ -53,6 +54,7 @@ func (s status) String() string {
"importing",
"import error message",
"generating Terraform configuration",
"cleaning up output directory",
"exporting resource mapping file",
"exporting skipped resources file",
"summary",
Expand Down Expand Up @@ -162,6 +164,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.status = statusGeneratingCfg
return m, aztfyclient.GenerateCfg(m.meta, msg.List)
case aztfyclient.GenerateCfgDoneMsg:
m.status = statusCleaningUpWorkspaceCfg
return m, aztfyclient.CleanUpWorkspace(m.meta)
case aztfyclient.WorkspaceCleanupDoneMsg:
m.status = statusSummary
return m, nil
case aztfyclient.QuitMsg:
Expand Down Expand Up @@ -217,12 +222,14 @@ func (m model) View() string {
s += importErrorView(m)
case statusImporting:
s += m.spinner.View() + m.progress.View()
case statusGeneratingCfg:
s += m.spinner.View() + " Generating Terraform Configurations..."
case statusExportResourceMapping:
s += m.spinner.View() + " Exporting Resource Mapping..."
case statusExportSkippedResources:
s += m.spinner.View() + " Exporting Skipped Resources..."
case statusGeneratingCfg:
s += m.spinner.View() + " Generating Terraform Configurations..."
case statusCleaningUpWorkspaceCfg:
s += m.spinner.View() + " Cleaning up the output directory..."
case statusSummary:
s += summaryView(m)
case statusError:
Expand Down
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
flagContinue bool
flagNonInteractive bool
flagGenerateMappingFile bool
flagHCLOnly bool

// common flags (hidden)
hflagMockClient bool
Expand Down Expand Up @@ -78,6 +79,14 @@ func main() {
return fmt.Errorf("`--generate-mapping-file` must be used together with `--non-interactive`")
}
}
if flagHCLOnly {
if flagBackendType != "local" {
return fmt.Errorf("`--hcl-only` only works for local backend")
}
if flagAppend {
return fmt.Errorf("`--appned` conflicts with `--hcl-only`")
}
}

// Identify the subscription id, which comes from one of following (starts from the highest priority):
// - Command line option
Expand Down Expand Up @@ -182,6 +191,12 @@ func main() {
Usage: "Only generate the resource mapping file, but DO NOT import any resource",
Destination: &flagGenerateMappingFile,
},
&cli.BoolFlag{
Name: "hcl-only",
EnvVars: []string{"AZTFY_HCL_ONLY"},
Usage: "Only generate HCL code, but not the files for resource management (e.g. the state file)",
Destination: &flagHCLOnly,
},

// Hidden flags
&cli.BoolFlag{
Expand Down Expand Up @@ -292,6 +307,7 @@ func main() {
Parallelism: flagParallelism,
PlainUI: hflagPlainUI,
GenerateMappingFile: flagGenerateMappingFile,
HCLOnly: flagHCLOnly,
},
ResourceId: resId,
TFResourceName: flagResName,
Expand Down Expand Up @@ -336,6 +352,7 @@ func main() {
Parallelism: flagParallelism,
PlainUI: hflagPlainUI,
GenerateMappingFile: flagGenerateMappingFile,
HCLOnly: flagHCLOnly,
},
ResourceGroupName: rg,
ResourceNamePattern: flagPattern,
Expand Down Expand Up @@ -379,6 +396,7 @@ func main() {
Parallelism: flagParallelism,
PlainUI: hflagPlainUI,
GenerateMappingFile: flagGenerateMappingFile,
HCLOnly: flagHCLOnly,
},
ARGPredicate: predicate,
ResourceNamePattern: flagPattern,
Expand Down Expand Up @@ -422,6 +440,7 @@ func main() {
Parallelism: flagParallelism,
PlainUI: hflagPlainUI,
GenerateMappingFile: flagGenerateMappingFile,
HCLOnly: flagHCLOnly,
},
MappingFile: mapFile,
}
Expand Down