Skip to content

Commit dc99df0

Browse files
committed
cmd/loop: return error in readMacaroon
As we only use the readMacaroon function inside getClientConn where we have an error return value anyway, we might as well pass the error along correctly instead of failing hard directly.
1 parent 39d1121 commit dc99df0

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

cmd/loop/main.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,16 @@ func getClientConn(address, tlsCertPath, macaroonPath string) (*grpc.ClientConn,
380380
// TLS cannot be disabled, we'll always have a cert file to read.
381381
creds, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
382382
if err != nil {
383-
fatal(err)
383+
return nil, err
384384
}
385385

386386
// Macaroons are not yet enabled by default.
387387
if macaroonPath != "" {
388-
opts = append(opts, readMacaroon(macaroonPath))
388+
macOption, err := readMacaroon(macaroonPath)
389+
if err != nil {
390+
return nil, err
391+
}
392+
opts = append(opts, macOption)
389393
}
390394

391395
opts = append(opts, grpc.WithTransportCredentials(creds))
@@ -401,16 +405,16 @@ func getClientConn(address, tlsCertPath, macaroonPath string) (*grpc.ClientConn,
401405

402406
// readMacaroon tries to read the macaroon file at the specified path and create
403407
// gRPC dial options from it.
404-
func readMacaroon(macPath string) grpc.DialOption {
408+
func readMacaroon(macPath string) (grpc.DialOption, error) {
405409
// Load the specified macaroon file.
406410
macBytes, err := ioutil.ReadFile(macPath)
407411
if err != nil {
408-
fatal(fmt.Errorf("unable to read macaroon path : %v", err))
412+
return nil, fmt.Errorf("unable to read macaroon path : %v", err)
409413
}
410414

411415
mac := &macaroon.Macaroon{}
412416
if err = mac.UnmarshalBinary(macBytes); err != nil {
413-
fatal(fmt.Errorf("unable to decode macaroon: %v", err))
417+
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
414418
}
415419

416420
macConstraints := []macaroons.Constraint{
@@ -430,10 +434,10 @@ func readMacaroon(macPath string) grpc.DialOption {
430434
// Apply constraints to the macaroon.
431435
constrainedMac, err := macaroons.AddConstraints(mac, macConstraints...)
432436
if err != nil {
433-
fatal(err)
437+
return nil, err
434438
}
435439

436440
// Now we append the macaroon credentials to the dial options.
437441
cred := macaroons.NewMacaroonCredential(constrainedMac)
438-
return grpc.WithPerRPCCredentials(cred)
442+
return grpc.WithPerRPCCredentials(cred), nil
439443
}

0 commit comments

Comments
 (0)