client.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package environ
  2. import (
  3. context "context"
  4. "fmt"
  5. "io"
  6. "runtime"
  7. "text/tabwriter"
  8. "time"
  9. "google.golang.org/grpc"
  10. )
  11. // GetClientEnvironment returns the clients environment.
  12. func GetClientEnvironment() *Environment {
  13. clientEnv := &Environment{
  14. Version: Version,
  15. GitCommit: GitCommit,
  16. BuildTime: BuildTime,
  17. GoOsArch: GoOsArch,
  18. ApiVersion: APIVersion,
  19. GoVersion: runtime.Version(),
  20. }
  21. return clientEnv
  22. }
  23. // GetBuildInfo uses an existing grpc connection to request the server eviron and returns that with the client environ.
  24. func GetBuildInfo(conn *grpc.ClientConn, timeout time.Duration) (clientEnv *Environment, serverEnv *Environment, err error) {
  25. clientEnv = &Environment{
  26. Version: Version,
  27. GitCommit: GitCommit,
  28. BuildTime: BuildTime,
  29. GoOsArch: GoOsArch,
  30. ApiVersion: APIVersion,
  31. GoVersion: runtime.Version(),
  32. }
  33. svcClient := NewEnvironmentSvcClient(conn)
  34. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  35. defer cancel()
  36. rsp, err := svcClient.GetEnvironment(ctx, &Void{})
  37. if err != nil {
  38. return clientEnv, nil, fmt.Errorf("failed to contact environ server")
  39. }
  40. return clientEnv, rsp, nil
  41. }
  42. // PrintBuildInfo prints the environment for client and server, if they are defined
  43. func PrintBuildInfo(out io.Writer, clientEnv, serverEnv *Environment) {
  44. if clientEnv != nil {
  45. w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0)
  46. fmt.Fprintln(w, "Client:")
  47. fmt.Fprintf(w, " Version:\t%s\n", clientEnv.Version)
  48. fmt.Fprintf(w, " API version:\t%s\n", clientEnv.ApiVersion)
  49. fmt.Fprintf(w, " Go version:\t%s\n", clientEnv.GoVersion)
  50. fmt.Fprintf(w, " Git commit:\t%s\n", clientEnv.GitCommit)
  51. fmt.Fprintf(w, " Built:\t%s\n", clientEnv.BuildTime)
  52. fmt.Fprintf(w, " OS/Arch:\t%s\n", clientEnv.GoOsArch)
  53. w.Flush()
  54. }
  55. if serverEnv != nil {
  56. w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0)
  57. fmt.Fprintln(w, "Server")
  58. fmt.Fprintf(w, " Version:\t%s\n", serverEnv.Version)
  59. fmt.Fprintf(w, " API version:\t%s\n", serverEnv.ApiVersion)
  60. fmt.Fprintf(w, " Go version:\t%s\n", serverEnv.GoVersion)
  61. fmt.Fprintf(w, " Git commit:\t%s\n", serverEnv.GitCommit)
  62. fmt.Fprintf(w, " Built:\t%s\n", serverEnv.BuildTime)
  63. fmt.Fprintf(w, " OS/Arch:\t%s\n", serverEnv.GoOsArch)
  64. w.Flush()
  65. }
  66. }