example.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright (c) 2019 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package main
  25. import (
  26. "context"
  27. "encoding/json"
  28. "os"
  29. "os/exec"
  30. "time"
  31. "github.com/docker/api/client"
  32. "github.com/gogo/protobuf/types"
  33. "github.com/pkg/errors"
  34. "github.com/urfave/cli"
  35. )
  36. var exampleCommand = cli.Command{
  37. Name: "example",
  38. Usage: "sample command using backend, to be removed later",
  39. Action: func(clix *cli.Context) error {
  40. // return information for the current context
  41. ctx, cancel := client.NewContext()
  42. defer cancel()
  43. // get our current context
  44. ctx = current(ctx)
  45. client, err := connect(ctx)
  46. if err != nil {
  47. return errors.Wrap(err, "cannot connect to backend")
  48. }
  49. defer client.Close()
  50. info, err := client.BackendInformation(ctx, &types.Empty{})
  51. if err != nil {
  52. return errors.Wrap(err, "fetch backend information")
  53. }
  54. enc := json.NewEncoder(os.Stdout)
  55. enc.SetIndent("", " ")
  56. return enc.Encode(info)
  57. },
  58. }
  59. // mock information for getting context
  60. // factor out this into a context store package
  61. func current(ctx context.Context) context.Context {
  62. // test backend address
  63. return context.WithValue(ctx, backendAddressKey{}, "/tmp/backend.sock")
  64. }
  65. func connect(ctx context.Context) (*client.Client, error) {
  66. address, err := BackendAddress(ctx)
  67. if err != nil {
  68. return nil, errors.Wrap(err, "no backend address")
  69. }
  70. c, err := client.New("unix://"+address, 500*time.Millisecond)
  71. if err != nil {
  72. if err != context.DeadlineExceeded {
  73. return nil, errors.Wrap(err, "connect to backend")
  74. }
  75. // the backend is not running so start it
  76. cmd := exec.Command("backend-example", "--address", address)
  77. go cmd.Wait()
  78. if err := cmd.Start(); err != nil {
  79. return nil, errors.Wrap(err, "start backend")
  80. }
  81. cl, e := client.New("unix://"+address, 10*time.Second)
  82. return cl, e
  83. }
  84. return c, nil
  85. }
  86. type backendAddressKey struct{}
  87. func BackendAddress(ctx context.Context) (string, error) {
  88. v, ok := ctx.Value(backendAddressKey{}).(string)
  89. if !ok {
  90. return "", errors.New("no backend address key")
  91. }
  92. return v, nil
  93. }