func NewV2FromReader()

in pkg/client/reader.go [65:120]


func NewV2FromReader(reader io.Reader, ver VersionInfo, opts ...V2ClientOption) (V2, []Service, error) {
	info := &proto.StartUpInfo{}
	data, err := io.ReadAll(reader)
	if err != nil {
		return nil, nil, err
	}
	err = protobuf.Unmarshal(data, info)
	if err != nil {
		return nil, nil, err
	}

	if info.AgentInfo != nil {
		opts = append(opts, WithAgentInfo(AgentInfo{
			ID:           info.AgentInfo.Id,
			Version:      info.AgentInfo.Version,
			Snapshot:     info.AgentInfo.Snapshot,
			ManagedMode:  info.AgentInfo.Mode,
			Unprivileged: info.AgentInfo.Unprivileged,
		}))
	}

	if info.Services == nil {
		return nil, []Service{ServiceCheckin}, ErrV2Unavailable
	}
	cert, err := tls.X509KeyPair(info.PeerCert, info.PeerKey)
	if err != nil {
		return nil, nil, err
	}
	caCertPool := x509.NewCertPool()
	caCertPool.AppendCertsFromPEM(info.CaCert)
	trans := credentials.NewTLS(&tls.Config{
		ServerName:   info.ServerName,
		Certificates: []tls.Certificate{cert},
		RootCAs:      caCertPool,
	})
	for _, s := range info.Supports {
		if s == proto.ConnectionSupports_CheckinChunking {
			opts = append(opts, WithChunking(true))
		}
	}
	if info.MaxMessageSize > 0 {
		opts = append(opts, WithMaxMessageSize(int(info.MaxMessageSize)))
	}
	opts = append(opts, WithGRPCDialOptions(grpc.WithTransportCredentials(trans)))
	client := NewV2(
		info.Addr,
		info.Token,
		ver,
		opts...,
	)
	services := make([]Service, 0, len(info.Services))
	for _, srv := range info.Services {
		services = append(services, Service(srv))
	}
	return client, services, nil
}