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

Adding log level to CLI option and add more logs #317

Merged
merged 2 commits into from
Dec 2, 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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ require (
github.com/charmbracelet/bubbles v0.14.0
github.com/charmbracelet/bubbletea v0.22.1
github.com/charmbracelet/lipgloss v0.5.0
github.com/hashicorp/go-hclog v1.3.1
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hc-install v0.4.0
github.com/hashicorp/hcl/v2 v2.13.0
github.com/hashicorp/terraform-exec v0.17.2
github.com/hexops/gotextdiff v1.0.3
github.com/magodo/armid v0.0.0-20220923023118-aec41eaf7370
github.com/magodo/azlist v0.0.0-20221028090226-002373d16e12
github.com/magodo/azlist v0.0.0-20221201100009-664a9dd10ef3
github.com/magodo/aztft v0.3.1-0.20221128054002-943ad3127ad0
github.com/magodo/spinner v0.0.0-20220720073946-50f31b2dc5a6
github.com/magodo/textinput v0.0.0-20210913072708-7d24f2b4b0c0
Expand Down Expand Up @@ -72,6 +73,7 @@ require (
github.com/containerd/console v1.0.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.1.2 // indirect
Expand All @@ -85,6 +87,7 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magodo/tfpluginschema v0.0.0-20220905090502-2d6a05ebaefd // indirect
github.com/magodo/tfstate v0.0.0-20220409052014-9b9568dda918 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand Down
17 changes: 15 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
Expand Down Expand Up @@ -141,6 +143,8 @@ github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuD
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.3.1 h1:vDwF1DFNZhntP4DAjuTpOw3uEgMUpXh1pB5fW9DqHpo=
github.com/hashicorp/go-hclog v1.3.1/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down Expand Up @@ -181,8 +185,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magodo/armid v0.0.0-20220923023118-aec41eaf7370 h1:n8RrB7jcZ9lQE7tyF2a7AEJ3Ux/E6E6FTeZLfgadPmg=
github.com/magodo/armid v0.0.0-20220923023118-aec41eaf7370/go.mod h1:rR8E7zfGMbmfnSQvrkFiWYdhrfTqsVSltelnZB09BwA=
github.com/magodo/azlist v0.0.0-20221028090226-002373d16e12 h1:sy3mo2oXhPIqgH8mdhBEOCpc4EcY9QghrSmn/VlCj4U=
github.com/magodo/azlist v0.0.0-20221028090226-002373d16e12/go.mod h1:Zke5PFfagiNep06RgNgMXgpLVW4LafSWJJqSQKQAd80=
github.com/magodo/azlist v0.0.0-20221201100009-664a9dd10ef3 h1:edFobje+hvzs8Vx2Lb8qRZLGN2OOtWoXuUQGH9BdAec=
github.com/magodo/azlist v0.0.0-20221201100009-664a9dd10ef3/go.mod h1:r1a269lM5tSby3J7PaEUZQCKWgCLKz9KSWgs7u6fR/M=
github.com/magodo/aztft v0.3.1-0.20221128054002-943ad3127ad0 h1:cyPS+rRd0OZUzpH8CtDNzrKUTmAa/Sz93Cd4j/SC9LM=
github.com/magodo/aztft v0.3.1-0.20221128054002-943ad3127ad0/go.mod h1:tk3eGjxk+d+Y3fcenXT2fWMLGZqzHww4mdxYuvcrfLU=
github.com/magodo/spinner v0.0.0-20220720073946-50f31b2dc5a6 h1:CElHO4hPXC+Eivy8sUC/WrnH3jmQzdF2x0lEXBEYul8=
Expand All @@ -200,6 +204,10 @@ github.com/magodo/tfstate v0.0.0-20220409052014-9b9568dda918/go.mod h1:BW96zQS6A
github.com/magodo/workerpool v0.0.0-20211124060943-1c48f3e5a514 h1:9JtvsO+tAKh70rXqUb39Ldn4p6zDqDOgrCpo15MM1cw=
github.com/magodo/workerpool v0.0.0-20211124060943-1c48f3e5a514/go.mod h1:oI7XLq0SfJZISAwYhT7DHmi1Fqbr1Q8ZE8gegIe7iAI=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down Expand Up @@ -253,6 +261,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
Expand Down Expand Up @@ -294,6 +303,8 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -302,8 +313,10 @@ golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import "github.com/hashicorp/go-hclog"

type CommonConfig struct {
LogPath string
LogLevel hclog.Level
SubscriptionId string
OutputDir string
Append bool
Expand Down
19 changes: 19 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package log

type Logger interface {
Printf(format string, v ...any)
}

var log Logger = nullLogger{}

type nullLogger struct{}

func (nullLogger) Printf(format string, v ...any) {}

func SetLogger(l Logger) {
log = l
}

func Printf(format string, v ...any) {
log.Printf(format, v...)
}
7 changes: 7 additions & 0 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/Azure/aztfy/internal/client"
"github.com/Azure/aztfy/internal/config"
"github.com/Azure/aztfy/internal/log"
"github.com/Azure/aztfy/internal/resmap"
"github.com/Azure/aztfy/internal/utils"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
Expand Down Expand Up @@ -196,6 +197,7 @@ func (meta *baseMeta) ParallelImport(items []*ImportItem) {
// Ensure the state file is removed after this round import, preparing for the next round.
defer os.Remove(stateFile)

log.Printf("[DEBUG] Merging terraform state file %s", stateFile)
newState, err := tfmerge.Merge(ctx, meta.tf, meta.baseState, stateFile)
if err != nil {
items[idx].ImportError = fmt.Errorf("failed to merge state file: %v", err)
Expand All @@ -222,6 +224,7 @@ func (meta *baseMeta) ParallelImport(items []*ImportItem) {
defer os.Remove(cfgFile)

// Import resources
log.Printf("[INFO] Importing %s as %s", item.TFResourceId, item.TFAddr.String())
err := tf.Import(ctx, item.TFAddr.String(), item.TFResourceId)
item.ImportError = err
item.Imported = err == nil
Expand Down Expand Up @@ -415,6 +418,7 @@ func (meta baseMeta) filenameTmpCfg() string {
}

func (meta *baseMeta) initTF(ctx context.Context) error {
log.Printf("[INFO] Init Terraform")
tfDir := filepath.Join(meta.rootdir, "terraform")
// #nosec G301
if err := os.MkdirAll(tfDir, 0750); err != nil {
Expand All @@ -424,6 +428,7 @@ func (meta *baseMeta) initTF(ctx context.Context) error {
if err != nil {
return fmt.Errorf("error finding a terraform exectuable: %w", err)
}
log.Printf("[INFO] Find terraform binary at %s", execPath)

newTF := func(dir string) (*tfexec.Terraform, error) {
tf, err := tfexec.NewTerraform(dir, execPath)
Expand Down Expand Up @@ -459,10 +464,12 @@ func (meta *baseMeta) initTF(ctx context.Context) error {
}

func (meta *baseMeta) initProvider(ctx context.Context) error {
log.Printf("[INFO] Init provider")
exists, err := dirContainsProviderSetting(meta.outdir)
if err != nil {
return err
}
log.Printf("[INFO] Output directory contains provider setting: %t", exists)
if !exists {
cfgFile := filepath.Join(meta.outdir, meta.filenameProviderSetting())
if err := utils.WriteFileSync(cfgFile, []byte(meta.providerConfig(meta.backendType)), 0644); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/meta/meta_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

"github.com/Azure/aztfy/internal/config"
"github.com/Azure/aztfy/internal/log"
"github.com/Azure/aztfy/internal/resmap"
"github.com/Azure/aztfy/internal/tfaddr"
"github.com/magodo/armid"
Expand All @@ -20,6 +21,7 @@ type MetaMap struct {
}

func newMetaMap(cfg config.Config) (Meta, error) {
log.Printf("[INFO] New map meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions internal/meta/meta_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/Azure/aztfy/internal/config"
"github.com/Azure/aztfy/internal/log"
"github.com/Azure/aztfy/internal/resourceset"
"github.com/Azure/aztfy/internal/tfaddr"
"github.com/magodo/azlist/azlist"
Expand All @@ -21,6 +22,7 @@ type MetaQuery struct {
}

func newMetaQuery(cfg config.Config) (Meta, error) {
log.Printf("[INFO] New query meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions internal/meta/meta_res.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/Azure/aztfy/internal/config"
"github.com/Azure/aztfy/internal/log"
"github.com/Azure/aztfy/internal/resourceset"
"github.com/Azure/aztfy/internal/tfaddr"
"github.com/magodo/armid"
Expand All @@ -18,6 +19,7 @@ type MetaResource struct {
}

func newMetaResource(cfg config.Config) (Meta, error) {
log.Printf("[INFO] New resource meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand Down
2 changes: 2 additions & 0 deletions internal/meta/meta_rg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/Azure/aztfy/internal/config"
"github.com/Azure/aztfy/internal/log"
"github.com/Azure/aztfy/internal/resourceset"
"github.com/Azure/aztfy/internal/tfaddr"
"github.com/magodo/armid"
Expand All @@ -21,6 +22,7 @@ type MetaResourceGroup struct {
}

func newMetaResourceGroup(cfg config.Config) (Meta, error) {
log.Printf("[INFO] New resource group meta")
baseMeta, err := NewBaseMeta(cfg.CommonConfig)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions internal/resourceset/azure_resource_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (rset AzureResourceSet) ToTFResources() []TFResource {
azureId := res.Id.String()
tftypes, tfids, exact, err := aztft.QueryTypeAndId(azureId, true)
if err != nil {
log.Printf("WARNING: Failed to query resource type for %s: %v\n", azureId, err)
log.Printf("[WARN] Failed to query resource type for %s: %v\n", azureId, err)
// Still put this unresolved resource in the resource set, so that users can later specify the expected TF resource type.
tfresources = append(tfresources, TFResource{
AzureId: res.Id,
Expand All @@ -38,7 +38,7 @@ func (rset AzureResourceSet) ToTFResources() []TFResource {
} else {
if !exact {
// It is not possible to return multiple result when API is used.
log.Printf("WARNING: No query result for resource type and TF id for %s\n", azureId)
log.Printf("[WARN] No query result for resource type and TF id for %s\n", azureId)
// Still put this unresolved resource in the resource set, so that users can later specify the expected TF resource type.
tfresources = append(tfresources, TFResource{
AzureId: res.Id,
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (m model) Init() tea.Cmd {

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if _, ok := msg.(spinner.TickMsg); !ok {
log.Printf("[UI] STATUS: %s | MSG: %#v\n", m.status, msg)
log.Printf("[INFO] UI update. STATUS: %s | MSG: %#v\n", m.status, msg)
}

switch msg := msg.(type) {
Expand Down
Loading