Skip to content

Commit 983b950

Browse files
Add build tags for additional output on call failure
1 parent 913904b commit 983b950

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

driver.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
243243
// SetConfigFromFlags handles additional driver arguments as retrieved by [Driver.GetCreateFlags];
244244
// see [drivers.Driver.SetConfigFromFlags]
245245
func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
246+
return d.setConfigFromFlags(opts)
247+
}
248+
249+
func (d *Driver) setConfigFromFlagsImpl(opts drivers.DriverOptions) error {
246250
d.AccessToken = opts.String(flagAPIToken)
247251
d.Image = opts.String(flagImage)
248252
d.ImageID = opts.Int(flagImageID)
@@ -267,7 +271,7 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
267271
d.placementGroup = opts.String(flagPlacementGroup)
268272
if opts.Bool(flagAutoSpread) {
269273
if d.placementGroup != "" {
270-
return errors.Errorf(flagAutoSpread + " and " + flagPlacementGroup + " are mutually exclusive")
274+
return d.flagFailure("%v and %v are mutually exclusive", flagAutoSpread, flagPlacementGroup)
271275
}
272276
d.placementGroup = autoSpreadPgName
273277
}
@@ -280,17 +284,17 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
280284
d.SetSwarmConfigFromFlags(opts)
281285

282286
if d.AccessToken == "" {
283-
return errors.Errorf("hetzner requires --%v to be set", flagAPIToken)
287+
return d.flagFailure("hetzner requires --%v to be set", flagAPIToken)
284288
}
285289

286290
if d.ImageID != 0 && d.Image != "" && d.Image != defaultImage /* support legacy behaviour */ {
287-
return errors.Errorf("--%v and --%v are mutually exclusive", flagImage, flagImageID)
291+
return d.flagFailure("--%v and --%v are mutually exclusive", flagImage, flagImageID)
288292
} else if d.ImageID == 0 && d.Image == "" {
289293
d.Image = defaultImage
290294
}
291295

292296
if d.DisablePublic4 && d.DisablePublic6 && !d.UsePrivateNetwork {
293-
return errors.Errorf("--%v must be used if public networking is disabled (hint: implicitly set by --%v)",
297+
return d.flagFailure("--%v must be used if public networking is disabled (hint: implicitly set by --%v)",
294298
flagUsePrivateNetwork, flagDisablePublic)
295299
}
296300

@@ -312,7 +316,7 @@ func (d *Driver) setLabelsFromFlags(opts drivers.DriverOptions) error {
312316
for _, label := range opts.StringSlice(flagServerLabel) {
313317
split := strings.SplitN(label, "=", 2)
314318
if len(split) != 2 {
315-
return errors.Errorf("server label %v is not in key=value format", label)
319+
return d.flagFailure("server label %v is not in key=value format", label)
316320
}
317321
d.ServerLabels[split[0]] = split[1]
318322
}
@@ -331,7 +335,7 @@ func (d *Driver) setLabelsFromFlags(opts drivers.DriverOptions) error {
331335
func (d *Driver) PreCreateCheck() error {
332336
if d.IsExistingKey {
333337
if d.originalKey == "" {
334-
return errors.New("specifying an existing key ID requires the existing key path to be set as well")
338+
return d.flagFailure("specifying an existing key ID requires the existing key path to be set as well")
335339
}
336340

337341
key, err := d.getKey()

flag_failure.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build !flag_debug
2+
3+
package main
4+
5+
import (
6+
"github.com/docker/machine/libmachine/drivers"
7+
"github.com/pkg/errors"
8+
)
9+
10+
func (d *Driver) flagFailure(format string, args ...interface{}) error {
11+
return errors.Errorf(format, args...)
12+
}
13+
14+
func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error {
15+
return d.setConfigFromFlagsImpl(opts)
16+
}

flag_failure_debug.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build flag_debug
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
"fmt"
8+
9+
"github.com/docker/machine/libmachine/drivers"
10+
"github.com/pkg/errors"
11+
)
12+
13+
var lastOpts drivers.DriverOptions
14+
15+
func (d *Driver) flagFailure(format string, args ...interface{}) error {
16+
// machine driver may not flush logs received when getting an RPC error, so we have to resort to this terribleness
17+
line1 := fmt.Sprintf("Flag failure detected:\n -> last opts: %v\n -> driver state %v", lastOpts, d)
18+
var line2 string
19+
if out, err := json.MarshalIndent(d, "", " "); err == nil {
20+
line2 = fmt.Sprintf(" -> driver json:\n%s", out)
21+
} else {
22+
line2 = fmt.Sprintf("could not encode driver json: %v", err)
23+
}
24+
25+
combined := append([]interface{}{line1, line2}, args...)
26+
return errors.Errorf("%s\n%s\n"+format, combined...)
27+
}
28+
29+
func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error {
30+
lastOpts = opts
31+
return d.setConfigFromFlagsImpl(opts)
32+
}

0 commit comments

Comments
 (0)