1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package environ
- import (
- context "context"
- "fmt"
- "io"
- "runtime"
- "text/tabwriter"
- "time"
- "google.golang.org/grpc"
- )
- // GetClientEnvironment returns the clients environment.
- func GetClientEnvironment() *Environment {
- clientEnv := &Environment{
- Version: Version,
- GitCommit: GitCommit,
- BuildTime: BuildTime,
- GoOsArch: GoOsArch,
- ApiVersion: APIVersion,
- GoVersion: runtime.Version(),
- }
- return clientEnv
- }
- // GetBuildInfo uses an existing grpc connection to request the server eviron and returns that with the client environ.
- func GetBuildInfo(conn *grpc.ClientConn, timeout time.Duration) (clientEnv *Environment, serverEnv *Environment, err error) {
- clientEnv = &Environment{
- Version: Version,
- GitCommit: GitCommit,
- BuildTime: BuildTime,
- GoOsArch: GoOsArch,
- ApiVersion: APIVersion,
- GoVersion: runtime.Version(),
- }
- svcClient := NewEnvironmentSvcClient(conn)
- ctx, cancel := context.WithTimeout(context.Background(), timeout)
- defer cancel()
- rsp, err := svcClient.GetEnvironment(ctx, &Void{})
- if err != nil {
- return clientEnv, nil, fmt.Errorf("failed to contact environ server")
- }
- return clientEnv, rsp, nil
- }
- // PrintBuildInfo prints the environment for client and server, if they are defined
- func PrintBuildInfo(out io.Writer, clientEnv, serverEnv *Environment) {
- if clientEnv != nil {
- w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0)
- fmt.Fprintln(w, "Client:")
- fmt.Fprintf(w, " Version:\t%s\n", clientEnv.Version)
- fmt.Fprintf(w, " API version:\t%s\n", clientEnv.ApiVersion)
- fmt.Fprintf(w, " Go version:\t%s\n", clientEnv.GoVersion)
- fmt.Fprintf(w, " Git commit:\t%s\n", clientEnv.GitCommit)
- fmt.Fprintf(w, " Built:\t%s\n", clientEnv.BuildTime)
- fmt.Fprintf(w, " OS/Arch:\t%s\n", clientEnv.GoOsArch)
- w.Flush()
- }
- if serverEnv != nil {
- w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0)
- fmt.Fprintln(w, "Server")
- fmt.Fprintf(w, " Version:\t%s\n", serverEnv.Version)
- fmt.Fprintf(w, " API version:\t%s\n", serverEnv.ApiVersion)
- fmt.Fprintf(w, " Go version:\t%s\n", serverEnv.GoVersion)
- fmt.Fprintf(w, " Git commit:\t%s\n", serverEnv.GitCommit)
- fmt.Fprintf(w, " Built:\t%s\n", serverEnv.BuildTime)
- fmt.Fprintf(w, " OS/Arch:\t%s\n", serverEnv.GoOsArch)
- w.Flush()
- }
- }
|