Skip to content

Commit ab4bf2b

Browse files
Allow switching to simple logging (#4474)
1 parent e481f6b commit ab4bf2b

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

v2/cmd/asoctl/cmd/import_azure_resource.go

+49-18
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
"io"
1111
"os"
1212
"sync"
13+
"time"
1314

1415
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
1516
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1617
"github.com/go-logr/logr"
1718
"github.com/rotisserie/eris"
1819
"github.com/spf13/cobra"
20+
"github.com/vbauerster/mpb/v8"
1921

2022
"github.com/Azure/azure-service-operator/v2/api"
2123
"github.com/Azure/azure-service-operator/v2/cmd/asoctl/pkg/importreporter"
@@ -111,6 +113,13 @@ https://docs.microsoft.com/azure/active-directory/develop/authentication-nationa
111113
4,
112114
"The number of parallel workers to use when importing resources")
113115

116+
cmd.Flags().BoolVarP(
117+
&options.simpleLogging,
118+
"simple-logging",
119+
"s",
120+
false,
121+
"Use simple logging instead of progress bars")
122+
114123
return cmd
115124
}
116125

@@ -131,30 +140,51 @@ func importAzureResource(
131140
return eris.Wrapf(err, "failed to create ARM client")
132141
}
133142

134-
// Caution: the progress bar can deadlock if no bar is ever created, so make sure the gap between
135-
// this and the call to importer.Import() is as small as possible.
136-
log, progressBar := CreateLoggerAndProgressBar()
137-
138143
done := make(chan struct{}) // signal for when we're done
139-
pb := importreporter.NewBar("Import Azure Resources", progressBar, done)
144+
145+
var log logr.Logger
146+
var progress importreporter.Interface
147+
var output io.Writer
148+
if options.simpleLogging {
149+
log = CreateLogger()
150+
progress = importreporter.NewLog("Import Azure Resources", log, done)
151+
output = os.Stderr
152+
} else {
153+
var bar *mpb.Progress
154+
155+
// Caution: the progress bar can deadlock if no bar is ever created, so make sure the gap between
156+
// this and the call to importer.Import() is as small as possible.
157+
log, bar = CreateLoggerAndProgressBar()
158+
progress = importreporter.NewBar("Import Azure Resources", bar, done)
159+
160+
// The progress bar can deadlock if no bar is ever created - which can happen if we need to return an error
161+
// between here and the call to importer.Import(). To avoid this, we create a dummy bar that will complete
162+
// asynchronously after a short delay.
163+
setup := progress.Create("Initalizing")
164+
setup.AddPending(1)
165+
go func() {
166+
time.Sleep(200 * time.Millisecond)
167+
setup.Completed(1)
168+
}()
169+
170+
output = bar
171+
172+
// Ensure the progress bar is closed when we're done
173+
defer bar.Wait()
174+
}
140175

141176
importerOptions := importresources.ResourceImporterOptions{
142177
Workers: options.workers,
143178
}
144179

145-
importer := importresources.New(api.CreateScheme(), client, log, pb, importerOptions)
180+
importer := importresources.New(api.CreateScheme(), client, log, progress, importerOptions)
146181
for _, armID := range armIDs {
147182
err = importer.AddARMID(armID)
148183
if err != nil {
149184
return eris.Wrapf(err, "failed to add %q to import list", armID)
150185
}
151186
}
152187

153-
// Make sure all output is written when we're done.
154-
// This defer has to be immediately before the call the importer.Import();
155-
// if you move it earlier, any `return err` between there and here will cause a deadlock.
156-
defer progressBar.Wait()
157-
158188
result, err := importer.Import(ctx, done)
159189

160190
if ctx.Err() != nil {
@@ -180,7 +210,7 @@ func importAzureResource(
180210
return eris.Wrap(err, "failed to apply options to imported resources")
181211
}
182212

183-
err = writeResources(result, options, log, progressBar)
213+
err = writeResources(result, options, log, output)
184214
if err != nil {
185215
return eris.Wrap(err, "failed to write resources")
186216
}
@@ -274,12 +304,13 @@ func writeResources(
274304
}
275305

276306
type importAzureResourceOptions struct {
277-
outputPath string
278-
outputFolder string
279-
namespace string
280-
annotations []string
281-
labels []string
282-
workers int
307+
outputPath string
308+
outputFolder string
309+
namespace string
310+
annotations []string
311+
labels []string
312+
workers int
313+
simpleLogging bool
283314

284315
readCloud sync.Once
285316
azureAuthorityHost string

0 commit comments

Comments
 (0)