-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from magodo/refactor
Integrate with bubbletea for better TUI
- Loading branch information
Showing
23 changed files
with
1,295 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package config | ||
|
||
import "github.com/meowgorithm/babyenv" | ||
|
||
type Config struct { | ||
ResourceGroupName string // specified via CLI | ||
Logfile string `env:"AZTFY_LOGFILE" default:""` | ||
Debug bool `env:"AZTFY_DEBUG" default:"false"` | ||
MockClient bool `env:"AZTFY_MOCK_CLIENT" default:"false"` | ||
} | ||
|
||
func NewConfig(rg string) (*Config, error) { | ||
var cfg Config | ||
if err := babyenv.Parse(&cfg); err != nil { | ||
return nil, err | ||
} | ||
cfg.ResourceGroupName = rg | ||
return &cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package internal | ||
package meta | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package meta | ||
|
||
import ( | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"io" | ||
) | ||
|
||
type ConfigInfos []ConfigInfo | ||
|
||
type ConfigInfo struct { | ||
ImportItem | ||
hcl *hclwrite.File | ||
} | ||
|
||
func (cfg ConfigInfo) DumpHCL(w io.Writer) (int, error) { | ||
return w.Write(hclwrite.Format(cfg.hcl.Bytes())) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package internal | ||
package meta | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package meta | ||
|
||
type ImportItem struct { | ||
// The azure resource id | ||
ResourceID string | ||
|
||
// Whether this azure resource failed to import into terraform (this might due to the TFResourceType doesn't match the resource) | ||
ImportError error | ||
|
||
// Whether this azure resource failed to validate into terraform (tbh, this should reside in UI layer only) | ||
ValidateError error | ||
|
||
// The terraform resource type | ||
TFResourceType string | ||
|
||
// The terraform resource name | ||
TFResourceName string | ||
} | ||
|
||
func (item ImportItem) Skip() bool { | ||
return item.TFResourceType == "" && item.TFResourceName == "" | ||
} | ||
|
||
func (item *ImportItem) TFAddr() string { | ||
if item.Skip() { | ||
return "" | ||
} | ||
return item.TFResourceType + "." + item.TFResourceName | ||
} | ||
|
||
type ImportList []ImportItem | ||
|
||
func (l ImportList) NonSkipped() ImportList { | ||
var out ImportList | ||
for _, item := range l { | ||
if item.Skip() { | ||
continue | ||
} | ||
out = append(out, item) | ||
} | ||
return out | ||
} | ||
|
||
func (l ImportList) ImportErrored() ImportList { | ||
var out ImportList | ||
for _, item := range l { | ||
if item.ImportError == nil { | ||
continue | ||
} | ||
out = append(out, item) | ||
} | ||
return out | ||
} | ||
|
||
func (l ImportList) Imported() ImportList { | ||
var out ImportList | ||
for _, item := range l.NonSkipped() { | ||
if item.ImportError != nil { | ||
continue | ||
} | ||
out = append(out, item) | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package meta | ||
|
||
import "github.com/magodo/aztfy/internal/config" | ||
|
||
type Meta interface { | ||
Init() error | ||
ResourceGroupName() string | ||
Workspace() string | ||
ListResource() ImportList | ||
CleanTFState() | ||
Import(item ImportItem) error | ||
GenerateCfg(l ImportList) error | ||
} | ||
|
||
func NewMeta(cfg config.Config) (Meta, error) { | ||
if cfg.MockClient { | ||
return newMetaDummy(cfg.ResourceGroupName) | ||
} | ||
return newMetaImpl(cfg.ResourceGroupName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package meta | ||
|
||
import "time" | ||
|
||
type MetaDummy struct { | ||
rg string | ||
} | ||
|
||
func newMetaDummy(rg string) (Meta, error) { | ||
return MetaDummy{rg: rg}, nil | ||
} | ||
|
||
func (m MetaDummy) Init() error { | ||
time.Sleep(500 * time.Millisecond) | ||
return nil | ||
} | ||
|
||
func (m MetaDummy) ResourceGroupName() string { | ||
return m.rg | ||
} | ||
|
||
func (m MetaDummy) Workspace() string { | ||
return "example-workspace" | ||
} | ||
|
||
func (m MetaDummy) ListResource() ImportList { | ||
time.Sleep(500 * time.Millisecond) | ||
return ImportList{ | ||
ImportItem{ | ||
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg/providers/Microsoft.Network/virtualNetworks/example-network", | ||
}, | ||
ImportItem{ | ||
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg/providers/Microsoft.Compute/virtualMachines/example-machine", | ||
}, | ||
ImportItem{ | ||
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg/providers/Microsoft.Network/networkInterfaces/example-nic", | ||
}, | ||
ImportItem{ | ||
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg/providers/virtualNetworks/example-network/subnets/internal", | ||
}, | ||
ImportItem{ | ||
ResourceID: "/subscriptions/0000000-0000-0000-0000-00000000000/resourceGroups/example-rg", | ||
}, | ||
} | ||
} | ||
|
||
func (m MetaDummy) CleanTFState() { | ||
return | ||
} | ||
|
||
func (m MetaDummy) Import(item ImportItem) error { | ||
time.Sleep(time.Second) | ||
return nil | ||
} | ||
|
||
func (m MetaDummy) GenerateCfg(l ImportList) error { | ||
time.Sleep(500 * time.Millisecond) | ||
return nil | ||
} |
Oops, something went wrong.