Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package command |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | "fmt" |
| 12 | |
| 13 | "github.com/mongodb/mongo-go-driver/bson" |
| 14 | "github.com/mongodb/mongo-go-driver/x/bsonx" |
| 15 | "github.com/mongodb/mongo-go-driver/x/network/result" |
| 16 | "github.com/mongodb/mongo-go-driver/x/network/wiremessage" |
| 17 | ) |
| 18 | |
| 19 | // BuildInfo represents the buildInfo command. |
| 20 | // |
| 21 | // The buildInfo command is used for getting the build information for a |
| 22 | // MongoDB server. |
| 23 | type BuildInfo struct { |
| 24 | err error |
| 25 | res result.BuildInfo |
| 26 | } |
| 27 | |
| 28 | // Encode will encode this command into a wire message for the given server description. |
| 29 | func (bi *BuildInfo) Encode() (wiremessage.WireMessage, error) { |
| 30 | // This can probably just be a global variable that we reuse. |
| 31 | cmd := bsonx.Doc{{"buildInfo", bsonx.Int32(1)}} |
| 32 | rdr, err := cmd.MarshalBSON() |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | query := wiremessage.Query{ |
| 37 | MsgHeader: wiremessage.Header{RequestID: wiremessage.NextRequestID()}, |
| 38 | FullCollectionName: "admin.$cmd", |
| 39 | Flags: wiremessage.SlaveOK, |
| 40 | NumberToReturn: -1, |
| 41 | Query: rdr, |
| 42 | } |
| 43 | return query, nil |
| 44 | } |
| 45 | |
| 46 | // Decode will decode the wire message using the provided server description. Errors during decoding |
| 47 | // are deferred until either the Result or Err methods are called. |
| 48 | func (bi *BuildInfo) Decode(wm wiremessage.WireMessage) *BuildInfo { |
| 49 | reply, ok := wm.(wiremessage.Reply) |
| 50 | if !ok { |
| 51 | bi.err = fmt.Errorf("unsupported response wiremessage type %T", wm) |
| 52 | return bi |
| 53 | } |
| 54 | rdr, err := decodeCommandOpReply(reply) |
| 55 | if err != nil { |
| 56 | bi.err = err |
| 57 | return bi |
| 58 | } |
| 59 | err = bson.Unmarshal(rdr, &bi.res) |
| 60 | if err != nil { |
| 61 | bi.err = err |
| 62 | return bi |
| 63 | } |
| 64 | return bi |
| 65 | } |
| 66 | |
| 67 | // Result returns the result of a decoded wire message and server description. |
| 68 | func (bi *BuildInfo) Result() (result.BuildInfo, error) { |
| 69 | if bi.err != nil { |
| 70 | return result.BuildInfo{}, bi.err |
| 71 | } |
| 72 | |
| 73 | return bi.res, nil |
| 74 | } |
| 75 | |
| 76 | // Err returns the error set on this command. |
| 77 | func (bi *BuildInfo) Err() error { return bi.err } |
| 78 | |
| 79 | // RoundTrip handles the execution of this command using the provided wiremessage.ReadWriter. |
| 80 | func (bi *BuildInfo) RoundTrip(ctx context.Context, rw wiremessage.ReadWriter) (result.BuildInfo, error) { |
| 81 | wm, err := bi.Encode() |
| 82 | if err != nil { |
| 83 | return result.BuildInfo{}, err |
| 84 | } |
| 85 | |
| 86 | err = rw.WriteWireMessage(ctx, wm) |
| 87 | if err != nil { |
| 88 | return result.BuildInfo{}, err |
| 89 | } |
| 90 | wm, err = rw.ReadWireMessage(ctx) |
| 91 | if err != nil { |
| 92 | return result.BuildInfo{}, err |
| 93 | } |
| 94 | return bi.Decode(wm).Result() |
| 95 | } |