repo
stringlengths 5
54
| path
stringlengths 4
155
| func_name
stringlengths 1
118
| original_string
stringlengths 52
85.5k
| language
stringclasses 1
value | code
stringlengths 52
85.5k
| code_tokens
sequence | docstring
stringlengths 6
2.61k
| docstring_tokens
sequence | sha
stringlengths 40
40
| url
stringlengths 85
252
| partition
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|
vmware/govmomi | toolbox/hgfs/server.go | SetattrV2 | func (s *Server) SetattrV2(p *Packet) (interface{}, error) {
res := &ReplySetattrV2{}
req := new(RequestSetattrV2)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
name := req.FileName.Path()
_, err = os.Stat(name)
if err != nil && os.IsNotExist(err) {
// assuming this is a virtual file
return res, nil
}
uid := -1
if req.Attr.Mask&AttrValidUserID == AttrValidUserID {
uid = int(req.Attr.UserID)
}
gid := -1
if req.Attr.Mask&AttrValidGroupID == AttrValidGroupID {
gid = int(req.Attr.GroupID)
}
err = s.chown(name, uid, gid)
if err != nil {
return nil, err
}
var perm os.FileMode
if req.Attr.Mask&AttrValidOwnerPerms == AttrValidOwnerPerms {
perm |= os.FileMode(req.Attr.OwnerPerms) << 6
}
if req.Attr.Mask&AttrValidGroupPerms == AttrValidGroupPerms {
perm |= os.FileMode(req.Attr.GroupPerms) << 3
}
if req.Attr.Mask&AttrValidOtherPerms == AttrValidOtherPerms {
perm |= os.FileMode(req.Attr.OtherPerms)
}
if perm != 0 {
err = s.chmod(name, perm)
if err != nil {
return nil, err
}
}
return res, nil
} | go | func (s *Server) SetattrV2(p *Packet) (interface{}, error) {
res := &ReplySetattrV2{}
req := new(RequestSetattrV2)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
name := req.FileName.Path()
_, err = os.Stat(name)
if err != nil && os.IsNotExist(err) {
// assuming this is a virtual file
return res, nil
}
uid := -1
if req.Attr.Mask&AttrValidUserID == AttrValidUserID {
uid = int(req.Attr.UserID)
}
gid := -1
if req.Attr.Mask&AttrValidGroupID == AttrValidGroupID {
gid = int(req.Attr.GroupID)
}
err = s.chown(name, uid, gid)
if err != nil {
return nil, err
}
var perm os.FileMode
if req.Attr.Mask&AttrValidOwnerPerms == AttrValidOwnerPerms {
perm |= os.FileMode(req.Attr.OwnerPerms) << 6
}
if req.Attr.Mask&AttrValidGroupPerms == AttrValidGroupPerms {
perm |= os.FileMode(req.Attr.GroupPerms) << 3
}
if req.Attr.Mask&AttrValidOtherPerms == AttrValidOtherPerms {
perm |= os.FileMode(req.Attr.OtherPerms)
}
if perm != 0 {
err = s.chmod(name, perm)
if err != nil {
return nil, err
}
}
return res, nil
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"SetattrV2",
"(",
"p",
"*",
"Packet",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"res",
":=",
"&",
"ReplySetattrV2",
"{",
"}",
"\n\n",
"req",
":=",
"new",
"(",
"RequestSetattrV2",
")",
"\n",
"err",
":=",
"UnmarshalBinary",
"(",
"p",
".",
"Payload",
",",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"name",
":=",
"req",
".",
"FileName",
".",
"Path",
"(",
")",
"\n\n",
"_",
",",
"err",
"=",
"os",
".",
"Stat",
"(",
"name",
")",
"\n",
"if",
"err",
"!=",
"nil",
"&&",
"os",
".",
"IsNotExist",
"(",
"err",
")",
"{",
"// assuming this is a virtual file",
"return",
"res",
",",
"nil",
"\n",
"}",
"\n\n",
"uid",
":=",
"-",
"1",
"\n",
"if",
"req",
".",
"Attr",
".",
"Mask",
"&",
"AttrValidUserID",
"==",
"AttrValidUserID",
"{",
"uid",
"=",
"int",
"(",
"req",
".",
"Attr",
".",
"UserID",
")",
"\n",
"}",
"\n\n",
"gid",
":=",
"-",
"1",
"\n",
"if",
"req",
".",
"Attr",
".",
"Mask",
"&",
"AttrValidGroupID",
"==",
"AttrValidGroupID",
"{",
"gid",
"=",
"int",
"(",
"req",
".",
"Attr",
".",
"GroupID",
")",
"\n",
"}",
"\n\n",
"err",
"=",
"s",
".",
"chown",
"(",
"name",
",",
"uid",
",",
"gid",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"var",
"perm",
"os",
".",
"FileMode",
"\n\n",
"if",
"req",
".",
"Attr",
".",
"Mask",
"&",
"AttrValidOwnerPerms",
"==",
"AttrValidOwnerPerms",
"{",
"perm",
"|=",
"os",
".",
"FileMode",
"(",
"req",
".",
"Attr",
".",
"OwnerPerms",
")",
"<<",
"6",
"\n",
"}",
"\n\n",
"if",
"req",
".",
"Attr",
".",
"Mask",
"&",
"AttrValidGroupPerms",
"==",
"AttrValidGroupPerms",
"{",
"perm",
"|=",
"os",
".",
"FileMode",
"(",
"req",
".",
"Attr",
".",
"GroupPerms",
")",
"<<",
"3",
"\n",
"}",
"\n\n",
"if",
"req",
".",
"Attr",
".",
"Mask",
"&",
"AttrValidOtherPerms",
"==",
"AttrValidOtherPerms",
"{",
"perm",
"|=",
"os",
".",
"FileMode",
"(",
"req",
".",
"Attr",
".",
"OtherPerms",
")",
"\n",
"}",
"\n\n",
"if",
"perm",
"!=",
"0",
"{",
"err",
"=",
"s",
".",
"chmod",
"(",
"name",
",",
"perm",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"res",
",",
"nil",
"\n",
"}"
] | // SetattrV2 handles OpSetattrV2 requests | [
"SetattrV2",
"handles",
"OpSetattrV2",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/hgfs/server.go#L348-L402 | train |
vmware/govmomi | toolbox/hgfs/server.go | Open | func (s *Server) Open(p *Packet) (interface{}, error) {
req := new(RequestOpen)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
name := req.FileName.Path()
mode := req.OpenMode
if mode != OpenModeReadOnly {
return nil, &Status{
Err: fmt.Errorf("open mode(%d) not supported for file %q", mode, name),
Code: StatusAccessDenied,
}
}
file, err := s.OpenFile(name, mode)
if err != nil {
return nil, err
}
res := &ReplyOpen{
Handle: s.newHandle(),
}
session.mu.Lock()
session.files[res.Handle] = file
session.mu.Unlock()
return res, nil
} | go | func (s *Server) Open(p *Packet) (interface{}, error) {
req := new(RequestOpen)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
name := req.FileName.Path()
mode := req.OpenMode
if mode != OpenModeReadOnly {
return nil, &Status{
Err: fmt.Errorf("open mode(%d) not supported for file %q", mode, name),
Code: StatusAccessDenied,
}
}
file, err := s.OpenFile(name, mode)
if err != nil {
return nil, err
}
res := &ReplyOpen{
Handle: s.newHandle(),
}
session.mu.Lock()
session.files[res.Handle] = file
session.mu.Unlock()
return res, nil
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"Open",
"(",
"p",
"*",
"Packet",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"req",
":=",
"new",
"(",
"RequestOpen",
")",
"\n",
"err",
":=",
"UnmarshalBinary",
"(",
"p",
".",
"Payload",
",",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
",",
"err",
":=",
"s",
".",
"getSession",
"(",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"name",
":=",
"req",
".",
"FileName",
".",
"Path",
"(",
")",
"\n",
"mode",
":=",
"req",
".",
"OpenMode",
"\n\n",
"if",
"mode",
"!=",
"OpenModeReadOnly",
"{",
"return",
"nil",
",",
"&",
"Status",
"{",
"Err",
":",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"mode",
",",
"name",
")",
",",
"Code",
":",
"StatusAccessDenied",
",",
"}",
"\n",
"}",
"\n\n",
"file",
",",
"err",
":=",
"s",
".",
"OpenFile",
"(",
"name",
",",
"mode",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"res",
":=",
"&",
"ReplyOpen",
"{",
"Handle",
":",
"s",
".",
"newHandle",
"(",
")",
",",
"}",
"\n\n",
"session",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"session",
".",
"files",
"[",
"res",
".",
"Handle",
"]",
"=",
"file",
"\n",
"session",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"res",
",",
"nil",
"\n",
"}"
] | // Open handles OpOpen requests | [
"Open",
"handles",
"OpOpen",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/hgfs/server.go#L409-L445 | train |
vmware/govmomi | toolbox/hgfs/server.go | Close | func (s *Server) Close(p *Packet) (interface{}, error) {
req := new(RequestClose)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
session.mu.Lock()
file, ok := session.files[req.Handle]
if ok {
delete(session.files, req.Handle)
}
session.mu.Unlock()
if ok {
err = file.Close()
} else {
return nil, &Status{Code: StatusInvalidHandle}
}
return &ReplyClose{}, err
} | go | func (s *Server) Close(p *Packet) (interface{}, error) {
req := new(RequestClose)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
session.mu.Lock()
file, ok := session.files[req.Handle]
if ok {
delete(session.files, req.Handle)
}
session.mu.Unlock()
if ok {
err = file.Close()
} else {
return nil, &Status{Code: StatusInvalidHandle}
}
return &ReplyClose{}, err
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"Close",
"(",
"p",
"*",
"Packet",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"req",
":=",
"new",
"(",
"RequestClose",
")",
"\n",
"err",
":=",
"UnmarshalBinary",
"(",
"p",
".",
"Payload",
",",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
",",
"err",
":=",
"s",
".",
"getSession",
"(",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"file",
",",
"ok",
":=",
"session",
".",
"files",
"[",
"req",
".",
"Handle",
"]",
"\n",
"if",
"ok",
"{",
"delete",
"(",
"session",
".",
"files",
",",
"req",
".",
"Handle",
")",
"\n",
"}",
"\n",
"session",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"ok",
"{",
"err",
"=",
"file",
".",
"Close",
"(",
")",
"\n",
"}",
"else",
"{",
"return",
"nil",
",",
"&",
"Status",
"{",
"Code",
":",
"StatusInvalidHandle",
"}",
"\n",
"}",
"\n\n",
"return",
"&",
"ReplyClose",
"{",
"}",
",",
"err",
"\n",
"}"
] | // Close handles OpClose requests | [
"Close",
"handles",
"OpClose",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/hgfs/server.go#L448-L474 | train |
vmware/govmomi | toolbox/hgfs/server.go | OpenV3 | func (s *Server) OpenV3(p *Packet) (interface{}, error) {
req := new(RequestOpenV3)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
name := req.FileName.Path()
if req.DesiredLock != LockNone {
return nil, &Status{
Err: fmt.Errorf("open lock type=%d not supported for file %q", req.DesiredLock, name),
Code: StatusOperationNotSupported,
}
}
file, err := s.OpenFile(name, req.OpenMode)
if err != nil {
return nil, err
}
res := &ReplyOpenV3{
Handle: s.newHandle(),
}
session.mu.Lock()
session.files[res.Handle] = file
session.mu.Unlock()
return res, nil
} | go | func (s *Server) OpenV3(p *Packet) (interface{}, error) {
req := new(RequestOpenV3)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
name := req.FileName.Path()
if req.DesiredLock != LockNone {
return nil, &Status{
Err: fmt.Errorf("open lock type=%d not supported for file %q", req.DesiredLock, name),
Code: StatusOperationNotSupported,
}
}
file, err := s.OpenFile(name, req.OpenMode)
if err != nil {
return nil, err
}
res := &ReplyOpenV3{
Handle: s.newHandle(),
}
session.mu.Lock()
session.files[res.Handle] = file
session.mu.Unlock()
return res, nil
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"OpenV3",
"(",
"p",
"*",
"Packet",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"req",
":=",
"new",
"(",
"RequestOpenV3",
")",
"\n",
"err",
":=",
"UnmarshalBinary",
"(",
"p",
".",
"Payload",
",",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
",",
"err",
":=",
"s",
".",
"getSession",
"(",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"name",
":=",
"req",
".",
"FileName",
".",
"Path",
"(",
")",
"\n\n",
"if",
"req",
".",
"DesiredLock",
"!=",
"LockNone",
"{",
"return",
"nil",
",",
"&",
"Status",
"{",
"Err",
":",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"req",
".",
"DesiredLock",
",",
"name",
")",
",",
"Code",
":",
"StatusOperationNotSupported",
",",
"}",
"\n",
"}",
"\n\n",
"file",
",",
"err",
":=",
"s",
".",
"OpenFile",
"(",
"name",
",",
"req",
".",
"OpenMode",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"res",
":=",
"&",
"ReplyOpenV3",
"{",
"Handle",
":",
"s",
".",
"newHandle",
"(",
")",
",",
"}",
"\n\n",
"session",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"session",
".",
"files",
"[",
"res",
".",
"Handle",
"]",
"=",
"file",
"\n",
"session",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"return",
"res",
",",
"nil",
"\n",
"}"
] | // OpenV3 handles OpOpenV3 requests | [
"OpenV3",
"handles",
"OpOpenV3",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/hgfs/server.go#L477-L512 | train |
vmware/govmomi | toolbox/hgfs/server.go | ReadV3 | func (s *Server) ReadV3(p *Packet) (interface{}, error) {
req := new(RequestReadV3)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
session.mu.Lock()
file, ok := session.files[req.Handle]
session.mu.Unlock()
if !ok {
return nil, &Status{Code: StatusInvalidHandle}
}
buf := make([]byte, req.RequiredSize)
// Use ReadFull as Read() of an archive io.Pipe may return much smaller chunks,
// such as when we've read a tar header.
n, err := io.ReadFull(file, buf)
if err != nil && n == 0 {
if err != io.EOF {
return nil, err
}
}
res := &ReplyReadV3{
ActualSize: uint32(n),
Payload: buf[:n],
}
return res, nil
} | go | func (s *Server) ReadV3(p *Packet) (interface{}, error) {
req := new(RequestReadV3)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
session.mu.Lock()
file, ok := session.files[req.Handle]
session.mu.Unlock()
if !ok {
return nil, &Status{Code: StatusInvalidHandle}
}
buf := make([]byte, req.RequiredSize)
// Use ReadFull as Read() of an archive io.Pipe may return much smaller chunks,
// such as when we've read a tar header.
n, err := io.ReadFull(file, buf)
if err != nil && n == 0 {
if err != io.EOF {
return nil, err
}
}
res := &ReplyReadV3{
ActualSize: uint32(n),
Payload: buf[:n],
}
return res, nil
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"ReadV3",
"(",
"p",
"*",
"Packet",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"req",
":=",
"new",
"(",
"RequestReadV3",
")",
"\n",
"err",
":=",
"UnmarshalBinary",
"(",
"p",
".",
"Payload",
",",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
",",
"err",
":=",
"s",
".",
"getSession",
"(",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"file",
",",
"ok",
":=",
"session",
".",
"files",
"[",
"req",
".",
"Handle",
"]",
"\n",
"session",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"&",
"Status",
"{",
"Code",
":",
"StatusInvalidHandle",
"}",
"\n",
"}",
"\n\n",
"buf",
":=",
"make",
"(",
"[",
"]",
"byte",
",",
"req",
".",
"RequiredSize",
")",
"\n\n",
"// Use ReadFull as Read() of an archive io.Pipe may return much smaller chunks,",
"// such as when we've read a tar header.",
"n",
",",
"err",
":=",
"io",
".",
"ReadFull",
"(",
"file",
",",
"buf",
")",
"\n",
"if",
"err",
"!=",
"nil",
"&&",
"n",
"==",
"0",
"{",
"if",
"err",
"!=",
"io",
".",
"EOF",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"res",
":=",
"&",
"ReplyReadV3",
"{",
"ActualSize",
":",
"uint32",
"(",
"n",
")",
",",
"Payload",
":",
"buf",
"[",
":",
"n",
"]",
",",
"}",
"\n\n",
"return",
"res",
",",
"nil",
"\n",
"}"
] | // ReadV3 handles OpReadV3 requests | [
"ReadV3",
"handles",
"OpReadV3",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/hgfs/server.go#L515-L552 | train |
vmware/govmomi | toolbox/hgfs/server.go | WriteV3 | func (s *Server) WriteV3(p *Packet) (interface{}, error) {
req := new(RequestWriteV3)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
session.mu.Lock()
file, ok := session.files[req.Handle]
session.mu.Unlock()
if !ok {
return nil, &Status{Code: StatusInvalidHandle}
}
n, err := file.Write(req.Payload)
if err != nil {
return nil, err
}
res := &ReplyWriteV3{
ActualSize: uint32(n),
}
return res, nil
} | go | func (s *Server) WriteV3(p *Packet) (interface{}, error) {
req := new(RequestWriteV3)
err := UnmarshalBinary(p.Payload, req)
if err != nil {
return nil, err
}
session, err := s.getSession(p)
if err != nil {
return nil, err
}
session.mu.Lock()
file, ok := session.files[req.Handle]
session.mu.Unlock()
if !ok {
return nil, &Status{Code: StatusInvalidHandle}
}
n, err := file.Write(req.Payload)
if err != nil {
return nil, err
}
res := &ReplyWriteV3{
ActualSize: uint32(n),
}
return res, nil
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"WriteV3",
"(",
"p",
"*",
"Packet",
")",
"(",
"interface",
"{",
"}",
",",
"error",
")",
"{",
"req",
":=",
"new",
"(",
"RequestWriteV3",
")",
"\n",
"err",
":=",
"UnmarshalBinary",
"(",
"p",
".",
"Payload",
",",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
",",
"err",
":=",
"s",
".",
"getSession",
"(",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"session",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"file",
",",
"ok",
":=",
"session",
".",
"files",
"[",
"req",
".",
"Handle",
"]",
"\n",
"session",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"!",
"ok",
"{",
"return",
"nil",
",",
"&",
"Status",
"{",
"Code",
":",
"StatusInvalidHandle",
"}",
"\n",
"}",
"\n\n",
"n",
",",
"err",
":=",
"file",
".",
"Write",
"(",
"req",
".",
"Payload",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"res",
":=",
"&",
"ReplyWriteV3",
"{",
"ActualSize",
":",
"uint32",
"(",
"n",
")",
",",
"}",
"\n\n",
"return",
"res",
",",
"nil",
"\n",
"}"
] | // WriteV3 handles OpWriteV3 requests | [
"WriteV3",
"handles",
"OpWriteV3",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/hgfs/server.go#L555-L585 | train |
vmware/govmomi | simulator/internal/server.go | NewServer | func NewServer(handler http.Handler) *Server {
ts := NewUnstartedServer(handler, "")
ts.Start()
return ts
} | go | func NewServer(handler http.Handler) *Server {
ts := NewUnstartedServer(handler, "")
ts.Start()
return ts
} | [
"func",
"NewServer",
"(",
"handler",
"http",
".",
"Handler",
")",
"*",
"Server",
"{",
"ts",
":=",
"NewUnstartedServer",
"(",
"handler",
",",
"\"",
"\"",
")",
"\n",
"ts",
".",
"Start",
"(",
")",
"\n",
"return",
"ts",
"\n",
"}"
] | // NewServer starts and returns a new Server.
// The caller should call Close when finished, to shut it down. | [
"NewServer",
"starts",
"and",
"returns",
"a",
"new",
"Server",
".",
"The",
"caller",
"should",
"call",
"Close",
"when",
"finished",
"to",
"shut",
"it",
"down",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L71-L75 | train |
vmware/govmomi | simulator/internal/server.go | NewUnstartedServer | func NewUnstartedServer(handler http.Handler, serve string) *Server {
return &Server{
Listener: newLocalListener(serve),
Config: &http.Server{Handler: handler},
}
} | go | func NewUnstartedServer(handler http.Handler, serve string) *Server {
return &Server{
Listener: newLocalListener(serve),
Config: &http.Server{Handler: handler},
}
} | [
"func",
"NewUnstartedServer",
"(",
"handler",
"http",
".",
"Handler",
",",
"serve",
"string",
")",
"*",
"Server",
"{",
"return",
"&",
"Server",
"{",
"Listener",
":",
"newLocalListener",
"(",
"serve",
")",
",",
"Config",
":",
"&",
"http",
".",
"Server",
"{",
"Handler",
":",
"handler",
"}",
",",
"}",
"\n",
"}"
] | // NewUnstartedServer returns a new Server but doesn't start it.
//
// After changing its configuration, the caller should call Start or
// StartTLS.
//
// The caller should call Close when finished, to shut it down.
// serve allows the server's listen address to be specified. | [
"NewUnstartedServer",
"returns",
"a",
"new",
"Server",
"but",
"doesn",
"t",
"start",
"it",
".",
"After",
"changing",
"its",
"configuration",
"the",
"caller",
"should",
"call",
"Start",
"or",
"StartTLS",
".",
"The",
"caller",
"should",
"call",
"Close",
"when",
"finished",
"to",
"shut",
"it",
"down",
".",
"serve",
"allows",
"the",
"server",
"s",
"listen",
"address",
"to",
"be",
"specified",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L84-L89 | train |
vmware/govmomi | simulator/internal/server.go | Start | func (s *Server) Start() {
if s.URL != "" {
panic("Server already started")
}
if s.client == nil {
s.client = &http.Client{Transport: &http.Transport{}}
}
s.URL = "http://" + s.Listener.Addr().String()
s.wrap()
s.goServe()
} | go | func (s *Server) Start() {
if s.URL != "" {
panic("Server already started")
}
if s.client == nil {
s.client = &http.Client{Transport: &http.Transport{}}
}
s.URL = "http://" + s.Listener.Addr().String()
s.wrap()
s.goServe()
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"Start",
"(",
")",
"{",
"if",
"s",
".",
"URL",
"!=",
"\"",
"\"",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"s",
".",
"client",
"==",
"nil",
"{",
"s",
".",
"client",
"=",
"&",
"http",
".",
"Client",
"{",
"Transport",
":",
"&",
"http",
".",
"Transport",
"{",
"}",
"}",
"\n",
"}",
"\n",
"s",
".",
"URL",
"=",
"\"",
"\"",
"+",
"s",
".",
"Listener",
".",
"Addr",
"(",
")",
".",
"String",
"(",
")",
"\n",
"s",
".",
"wrap",
"(",
")",
"\n",
"s",
".",
"goServe",
"(",
")",
"\n",
"}"
] | // Start starts a server from NewUnstartedServer. | [
"Start",
"starts",
"a",
"server",
"from",
"NewUnstartedServer",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L92-L102 | train |
vmware/govmomi | simulator/internal/server.go | StartTLS | func (s *Server) StartTLS() {
if s.URL != "" {
panic("Server already started")
}
if s.client == nil {
s.client = &http.Client{Transport: &http.Transport{}}
}
cert, err := tls.X509KeyPair(LocalhostCert, LocalhostKey)
if err != nil {
panic(fmt.Sprintf("httptest: NewTLSServer: %v", err))
}
existingConfig := s.TLS
if existingConfig != nil {
s.TLS = existingConfig.Clone()
} else {
s.TLS = new(tls.Config)
}
if s.TLS.NextProtos == nil {
s.TLS.NextProtos = []string{"http/1.1"}
}
if len(s.TLS.Certificates) == 0 {
s.TLS.Certificates = []tls.Certificate{cert}
}
s.certificate, err = x509.ParseCertificate(s.TLS.Certificates[0].Certificate[0])
if err != nil {
panic(fmt.Sprintf("httptest: NewTLSServer: %v", err))
}
certpool := x509.NewCertPool()
certpool.AddCert(s.certificate)
s.client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certpool,
},
}
s.Listener = tls.NewListener(s.Listener, s.TLS)
s.URL = "https://" + s.Listener.Addr().String()
s.wrap()
s.goServe()
} | go | func (s *Server) StartTLS() {
if s.URL != "" {
panic("Server already started")
}
if s.client == nil {
s.client = &http.Client{Transport: &http.Transport{}}
}
cert, err := tls.X509KeyPair(LocalhostCert, LocalhostKey)
if err != nil {
panic(fmt.Sprintf("httptest: NewTLSServer: %v", err))
}
existingConfig := s.TLS
if existingConfig != nil {
s.TLS = existingConfig.Clone()
} else {
s.TLS = new(tls.Config)
}
if s.TLS.NextProtos == nil {
s.TLS.NextProtos = []string{"http/1.1"}
}
if len(s.TLS.Certificates) == 0 {
s.TLS.Certificates = []tls.Certificate{cert}
}
s.certificate, err = x509.ParseCertificate(s.TLS.Certificates[0].Certificate[0])
if err != nil {
panic(fmt.Sprintf("httptest: NewTLSServer: %v", err))
}
certpool := x509.NewCertPool()
certpool.AddCert(s.certificate)
s.client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certpool,
},
}
s.Listener = tls.NewListener(s.Listener, s.TLS)
s.URL = "https://" + s.Listener.Addr().String()
s.wrap()
s.goServe()
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"StartTLS",
"(",
")",
"{",
"if",
"s",
".",
"URL",
"!=",
"\"",
"\"",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"s",
".",
"client",
"==",
"nil",
"{",
"s",
".",
"client",
"=",
"&",
"http",
".",
"Client",
"{",
"Transport",
":",
"&",
"http",
".",
"Transport",
"{",
"}",
"}",
"\n",
"}",
"\n",
"cert",
",",
"err",
":=",
"tls",
".",
"X509KeyPair",
"(",
"LocalhostCert",
",",
"LocalhostKey",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"err",
")",
")",
"\n",
"}",
"\n\n",
"existingConfig",
":=",
"s",
".",
"TLS",
"\n",
"if",
"existingConfig",
"!=",
"nil",
"{",
"s",
".",
"TLS",
"=",
"existingConfig",
".",
"Clone",
"(",
")",
"\n",
"}",
"else",
"{",
"s",
".",
"TLS",
"=",
"new",
"(",
"tls",
".",
"Config",
")",
"\n",
"}",
"\n",
"if",
"s",
".",
"TLS",
".",
"NextProtos",
"==",
"nil",
"{",
"s",
".",
"TLS",
".",
"NextProtos",
"=",
"[",
"]",
"string",
"{",
"\"",
"\"",
"}",
"\n",
"}",
"\n",
"if",
"len",
"(",
"s",
".",
"TLS",
".",
"Certificates",
")",
"==",
"0",
"{",
"s",
".",
"TLS",
".",
"Certificates",
"=",
"[",
"]",
"tls",
".",
"Certificate",
"{",
"cert",
"}",
"\n",
"}",
"\n",
"s",
".",
"certificate",
",",
"err",
"=",
"x509",
".",
"ParseCertificate",
"(",
"s",
".",
"TLS",
".",
"Certificates",
"[",
"0",
"]",
".",
"Certificate",
"[",
"0",
"]",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"panic",
"(",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"err",
")",
")",
"\n",
"}",
"\n",
"certpool",
":=",
"x509",
".",
"NewCertPool",
"(",
")",
"\n",
"certpool",
".",
"AddCert",
"(",
"s",
".",
"certificate",
")",
"\n",
"s",
".",
"client",
".",
"Transport",
"=",
"&",
"http",
".",
"Transport",
"{",
"TLSClientConfig",
":",
"&",
"tls",
".",
"Config",
"{",
"RootCAs",
":",
"certpool",
",",
"}",
",",
"}",
"\n",
"s",
".",
"Listener",
"=",
"tls",
".",
"NewListener",
"(",
"s",
".",
"Listener",
",",
"s",
".",
"TLS",
")",
"\n",
"s",
".",
"URL",
"=",
"\"",
"\"",
"+",
"s",
".",
"Listener",
".",
"Addr",
"(",
")",
".",
"String",
"(",
")",
"\n",
"s",
".",
"wrap",
"(",
")",
"\n",
"s",
".",
"goServe",
"(",
")",
"\n",
"}"
] | // StartTLS starts TLS on a server from NewUnstartedServer. | [
"StartTLS",
"starts",
"TLS",
"on",
"a",
"server",
"from",
"NewUnstartedServer",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L105-L144 | train |
vmware/govmomi | simulator/internal/server.go | NewTLSServer | func NewTLSServer(handler http.Handler) *Server {
ts := NewUnstartedServer(handler, "")
ts.StartTLS()
return ts
} | go | func NewTLSServer(handler http.Handler) *Server {
ts := NewUnstartedServer(handler, "")
ts.StartTLS()
return ts
} | [
"func",
"NewTLSServer",
"(",
"handler",
"http",
".",
"Handler",
")",
"*",
"Server",
"{",
"ts",
":=",
"NewUnstartedServer",
"(",
"handler",
",",
"\"",
"\"",
")",
"\n",
"ts",
".",
"StartTLS",
"(",
")",
"\n",
"return",
"ts",
"\n",
"}"
] | // NewTLSServer starts and returns a new Server using TLS.
// The caller should call Close when finished, to shut it down. | [
"NewTLSServer",
"starts",
"and",
"returns",
"a",
"new",
"Server",
"using",
"TLS",
".",
"The",
"caller",
"should",
"call",
"Close",
"when",
"finished",
"to",
"shut",
"it",
"down",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L148-L152 | train |
vmware/govmomi | simulator/internal/server.go | CloseClientConnections | func (s *Server) CloseClientConnections() {
s.mu.Lock()
nconn := len(s.conns)
ch := make(chan struct{}, nconn)
for c := range s.conns {
go s.closeConnChan(c, ch)
}
s.mu.Unlock()
// Wait for outstanding closes to finish.
//
// Out of paranoia for making a late change in Go 1.6, we
// bound how long this can wait, since golang.org/issue/14291
// isn't fully understood yet. At least this should only be used
// in tests.
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
for i := 0; i < nconn; i++ {
select {
case <-ch:
case <-timer.C:
// Too slow. Give up.
return
}
}
} | go | func (s *Server) CloseClientConnections() {
s.mu.Lock()
nconn := len(s.conns)
ch := make(chan struct{}, nconn)
for c := range s.conns {
go s.closeConnChan(c, ch)
}
s.mu.Unlock()
// Wait for outstanding closes to finish.
//
// Out of paranoia for making a late change in Go 1.6, we
// bound how long this can wait, since golang.org/issue/14291
// isn't fully understood yet. At least this should only be used
// in tests.
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
for i := 0; i < nconn; i++ {
select {
case <-ch:
case <-timer.C:
// Too slow. Give up.
return
}
}
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"CloseClientConnections",
"(",
")",
"{",
"s",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"nconn",
":=",
"len",
"(",
"s",
".",
"conns",
")",
"\n",
"ch",
":=",
"make",
"(",
"chan",
"struct",
"{",
"}",
",",
"nconn",
")",
"\n",
"for",
"c",
":=",
"range",
"s",
".",
"conns",
"{",
"go",
"s",
".",
"closeConnChan",
"(",
"c",
",",
"ch",
")",
"\n",
"}",
"\n",
"s",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n\n",
"// Wait for outstanding closes to finish.",
"//",
"// Out of paranoia for making a late change in Go 1.6, we",
"// bound how long this can wait, since golang.org/issue/14291",
"// isn't fully understood yet. At least this should only be used",
"// in tests.",
"timer",
":=",
"time",
".",
"NewTimer",
"(",
"5",
"*",
"time",
".",
"Second",
")",
"\n",
"defer",
"timer",
".",
"Stop",
"(",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"nconn",
";",
"i",
"++",
"{",
"select",
"{",
"case",
"<-",
"ch",
":",
"case",
"<-",
"timer",
".",
"C",
":",
"// Too slow. Give up.",
"return",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // CloseClientConnections closes any open HTTP connections to the test Server. | [
"CloseClientConnections",
"closes",
"any",
"open",
"HTTP",
"connections",
"to",
"the",
"test",
"Server",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L224-L249 | train |
vmware/govmomi | simulator/internal/server.go | wrap | func (s *Server) wrap() {
oldHook := s.Config.ConnState
s.Config.ConnState = func(c net.Conn, cs http.ConnState) {
s.mu.Lock()
defer s.mu.Unlock()
switch cs {
case http.StateNew:
s.wg.Add(1)
if _, exists := s.conns[c]; exists {
panic("invalid state transition")
}
if s.conns == nil {
s.conns = make(map[net.Conn]http.ConnState)
}
s.conns[c] = cs
if s.closed {
// Probably just a socket-late-binding dial from
// the default transport that lost the race (and
// thus this connection is now idle and will
// never be used).
s.closeConn(c)
}
case http.StateActive:
if oldState, ok := s.conns[c]; ok {
if oldState != http.StateNew && oldState != http.StateIdle {
panic("invalid state transition")
}
s.conns[c] = cs
}
case http.StateIdle:
if oldState, ok := s.conns[c]; ok {
if oldState != http.StateActive {
panic("invalid state transition")
}
s.conns[c] = cs
}
if s.closed {
s.closeConn(c)
}
case http.StateHijacked, http.StateClosed:
s.forgetConn(c)
}
if oldHook != nil {
oldHook(c, cs)
}
}
} | go | func (s *Server) wrap() {
oldHook := s.Config.ConnState
s.Config.ConnState = func(c net.Conn, cs http.ConnState) {
s.mu.Lock()
defer s.mu.Unlock()
switch cs {
case http.StateNew:
s.wg.Add(1)
if _, exists := s.conns[c]; exists {
panic("invalid state transition")
}
if s.conns == nil {
s.conns = make(map[net.Conn]http.ConnState)
}
s.conns[c] = cs
if s.closed {
// Probably just a socket-late-binding dial from
// the default transport that lost the race (and
// thus this connection is now idle and will
// never be used).
s.closeConn(c)
}
case http.StateActive:
if oldState, ok := s.conns[c]; ok {
if oldState != http.StateNew && oldState != http.StateIdle {
panic("invalid state transition")
}
s.conns[c] = cs
}
case http.StateIdle:
if oldState, ok := s.conns[c]; ok {
if oldState != http.StateActive {
panic("invalid state transition")
}
s.conns[c] = cs
}
if s.closed {
s.closeConn(c)
}
case http.StateHijacked, http.StateClosed:
s.forgetConn(c)
}
if oldHook != nil {
oldHook(c, cs)
}
}
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"wrap",
"(",
")",
"{",
"oldHook",
":=",
"s",
".",
"Config",
".",
"ConnState",
"\n",
"s",
".",
"Config",
".",
"ConnState",
"=",
"func",
"(",
"c",
"net",
".",
"Conn",
",",
"cs",
"http",
".",
"ConnState",
")",
"{",
"s",
".",
"mu",
".",
"Lock",
"(",
")",
"\n",
"defer",
"s",
".",
"mu",
".",
"Unlock",
"(",
")",
"\n",
"switch",
"cs",
"{",
"case",
"http",
".",
"StateNew",
":",
"s",
".",
"wg",
".",
"Add",
"(",
"1",
")",
"\n",
"if",
"_",
",",
"exists",
":=",
"s",
".",
"conns",
"[",
"c",
"]",
";",
"exists",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"if",
"s",
".",
"conns",
"==",
"nil",
"{",
"s",
".",
"conns",
"=",
"make",
"(",
"map",
"[",
"net",
".",
"Conn",
"]",
"http",
".",
"ConnState",
")",
"\n",
"}",
"\n",
"s",
".",
"conns",
"[",
"c",
"]",
"=",
"cs",
"\n",
"if",
"s",
".",
"closed",
"{",
"// Probably just a socket-late-binding dial from",
"// the default transport that lost the race (and",
"// thus this connection is now idle and will",
"// never be used).",
"s",
".",
"closeConn",
"(",
"c",
")",
"\n",
"}",
"\n",
"case",
"http",
".",
"StateActive",
":",
"if",
"oldState",
",",
"ok",
":=",
"s",
".",
"conns",
"[",
"c",
"]",
";",
"ok",
"{",
"if",
"oldState",
"!=",
"http",
".",
"StateNew",
"&&",
"oldState",
"!=",
"http",
".",
"StateIdle",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"s",
".",
"conns",
"[",
"c",
"]",
"=",
"cs",
"\n",
"}",
"\n",
"case",
"http",
".",
"StateIdle",
":",
"if",
"oldState",
",",
"ok",
":=",
"s",
".",
"conns",
"[",
"c",
"]",
";",
"ok",
"{",
"if",
"oldState",
"!=",
"http",
".",
"StateActive",
"{",
"panic",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"s",
".",
"conns",
"[",
"c",
"]",
"=",
"cs",
"\n",
"}",
"\n",
"if",
"s",
".",
"closed",
"{",
"s",
".",
"closeConn",
"(",
"c",
")",
"\n",
"}",
"\n",
"case",
"http",
".",
"StateHijacked",
",",
"http",
".",
"StateClosed",
":",
"s",
".",
"forgetConn",
"(",
"c",
")",
"\n",
"}",
"\n",
"if",
"oldHook",
"!=",
"nil",
"{",
"oldHook",
"(",
"c",
",",
"cs",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // wrap installs the connection state-tracking hook to know which
// connections are idle. | [
"wrap",
"installs",
"the",
"connection",
"state",
"-",
"tracking",
"hook",
"to",
"know",
"which",
"connections",
"are",
"idle",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L274-L320 | train |
vmware/govmomi | simulator/internal/server.go | closeConnChan | func (s *Server) closeConnChan(c net.Conn, done chan<- struct{}) {
c.Close()
if done != nil {
done <- struct{}{}
}
} | go | func (s *Server) closeConnChan(c net.Conn, done chan<- struct{}) {
c.Close()
if done != nil {
done <- struct{}{}
}
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"closeConnChan",
"(",
"c",
"net",
".",
"Conn",
",",
"done",
"chan",
"<-",
"struct",
"{",
"}",
")",
"{",
"c",
".",
"Close",
"(",
")",
"\n",
"if",
"done",
"!=",
"nil",
"{",
"done",
"<-",
"struct",
"{",
"}",
"{",
"}",
"\n",
"}",
"\n",
"}"
] | // closeConnChan is like closeConn, but takes an optional channel to receive a value
// when the goroutine closing c is done. | [
"closeConnChan",
"is",
"like",
"closeConn",
"but",
"takes",
"an",
"optional",
"channel",
"to",
"receive",
"a",
"value",
"when",
"the",
"goroutine",
"closing",
"c",
"is",
"done",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L328-L333 | train |
vmware/govmomi | simulator/internal/server.go | forgetConn | func (s *Server) forgetConn(c net.Conn) {
if _, ok := s.conns[c]; ok {
delete(s.conns, c)
s.wg.Done()
}
} | go | func (s *Server) forgetConn(c net.Conn) {
if _, ok := s.conns[c]; ok {
delete(s.conns, c)
s.wg.Done()
}
} | [
"func",
"(",
"s",
"*",
"Server",
")",
"forgetConn",
"(",
"c",
"net",
".",
"Conn",
")",
"{",
"if",
"_",
",",
"ok",
":=",
"s",
".",
"conns",
"[",
"c",
"]",
";",
"ok",
"{",
"delete",
"(",
"s",
".",
"conns",
",",
"c",
")",
"\n",
"s",
".",
"wg",
".",
"Done",
"(",
")",
"\n",
"}",
"\n",
"}"
] | // forgetConn removes c from the set of tracked conns and decrements it from the
// waitgroup, unless it was previously removed.
// s.mu must be held. | [
"forgetConn",
"removes",
"c",
"from",
"the",
"set",
"of",
"tracked",
"conns",
"and",
"decrements",
"it",
"from",
"the",
"waitgroup",
"unless",
"it",
"was",
"previously",
"removed",
".",
"s",
".",
"mu",
"must",
"be",
"held",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/internal/server.go#L338-L343 | train |
vmware/govmomi | simulator/property_filter.go | matches | func (f *PropertyFilter) matches(ctx *Context, ref types.ManagedObjectReference, change *types.PropertyChange) bool {
var kind reflect.Type
for _, p := range f.Spec.PropSet {
if p.Type != ref.Type {
if kind == nil {
kind = getManagedObject(ctx.Map.Get(ref)).Type()
}
// e.g. ManagedEntity, ComputeResource
field, ok := kind.FieldByName(p.Type)
if !(ok && field.Anonymous) {
continue
}
}
if isTrue(p.All) {
return true
}
for _, name := range p.PathSet {
if name == change.Name {
return true
}
// strings.HasPrefix("runtime.powerState", "runtime") == parent field matches
if strings.HasPrefix(change.Name, name) {
if obj := ctx.Map.Get(ref); obj != nil { // object may have since been deleted
change.Name = name
change.Val, _ = fieldValue(reflect.ValueOf(obj), name)
}
return true
}
}
}
return false
} | go | func (f *PropertyFilter) matches(ctx *Context, ref types.ManagedObjectReference, change *types.PropertyChange) bool {
var kind reflect.Type
for _, p := range f.Spec.PropSet {
if p.Type != ref.Type {
if kind == nil {
kind = getManagedObject(ctx.Map.Get(ref)).Type()
}
// e.g. ManagedEntity, ComputeResource
field, ok := kind.FieldByName(p.Type)
if !(ok && field.Anonymous) {
continue
}
}
if isTrue(p.All) {
return true
}
for _, name := range p.PathSet {
if name == change.Name {
return true
}
// strings.HasPrefix("runtime.powerState", "runtime") == parent field matches
if strings.HasPrefix(change.Name, name) {
if obj := ctx.Map.Get(ref); obj != nil { // object may have since been deleted
change.Name = name
change.Val, _ = fieldValue(reflect.ValueOf(obj), name)
}
return true
}
}
}
return false
} | [
"func",
"(",
"f",
"*",
"PropertyFilter",
")",
"matches",
"(",
"ctx",
"*",
"Context",
",",
"ref",
"types",
".",
"ManagedObjectReference",
",",
"change",
"*",
"types",
".",
"PropertyChange",
")",
"bool",
"{",
"var",
"kind",
"reflect",
".",
"Type",
"\n\n",
"for",
"_",
",",
"p",
":=",
"range",
"f",
".",
"Spec",
".",
"PropSet",
"{",
"if",
"p",
".",
"Type",
"!=",
"ref",
".",
"Type",
"{",
"if",
"kind",
"==",
"nil",
"{",
"kind",
"=",
"getManagedObject",
"(",
"ctx",
".",
"Map",
".",
"Get",
"(",
"ref",
")",
")",
".",
"Type",
"(",
")",
"\n",
"}",
"\n",
"// e.g. ManagedEntity, ComputeResource",
"field",
",",
"ok",
":=",
"kind",
".",
"FieldByName",
"(",
"p",
".",
"Type",
")",
"\n",
"if",
"!",
"(",
"ok",
"&&",
"field",
".",
"Anonymous",
")",
"{",
"continue",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"isTrue",
"(",
"p",
".",
"All",
")",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"for",
"_",
",",
"name",
":=",
"range",
"p",
".",
"PathSet",
"{",
"if",
"name",
"==",
"change",
".",
"Name",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"// strings.HasPrefix(\"runtime.powerState\", \"runtime\") == parent field matches",
"if",
"strings",
".",
"HasPrefix",
"(",
"change",
".",
"Name",
",",
"name",
")",
"{",
"if",
"obj",
":=",
"ctx",
".",
"Map",
".",
"Get",
"(",
"ref",
")",
";",
"obj",
"!=",
"nil",
"{",
"// object may have since been deleted",
"change",
".",
"Name",
"=",
"name",
"\n",
"change",
".",
"Val",
",",
"_",
"=",
"fieldValue",
"(",
"reflect",
".",
"ValueOf",
"(",
"obj",
")",
",",
"name",
")",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // matches returns true if the change matches one of the filter Spec.PropSet | [
"matches",
"returns",
"true",
"if",
"the",
"change",
"matches",
"one",
"of",
"the",
"filter",
"Spec",
".",
"PropSet"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/property_filter.go#L49-L86 | train |
vmware/govmomi | simulator/property_filter.go | apply | func (f *PropertyFilter) apply(ctx *Context, change types.ObjectUpdate) types.ObjectUpdate {
parents := make(map[string]bool)
set := change.ChangeSet
change.ChangeSet = nil
for i, p := range set {
if f.matches(ctx, change.Obj, &p) {
if p.Name != set[i].Name {
// update matches a parent field from the spec.
if parents[p.Name] {
continue // only return 1 instance of the parent
}
parents[p.Name] = true
}
change.ChangeSet = append(change.ChangeSet, p)
}
}
return change
} | go | func (f *PropertyFilter) apply(ctx *Context, change types.ObjectUpdate) types.ObjectUpdate {
parents := make(map[string]bool)
set := change.ChangeSet
change.ChangeSet = nil
for i, p := range set {
if f.matches(ctx, change.Obj, &p) {
if p.Name != set[i].Name {
// update matches a parent field from the spec.
if parents[p.Name] {
continue // only return 1 instance of the parent
}
parents[p.Name] = true
}
change.ChangeSet = append(change.ChangeSet, p)
}
}
return change
} | [
"func",
"(",
"f",
"*",
"PropertyFilter",
")",
"apply",
"(",
"ctx",
"*",
"Context",
",",
"change",
"types",
".",
"ObjectUpdate",
")",
"types",
".",
"ObjectUpdate",
"{",
"parents",
":=",
"make",
"(",
"map",
"[",
"string",
"]",
"bool",
")",
"\n",
"set",
":=",
"change",
".",
"ChangeSet",
"\n",
"change",
".",
"ChangeSet",
"=",
"nil",
"\n\n",
"for",
"i",
",",
"p",
":=",
"range",
"set",
"{",
"if",
"f",
".",
"matches",
"(",
"ctx",
",",
"change",
".",
"Obj",
",",
"&",
"p",
")",
"{",
"if",
"p",
".",
"Name",
"!=",
"set",
"[",
"i",
"]",
".",
"Name",
"{",
"// update matches a parent field from the spec.",
"if",
"parents",
"[",
"p",
".",
"Name",
"]",
"{",
"continue",
"// only return 1 instance of the parent",
"\n",
"}",
"\n",
"parents",
"[",
"p",
".",
"Name",
"]",
"=",
"true",
"\n",
"}",
"\n",
"change",
".",
"ChangeSet",
"=",
"append",
"(",
"change",
".",
"ChangeSet",
",",
"p",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"change",
"\n",
"}"
] | // apply the PropertyFilter.Spec to the given ObjectUpdate | [
"apply",
"the",
"PropertyFilter",
".",
"Spec",
"to",
"the",
"given",
"ObjectUpdate"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/property_filter.go#L89-L108 | train |
vmware/govmomi | vim25/xml/typeinfo.go | getTypeInfo | func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
tinfoLock.RLock()
tinfo, ok := tinfoMap[typ]
tinfoLock.RUnlock()
if ok {
return tinfo, nil
}
tinfo = &typeInfo{}
if typ.Kind() == reflect.Struct && typ != nameType {
n := typ.NumField()
for i := 0; i < n; i++ {
f := typ.Field(i)
if f.PkgPath != "" || f.Tag.Get("xml") == "-" {
continue // Private field
}
// For embedded structs, embed its fields.
if f.Anonymous {
t := f.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Struct {
inner, err := getTypeInfo(t)
if err != nil {
return nil, err
}
if tinfo.xmlname == nil {
tinfo.xmlname = inner.xmlname
}
for _, finfo := range inner.fields {
finfo.idx = append([]int{i}, finfo.idx...)
if err := addFieldInfo(typ, tinfo, &finfo); err != nil {
return nil, err
}
}
continue
}
}
finfo, err := structFieldInfo(typ, &f)
if err != nil {
return nil, err
}
if f.Name == "XMLName" {
tinfo.xmlname = finfo
continue
}
// Add the field if it doesn't conflict with other fields.
if err := addFieldInfo(typ, tinfo, finfo); err != nil {
return nil, err
}
}
}
tinfoLock.Lock()
tinfoMap[typ] = tinfo
tinfoLock.Unlock()
return tinfo, nil
} | go | func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
tinfoLock.RLock()
tinfo, ok := tinfoMap[typ]
tinfoLock.RUnlock()
if ok {
return tinfo, nil
}
tinfo = &typeInfo{}
if typ.Kind() == reflect.Struct && typ != nameType {
n := typ.NumField()
for i := 0; i < n; i++ {
f := typ.Field(i)
if f.PkgPath != "" || f.Tag.Get("xml") == "-" {
continue // Private field
}
// For embedded structs, embed its fields.
if f.Anonymous {
t := f.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Struct {
inner, err := getTypeInfo(t)
if err != nil {
return nil, err
}
if tinfo.xmlname == nil {
tinfo.xmlname = inner.xmlname
}
for _, finfo := range inner.fields {
finfo.idx = append([]int{i}, finfo.idx...)
if err := addFieldInfo(typ, tinfo, &finfo); err != nil {
return nil, err
}
}
continue
}
}
finfo, err := structFieldInfo(typ, &f)
if err != nil {
return nil, err
}
if f.Name == "XMLName" {
tinfo.xmlname = finfo
continue
}
// Add the field if it doesn't conflict with other fields.
if err := addFieldInfo(typ, tinfo, finfo); err != nil {
return nil, err
}
}
}
tinfoLock.Lock()
tinfoMap[typ] = tinfo
tinfoLock.Unlock()
return tinfo, nil
} | [
"func",
"getTypeInfo",
"(",
"typ",
"reflect",
".",
"Type",
")",
"(",
"*",
"typeInfo",
",",
"error",
")",
"{",
"tinfoLock",
".",
"RLock",
"(",
")",
"\n",
"tinfo",
",",
"ok",
":=",
"tinfoMap",
"[",
"typ",
"]",
"\n",
"tinfoLock",
".",
"RUnlock",
"(",
")",
"\n",
"if",
"ok",
"{",
"return",
"tinfo",
",",
"nil",
"\n",
"}",
"\n",
"tinfo",
"=",
"&",
"typeInfo",
"{",
"}",
"\n",
"if",
"typ",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Struct",
"&&",
"typ",
"!=",
"nameType",
"{",
"n",
":=",
"typ",
".",
"NumField",
"(",
")",
"\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
"{",
"f",
":=",
"typ",
".",
"Field",
"(",
"i",
")",
"\n",
"if",
"f",
".",
"PkgPath",
"!=",
"\"",
"\"",
"||",
"f",
".",
"Tag",
".",
"Get",
"(",
"\"",
"\"",
")",
"==",
"\"",
"\"",
"{",
"continue",
"// Private field",
"\n",
"}",
"\n\n",
"// For embedded structs, embed its fields.",
"if",
"f",
".",
"Anonymous",
"{",
"t",
":=",
"f",
".",
"Type",
"\n",
"if",
"t",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"t",
"=",
"t",
".",
"Elem",
"(",
")",
"\n",
"}",
"\n",
"if",
"t",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Struct",
"{",
"inner",
",",
"err",
":=",
"getTypeInfo",
"(",
"t",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"tinfo",
".",
"xmlname",
"==",
"nil",
"{",
"tinfo",
".",
"xmlname",
"=",
"inner",
".",
"xmlname",
"\n",
"}",
"\n",
"for",
"_",
",",
"finfo",
":=",
"range",
"inner",
".",
"fields",
"{",
"finfo",
".",
"idx",
"=",
"append",
"(",
"[",
"]",
"int",
"{",
"i",
"}",
",",
"finfo",
".",
"idx",
"...",
")",
"\n",
"if",
"err",
":=",
"addFieldInfo",
"(",
"typ",
",",
"tinfo",
",",
"&",
"finfo",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"continue",
"\n",
"}",
"\n",
"}",
"\n\n",
"finfo",
",",
"err",
":=",
"structFieldInfo",
"(",
"typ",
",",
"&",
"f",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"f",
".",
"Name",
"==",
"\"",
"\"",
"{",
"tinfo",
".",
"xmlname",
"=",
"finfo",
"\n",
"continue",
"\n",
"}",
"\n\n",
"// Add the field if it doesn't conflict with other fields.",
"if",
"err",
":=",
"addFieldInfo",
"(",
"typ",
",",
"tinfo",
",",
"finfo",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"tinfoLock",
".",
"Lock",
"(",
")",
"\n",
"tinfoMap",
"[",
"typ",
"]",
"=",
"tinfo",
"\n",
"tinfoLock",
".",
"Unlock",
"(",
")",
"\n",
"return",
"tinfo",
",",
"nil",
"\n",
"}"
] | // getTypeInfo returns the typeInfo structure with details necessary
// for marshalling and unmarshalling typ. | [
"getTypeInfo",
"returns",
"the",
"typeInfo",
"structure",
"with",
"details",
"necessary",
"for",
"marshalling",
"and",
"unmarshalling",
"typ",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/typeinfo.go#L52-L112 | train |
vmware/govmomi | vim25/xml/typeinfo.go | lookupXMLName | func lookupXMLName(typ reflect.Type) (xmlname *fieldInfo) {
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return nil
}
for i, n := 0, typ.NumField(); i < n; i++ {
f := typ.Field(i)
if f.Name != "XMLName" {
continue
}
finfo, err := structFieldInfo(typ, &f)
if finfo.name != "" && err == nil {
return finfo
}
// Also consider errors as a non-existent field tag
// and let getTypeInfo itself report the error.
break
}
return nil
} | go | func lookupXMLName(typ reflect.Type) (xmlname *fieldInfo) {
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return nil
}
for i, n := 0, typ.NumField(); i < n; i++ {
f := typ.Field(i)
if f.Name != "XMLName" {
continue
}
finfo, err := structFieldInfo(typ, &f)
if finfo.name != "" && err == nil {
return finfo
}
// Also consider errors as a non-existent field tag
// and let getTypeInfo itself report the error.
break
}
return nil
} | [
"func",
"lookupXMLName",
"(",
"typ",
"reflect",
".",
"Type",
")",
"(",
"xmlname",
"*",
"fieldInfo",
")",
"{",
"for",
"typ",
".",
"Kind",
"(",
")",
"==",
"reflect",
".",
"Ptr",
"{",
"typ",
"=",
"typ",
".",
"Elem",
"(",
")",
"\n",
"}",
"\n",
"if",
"typ",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Struct",
"{",
"return",
"nil",
"\n",
"}",
"\n",
"for",
"i",
",",
"n",
":=",
"0",
",",
"typ",
".",
"NumField",
"(",
")",
";",
"i",
"<",
"n",
";",
"i",
"++",
"{",
"f",
":=",
"typ",
".",
"Field",
"(",
"i",
")",
"\n",
"if",
"f",
".",
"Name",
"!=",
"\"",
"\"",
"{",
"continue",
"\n",
"}",
"\n",
"finfo",
",",
"err",
":=",
"structFieldInfo",
"(",
"typ",
",",
"&",
"f",
")",
"\n",
"if",
"finfo",
".",
"name",
"!=",
"\"",
"\"",
"&&",
"err",
"==",
"nil",
"{",
"return",
"finfo",
"\n",
"}",
"\n",
"// Also consider errors as a non-existent field tag",
"// and let getTypeInfo itself report the error.",
"break",
"\n",
"}",
"\n",
"return",
"nil",
"\n",
"}"
] | // lookupXMLName returns the fieldInfo for typ's XMLName field
// in case it exists and has a valid xml field tag, otherwise
// it returns nil. | [
"lookupXMLName",
"returns",
"the",
"fieldInfo",
"for",
"typ",
"s",
"XMLName",
"field",
"in",
"case",
"it",
"exists",
"and",
"has",
"a",
"valid",
"xml",
"field",
"tag",
"otherwise",
"it",
"returns",
"nil",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/typeinfo.go#L233-L254 | train |
vmware/govmomi | vapi/library/library_file.go | ListLibraryItemFiles | func (c *Manager) ListLibraryItemFiles(ctx context.Context, id string) ([]File, error) {
url := internal.URL(c, internal.LibraryItemFilePath).WithParameter("library_item_id", id)
var res []File
return res, c.Do(ctx, url.Request(http.MethodGet), &res)
} | go | func (c *Manager) ListLibraryItemFiles(ctx context.Context, id string) ([]File, error) {
url := internal.URL(c, internal.LibraryItemFilePath).WithParameter("library_item_id", id)
var res []File
return res, c.Do(ctx, url.Request(http.MethodGet), &res)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"ListLibraryItemFiles",
"(",
"ctx",
"context",
".",
"Context",
",",
"id",
"string",
")",
"(",
"[",
"]",
"File",
",",
"error",
")",
"{",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"LibraryItemFilePath",
")",
".",
"WithParameter",
"(",
"\"",
"\"",
",",
"id",
")",
"\n",
"var",
"res",
"[",
"]",
"File",
"\n",
"return",
"res",
",",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodGet",
")",
",",
"&",
"res",
")",
"\n",
"}"
] | // ListLibraryItemFiles returns a list of all the files for a library item. | [
"ListLibraryItemFiles",
"returns",
"a",
"list",
"of",
"all",
"the",
"files",
"for",
"a",
"library",
"item",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/library/library_file.go#L42-L46 | train |
vmware/govmomi | vapi/library/library_file.go | GetLibraryItemFile | func (c *Manager) GetLibraryItemFile(ctx context.Context, id, fileName string) (*File, error) {
url := internal.URL(c, internal.LibraryItemFilePath).WithID(id).WithAction("get")
spec := struct {
Name string `json:"name"`
}{fileName}
var res File
return &res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | go | func (c *Manager) GetLibraryItemFile(ctx context.Context, id, fileName string) (*File, error) {
url := internal.URL(c, internal.LibraryItemFilePath).WithID(id).WithAction("get")
spec := struct {
Name string `json:"name"`
}{fileName}
var res File
return &res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"GetLibraryItemFile",
"(",
"ctx",
"context",
".",
"Context",
",",
"id",
",",
"fileName",
"string",
")",
"(",
"*",
"File",
",",
"error",
")",
"{",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"LibraryItemFilePath",
")",
".",
"WithID",
"(",
"id",
")",
".",
"WithAction",
"(",
"\"",
"\"",
")",
"\n",
"spec",
":=",
"struct",
"{",
"Name",
"string",
"`json:\"name\"`",
"\n",
"}",
"{",
"fileName",
"}",
"\n",
"var",
"res",
"File",
"\n",
"return",
"&",
"res",
",",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodPost",
",",
"spec",
")",
",",
"&",
"res",
")",
"\n",
"}"
] | // GetLibraryItemFile returns a file with the provided name for a library item. | [
"GetLibraryItemFile",
"returns",
"a",
"file",
"with",
"the",
"provided",
"name",
"for",
"a",
"library",
"item",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/library/library_file.go#L49-L56 | train |
vmware/govmomi | nfc/lease_updater.go | File | func (o FileItem) File() types.OvfFile {
return types.OvfFile{
DeviceId: o.DeviceId,
Path: o.Path,
Size: o.Size,
}
} | go | func (o FileItem) File() types.OvfFile {
return types.OvfFile{
DeviceId: o.DeviceId,
Path: o.Path,
Size: o.Size,
}
} | [
"func",
"(",
"o",
"FileItem",
")",
"File",
"(",
")",
"types",
".",
"OvfFile",
"{",
"return",
"types",
".",
"OvfFile",
"{",
"DeviceId",
":",
"o",
".",
"DeviceId",
",",
"Path",
":",
"o",
".",
"Path",
",",
"Size",
":",
"o",
".",
"Size",
",",
"}",
"\n",
"}"
] | // File converts the FileItem.OvfFileItem to an OvfFile | [
"File",
"converts",
"the",
"FileItem",
".",
"OvfFileItem",
"to",
"an",
"OvfFile"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/nfc/lease_updater.go#L51-L57 | train |
vmware/govmomi | govc/datastore/ls.go | hasMultiplePaths | func (o *listOutput) hasMultiplePaths() bool {
if len(o.rs) == 0 {
return false
}
p := o.rs[0].FolderPath
// Multiple paths if any entry is not equal to the first one.
for _, e := range o.rs {
if e.FolderPath != p {
return true
}
}
return false
} | go | func (o *listOutput) hasMultiplePaths() bool {
if len(o.rs) == 0 {
return false
}
p := o.rs[0].FolderPath
// Multiple paths if any entry is not equal to the first one.
for _, e := range o.rs {
if e.FolderPath != p {
return true
}
}
return false
} | [
"func",
"(",
"o",
"*",
"listOutput",
")",
"hasMultiplePaths",
"(",
")",
"bool",
"{",
"if",
"len",
"(",
"o",
".",
"rs",
")",
"==",
"0",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"p",
":=",
"o",
".",
"rs",
"[",
"0",
"]",
".",
"FolderPath",
"\n\n",
"// Multiple paths if any entry is not equal to the first one.",
"for",
"_",
",",
"e",
":=",
"range",
"o",
".",
"rs",
"{",
"if",
"e",
".",
"FolderPath",
"!=",
"p",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // hasMultiplePaths returns whether or not the slice of search results contains
// results from more than one folder path. | [
"hasMultiplePaths",
"returns",
"whether",
"or",
"not",
"the",
"slice",
"of",
"search",
"results",
"contains",
"results",
"from",
"more",
"than",
"one",
"folder",
"path",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/datastore/ls.go#L233-L248 | train |
vmware/govmomi | vapi/tags/categories.go | Patch | func (c *Category) Patch(src *Category) {
if src.Name != "" {
c.Name = src.Name
}
if src.Description != "" {
c.Description = src.Description
}
if src.Cardinality != "" {
c.Cardinality = src.Cardinality
}
// Note that in order to append to AssociableTypes any existing types must be included in their original order.
for _, kind := range src.AssociableTypes {
if !c.hasType(kind) {
c.AssociableTypes = append(c.AssociableTypes, kind)
}
}
} | go | func (c *Category) Patch(src *Category) {
if src.Name != "" {
c.Name = src.Name
}
if src.Description != "" {
c.Description = src.Description
}
if src.Cardinality != "" {
c.Cardinality = src.Cardinality
}
// Note that in order to append to AssociableTypes any existing types must be included in their original order.
for _, kind := range src.AssociableTypes {
if !c.hasType(kind) {
c.AssociableTypes = append(c.AssociableTypes, kind)
}
}
} | [
"func",
"(",
"c",
"*",
"Category",
")",
"Patch",
"(",
"src",
"*",
"Category",
")",
"{",
"if",
"src",
".",
"Name",
"!=",
"\"",
"\"",
"{",
"c",
".",
"Name",
"=",
"src",
".",
"Name",
"\n",
"}",
"\n",
"if",
"src",
".",
"Description",
"!=",
"\"",
"\"",
"{",
"c",
".",
"Description",
"=",
"src",
".",
"Description",
"\n",
"}",
"\n",
"if",
"src",
".",
"Cardinality",
"!=",
"\"",
"\"",
"{",
"c",
".",
"Cardinality",
"=",
"src",
".",
"Cardinality",
"\n",
"}",
"\n",
"// Note that in order to append to AssociableTypes any existing types must be included in their original order.",
"for",
"_",
",",
"kind",
":=",
"range",
"src",
".",
"AssociableTypes",
"{",
"if",
"!",
"c",
".",
"hasType",
"(",
"kind",
")",
"{",
"c",
".",
"AssociableTypes",
"=",
"append",
"(",
"c",
".",
"AssociableTypes",
",",
"kind",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // Patch merges Category changes from the given src.
// AssociableTypes can only be appended to and cannot shrink. | [
"Patch",
"merges",
"Category",
"changes",
"from",
"the",
"given",
"src",
".",
"AssociableTypes",
"can",
"only",
"be",
"appended",
"to",
"and",
"cannot",
"shrink",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/tags/categories.go#L48-L64 | train |
vmware/govmomi | vapi/tags/categories.go | CreateCategory | func (c *Manager) CreateCategory(ctx context.Context, category *Category) (string, error) {
// create avoids the annoyance of CreateTag requiring field keys to be included in the request,
// even though the field value can be empty.
type create struct {
Name string `json:"name"`
Description string `json:"description"`
Cardinality string `json:"cardinality"`
AssociableTypes []string `json:"associable_types"`
}
spec := struct {
Category create `json:"create_spec"`
}{
Category: create{
Name: category.Name,
Description: category.Description,
Cardinality: category.Cardinality,
AssociableTypes: category.AssociableTypes,
},
}
if spec.Category.AssociableTypes == nil {
// otherwise create fails with invalid_argument
spec.Category.AssociableTypes = []string{}
}
url := internal.URL(c, internal.CategoryPath)
var res string
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | go | func (c *Manager) CreateCategory(ctx context.Context, category *Category) (string, error) {
// create avoids the annoyance of CreateTag requiring field keys to be included in the request,
// even though the field value can be empty.
type create struct {
Name string `json:"name"`
Description string `json:"description"`
Cardinality string `json:"cardinality"`
AssociableTypes []string `json:"associable_types"`
}
spec := struct {
Category create `json:"create_spec"`
}{
Category: create{
Name: category.Name,
Description: category.Description,
Cardinality: category.Cardinality,
AssociableTypes: category.AssociableTypes,
},
}
if spec.Category.AssociableTypes == nil {
// otherwise create fails with invalid_argument
spec.Category.AssociableTypes = []string{}
}
url := internal.URL(c, internal.CategoryPath)
var res string
return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"CreateCategory",
"(",
"ctx",
"context",
".",
"Context",
",",
"category",
"*",
"Category",
")",
"(",
"string",
",",
"error",
")",
"{",
"// create avoids the annoyance of CreateTag requiring field keys to be included in the request,",
"// even though the field value can be empty.",
"type",
"create",
"struct",
"{",
"Name",
"string",
"`json:\"name\"`",
"\n",
"Description",
"string",
"`json:\"description\"`",
"\n",
"Cardinality",
"string",
"`json:\"cardinality\"`",
"\n",
"AssociableTypes",
"[",
"]",
"string",
"`json:\"associable_types\"`",
"\n",
"}",
"\n",
"spec",
":=",
"struct",
"{",
"Category",
"create",
"`json:\"create_spec\"`",
"\n",
"}",
"{",
"Category",
":",
"create",
"{",
"Name",
":",
"category",
".",
"Name",
",",
"Description",
":",
"category",
".",
"Description",
",",
"Cardinality",
":",
"category",
".",
"Cardinality",
",",
"AssociableTypes",
":",
"category",
".",
"AssociableTypes",
",",
"}",
",",
"}",
"\n",
"if",
"spec",
".",
"Category",
".",
"AssociableTypes",
"==",
"nil",
"{",
"// otherwise create fails with invalid_argument",
"spec",
".",
"Category",
".",
"AssociableTypes",
"=",
"[",
"]",
"string",
"{",
"}",
"\n",
"}",
"\n",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"CategoryPath",
")",
"\n",
"var",
"res",
"string",
"\n",
"return",
"res",
",",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodPost",
",",
"spec",
")",
",",
"&",
"res",
")",
"\n",
"}"
] | // CreateCategory creates a new category and returns the category ID. | [
"CreateCategory",
"creates",
"a",
"new",
"category",
"and",
"returns",
"the",
"category",
"ID",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/tags/categories.go#L67-L93 | train |
vmware/govmomi | vapi/tags/categories.go | UpdateCategory | func (c *Manager) UpdateCategory(ctx context.Context, category *Category) error {
spec := struct {
Category Category `json:"update_spec"`
}{
Category: Category{
AssociableTypes: category.AssociableTypes,
Cardinality: category.Cardinality,
Description: category.Description,
Name: category.Name,
},
}
url := internal.URL(c, internal.CategoryPath).WithID(category.ID)
return c.Do(ctx, url.Request(http.MethodPatch, spec), nil)
} | go | func (c *Manager) UpdateCategory(ctx context.Context, category *Category) error {
spec := struct {
Category Category `json:"update_spec"`
}{
Category: Category{
AssociableTypes: category.AssociableTypes,
Cardinality: category.Cardinality,
Description: category.Description,
Name: category.Name,
},
}
url := internal.URL(c, internal.CategoryPath).WithID(category.ID)
return c.Do(ctx, url.Request(http.MethodPatch, spec), nil)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"UpdateCategory",
"(",
"ctx",
"context",
".",
"Context",
",",
"category",
"*",
"Category",
")",
"error",
"{",
"spec",
":=",
"struct",
"{",
"Category",
"Category",
"`json:\"update_spec\"`",
"\n",
"}",
"{",
"Category",
":",
"Category",
"{",
"AssociableTypes",
":",
"category",
".",
"AssociableTypes",
",",
"Cardinality",
":",
"category",
".",
"Cardinality",
",",
"Description",
":",
"category",
".",
"Description",
",",
"Name",
":",
"category",
".",
"Name",
",",
"}",
",",
"}",
"\n",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"CategoryPath",
")",
".",
"WithID",
"(",
"category",
".",
"ID",
")",
"\n",
"return",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodPatch",
",",
"spec",
")",
",",
"nil",
")",
"\n",
"}"
] | // UpdateCategory can update one or more of the AssociableTypes, Cardinality, Description and Name fields. | [
"UpdateCategory",
"can",
"update",
"one",
"or",
"more",
"of",
"the",
"AssociableTypes",
"Cardinality",
"Description",
"and",
"Name",
"fields",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/tags/categories.go#L96-L109 | train |
vmware/govmomi | vapi/tags/categories.go | DeleteCategory | func (c *Manager) DeleteCategory(ctx context.Context, category *Category) error {
url := internal.URL(c, internal.CategoryPath).WithID(category.ID)
return c.Do(ctx, url.Request(http.MethodDelete), nil)
} | go | func (c *Manager) DeleteCategory(ctx context.Context, category *Category) error {
url := internal.URL(c, internal.CategoryPath).WithID(category.ID)
return c.Do(ctx, url.Request(http.MethodDelete), nil)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"DeleteCategory",
"(",
"ctx",
"context",
".",
"Context",
",",
"category",
"*",
"Category",
")",
"error",
"{",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"CategoryPath",
")",
".",
"WithID",
"(",
"category",
".",
"ID",
")",
"\n",
"return",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodDelete",
")",
",",
"nil",
")",
"\n",
"}"
] | // DeleteCategory deletes an existing category. | [
"DeleteCategory",
"deletes",
"an",
"existing",
"category",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/tags/categories.go#L112-L115 | train |
vmware/govmomi | vapi/tags/categories.go | GetCategory | func (c *Manager) GetCategory(ctx context.Context, id string) (*Category, error) {
if isName(id) {
cat, err := c.GetCategories(ctx)
if err != nil {
return nil, err
}
for i := range cat {
if cat[i].Name == id {
return &cat[i], nil
}
}
}
url := internal.URL(c, internal.CategoryPath).WithID(id)
var res Category
return &res, c.Do(ctx, url.Request(http.MethodGet), &res)
} | go | func (c *Manager) GetCategory(ctx context.Context, id string) (*Category, error) {
if isName(id) {
cat, err := c.GetCategories(ctx)
if err != nil {
return nil, err
}
for i := range cat {
if cat[i].Name == id {
return &cat[i], nil
}
}
}
url := internal.URL(c, internal.CategoryPath).WithID(id)
var res Category
return &res, c.Do(ctx, url.Request(http.MethodGet), &res)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"GetCategory",
"(",
"ctx",
"context",
".",
"Context",
",",
"id",
"string",
")",
"(",
"*",
"Category",
",",
"error",
")",
"{",
"if",
"isName",
"(",
"id",
")",
"{",
"cat",
",",
"err",
":=",
"c",
".",
"GetCategories",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"for",
"i",
":=",
"range",
"cat",
"{",
"if",
"cat",
"[",
"i",
"]",
".",
"Name",
"==",
"id",
"{",
"return",
"&",
"cat",
"[",
"i",
"]",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"CategoryPath",
")",
".",
"WithID",
"(",
"id",
")",
"\n",
"var",
"res",
"Category",
"\n",
"return",
"&",
"res",
",",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodGet",
")",
",",
"&",
"res",
")",
"\n",
"}"
] | // GetCategory fetches the category information for the given identifier.
// The id parameter can be a Category ID or Category Name. | [
"GetCategory",
"fetches",
"the",
"category",
"information",
"for",
"the",
"given",
"identifier",
".",
"The",
"id",
"parameter",
"can",
"be",
"a",
"Category",
"ID",
"or",
"Category",
"Name",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/tags/categories.go#L119-L135 | train |
vmware/govmomi | vapi/tags/categories.go | ListCategories | func (c *Manager) ListCategories(ctx context.Context) ([]string, error) {
url := internal.URL(c, internal.CategoryPath)
var res []string
return res, c.Do(ctx, url.Request(http.MethodGet), &res)
} | go | func (c *Manager) ListCategories(ctx context.Context) ([]string, error) {
url := internal.URL(c, internal.CategoryPath)
var res []string
return res, c.Do(ctx, url.Request(http.MethodGet), &res)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"ListCategories",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"[",
"]",
"string",
",",
"error",
")",
"{",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"CategoryPath",
")",
"\n",
"var",
"res",
"[",
"]",
"string",
"\n",
"return",
"res",
",",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodGet",
")",
",",
"&",
"res",
")",
"\n",
"}"
] | // ListCategories returns all category IDs in the system. | [
"ListCategories",
"returns",
"all",
"category",
"IDs",
"in",
"the",
"system",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/tags/categories.go#L138-L142 | train |
vmware/govmomi | vapi/tags/categories.go | GetCategories | func (c *Manager) GetCategories(ctx context.Context) ([]Category, error) {
ids, err := c.ListCategories(ctx)
if err != nil {
return nil, fmt.Errorf("list categories: %s", err)
}
var categories []Category
for _, id := range ids {
category, err := c.GetCategory(ctx, id)
if err != nil {
return nil, fmt.Errorf("get category %s: %s", id, err)
}
categories = append(categories, *category)
}
return categories, nil
} | go | func (c *Manager) GetCategories(ctx context.Context) ([]Category, error) {
ids, err := c.ListCategories(ctx)
if err != nil {
return nil, fmt.Errorf("list categories: %s", err)
}
var categories []Category
for _, id := range ids {
category, err := c.GetCategory(ctx, id)
if err != nil {
return nil, fmt.Errorf("get category %s: %s", id, err)
}
categories = append(categories, *category)
}
return categories, nil
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"GetCategories",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"[",
"]",
"Category",
",",
"error",
")",
"{",
"ids",
",",
"err",
":=",
"c",
".",
"ListCategories",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
")",
"\n",
"}",
"\n\n",
"var",
"categories",
"[",
"]",
"Category",
"\n",
"for",
"_",
",",
"id",
":=",
"range",
"ids",
"{",
"category",
",",
"err",
":=",
"c",
".",
"GetCategory",
"(",
"ctx",
",",
"id",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"id",
",",
"err",
")",
"\n",
"}",
"\n\n",
"categories",
"=",
"append",
"(",
"categories",
",",
"*",
"category",
")",
"\n\n",
"}",
"\n",
"return",
"categories",
",",
"nil",
"\n",
"}"
] | // GetCategories fetches an array of category information in the system. | [
"GetCategories",
"fetches",
"an",
"array",
"of",
"category",
"information",
"in",
"the",
"system",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/tags/categories.go#L145-L162 | train |
vmware/govmomi | vapi/simulator/simulator.go | New | func New(u *url.URL, settings []vim.BaseOptionValue) (string, http.Handler) {
s := &handler{
ServeMux: http.NewServeMux(),
URL: *u,
Category: make(map[string]*tags.Category),
Tag: make(map[string]*tags.Tag),
Association: make(map[string]map[internal.AssociatedObject]bool),
Session: make(map[string]*session),
Library: make(map[string]content),
Update: make(map[string]update),
}
handlers := []struct {
p string
m http.HandlerFunc
}{
{internal.SessionPath, s.session},
{internal.CategoryPath, s.category},
{internal.CategoryPath + "/", s.categoryID},
{internal.TagPath, s.tag},
{internal.TagPath + "/", s.tagID},
{internal.AssociationPath, s.association},
{internal.AssociationPath + "/", s.associationID},
{internal.LibraryPath, s.library},
{internal.LocalLibraryPath, s.library},
{internal.LibraryPath + "/", s.libraryID},
{internal.LocalLibraryPath + "/", s.libraryID},
{internal.LibraryItemPath, s.libraryItem},
{internal.LibraryItemPath + "/", s.libraryItemID},
{internal.LibraryItemUpdateSession, s.libraryItemUpdateSession},
{internal.LibraryItemUpdateSession + "/", s.libraryItemUpdateSessionID},
{internal.LibraryItemUpdateSessionFile, s.libraryItemUpdateSessionFile},
{internal.LibraryItemUpdateSessionFile + "/", s.libraryItemUpdateSessionFileID},
{internal.LibraryItemAdd + "/", s.libraryItemAdd},
{internal.LibraryItemFilePath, s.libraryItemFile},
{internal.LibraryItemFilePath + "/", s.libraryItemFileID},
{internal.VCenterOVFLibraryItem + "/", s.libraryItemDeployID},
}
for i := range handlers {
h := handlers[i]
s.HandleFunc(internal.Path+h.p, func(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()
if !s.isAuthorized(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}
h.m(w, r)
})
}
return internal.Path + "/", s
} | go | func New(u *url.URL, settings []vim.BaseOptionValue) (string, http.Handler) {
s := &handler{
ServeMux: http.NewServeMux(),
URL: *u,
Category: make(map[string]*tags.Category),
Tag: make(map[string]*tags.Tag),
Association: make(map[string]map[internal.AssociatedObject]bool),
Session: make(map[string]*session),
Library: make(map[string]content),
Update: make(map[string]update),
}
handlers := []struct {
p string
m http.HandlerFunc
}{
{internal.SessionPath, s.session},
{internal.CategoryPath, s.category},
{internal.CategoryPath + "/", s.categoryID},
{internal.TagPath, s.tag},
{internal.TagPath + "/", s.tagID},
{internal.AssociationPath, s.association},
{internal.AssociationPath + "/", s.associationID},
{internal.LibraryPath, s.library},
{internal.LocalLibraryPath, s.library},
{internal.LibraryPath + "/", s.libraryID},
{internal.LocalLibraryPath + "/", s.libraryID},
{internal.LibraryItemPath, s.libraryItem},
{internal.LibraryItemPath + "/", s.libraryItemID},
{internal.LibraryItemUpdateSession, s.libraryItemUpdateSession},
{internal.LibraryItemUpdateSession + "/", s.libraryItemUpdateSessionID},
{internal.LibraryItemUpdateSessionFile, s.libraryItemUpdateSessionFile},
{internal.LibraryItemUpdateSessionFile + "/", s.libraryItemUpdateSessionFileID},
{internal.LibraryItemAdd + "/", s.libraryItemAdd},
{internal.LibraryItemFilePath, s.libraryItemFile},
{internal.LibraryItemFilePath + "/", s.libraryItemFileID},
{internal.VCenterOVFLibraryItem + "/", s.libraryItemDeployID},
}
for i := range handlers {
h := handlers[i]
s.HandleFunc(internal.Path+h.p, func(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()
if !s.isAuthorized(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}
h.m(w, r)
})
}
return internal.Path + "/", s
} | [
"func",
"New",
"(",
"u",
"*",
"url",
".",
"URL",
",",
"settings",
"[",
"]",
"vim",
".",
"BaseOptionValue",
")",
"(",
"string",
",",
"http",
".",
"Handler",
")",
"{",
"s",
":=",
"&",
"handler",
"{",
"ServeMux",
":",
"http",
".",
"NewServeMux",
"(",
")",
",",
"URL",
":",
"*",
"u",
",",
"Category",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"tags",
".",
"Category",
")",
",",
"Tag",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"tags",
".",
"Tag",
")",
",",
"Association",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"map",
"[",
"internal",
".",
"AssociatedObject",
"]",
"bool",
")",
",",
"Session",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"*",
"session",
")",
",",
"Library",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"content",
")",
",",
"Update",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"update",
")",
",",
"}",
"\n\n",
"handlers",
":=",
"[",
"]",
"struct",
"{",
"p",
"string",
"\n",
"m",
"http",
".",
"HandlerFunc",
"\n",
"}",
"{",
"{",
"internal",
".",
"SessionPath",
",",
"s",
".",
"session",
"}",
",",
"{",
"internal",
".",
"CategoryPath",
",",
"s",
".",
"category",
"}",
",",
"{",
"internal",
".",
"CategoryPath",
"+",
"\"",
"\"",
",",
"s",
".",
"categoryID",
"}",
",",
"{",
"internal",
".",
"TagPath",
",",
"s",
".",
"tag",
"}",
",",
"{",
"internal",
".",
"TagPath",
"+",
"\"",
"\"",
",",
"s",
".",
"tagID",
"}",
",",
"{",
"internal",
".",
"AssociationPath",
",",
"s",
".",
"association",
"}",
",",
"{",
"internal",
".",
"AssociationPath",
"+",
"\"",
"\"",
",",
"s",
".",
"associationID",
"}",
",",
"{",
"internal",
".",
"LibraryPath",
",",
"s",
".",
"library",
"}",
",",
"{",
"internal",
".",
"LocalLibraryPath",
",",
"s",
".",
"library",
"}",
",",
"{",
"internal",
".",
"LibraryPath",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryID",
"}",
",",
"{",
"internal",
".",
"LocalLibraryPath",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryID",
"}",
",",
"{",
"internal",
".",
"LibraryItemPath",
",",
"s",
".",
"libraryItem",
"}",
",",
"{",
"internal",
".",
"LibraryItemPath",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryItemID",
"}",
",",
"{",
"internal",
".",
"LibraryItemUpdateSession",
",",
"s",
".",
"libraryItemUpdateSession",
"}",
",",
"{",
"internal",
".",
"LibraryItemUpdateSession",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryItemUpdateSessionID",
"}",
",",
"{",
"internal",
".",
"LibraryItemUpdateSessionFile",
",",
"s",
".",
"libraryItemUpdateSessionFile",
"}",
",",
"{",
"internal",
".",
"LibraryItemUpdateSessionFile",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryItemUpdateSessionFileID",
"}",
",",
"{",
"internal",
".",
"LibraryItemAdd",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryItemAdd",
"}",
",",
"{",
"internal",
".",
"LibraryItemFilePath",
",",
"s",
".",
"libraryItemFile",
"}",
",",
"{",
"internal",
".",
"LibraryItemFilePath",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryItemFileID",
"}",
",",
"{",
"internal",
".",
"VCenterOVFLibraryItem",
"+",
"\"",
"\"",
",",
"s",
".",
"libraryItemDeployID",
"}",
",",
"}",
"\n\n",
"for",
"i",
":=",
"range",
"handlers",
"{",
"h",
":=",
"handlers",
"[",
"i",
"]",
"\n",
"s",
".",
"HandleFunc",
"(",
"internal",
".",
"Path",
"+",
"h",
".",
"p",
",",
"func",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"{",
"s",
".",
"Lock",
"(",
")",
"\n",
"defer",
"s",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"!",
"s",
".",
"isAuthorized",
"(",
"r",
")",
"{",
"w",
".",
"WriteHeader",
"(",
"http",
".",
"StatusUnauthorized",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"h",
".",
"m",
"(",
"w",
",",
"r",
")",
"\n",
"}",
")",
"\n",
"}",
"\n\n",
"return",
"internal",
".",
"Path",
"+",
"\"",
"\"",
",",
"s",
"\n",
"}"
] | // New creates a vAPI simulator. | [
"New",
"creates",
"a",
"vAPI",
"simulator",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L85-L140 | train |
vmware/govmomi | vapi/simulator/simulator.go | AttachedObjects | func (s *handler) AttachedObjects(tag vim.VslmTagEntry) ([]vim.ManagedObjectReference, vim.BaseMethodFault) {
t := s.findTag(tag)
if t == nil {
return nil, new(vim.NotFound)
}
var ids []vim.ManagedObjectReference
for id := range s.Association[t.ID] {
ids = append(ids, vim.ManagedObjectReference(id))
}
return ids, nil
} | go | func (s *handler) AttachedObjects(tag vim.VslmTagEntry) ([]vim.ManagedObjectReference, vim.BaseMethodFault) {
t := s.findTag(tag)
if t == nil {
return nil, new(vim.NotFound)
}
var ids []vim.ManagedObjectReference
for id := range s.Association[t.ID] {
ids = append(ids, vim.ManagedObjectReference(id))
}
return ids, nil
} | [
"func",
"(",
"s",
"*",
"handler",
")",
"AttachedObjects",
"(",
"tag",
"vim",
".",
"VslmTagEntry",
")",
"(",
"[",
"]",
"vim",
".",
"ManagedObjectReference",
",",
"vim",
".",
"BaseMethodFault",
")",
"{",
"t",
":=",
"s",
".",
"findTag",
"(",
"tag",
")",
"\n",
"if",
"t",
"==",
"nil",
"{",
"return",
"nil",
",",
"new",
"(",
"vim",
".",
"NotFound",
")",
"\n",
"}",
"\n",
"var",
"ids",
"[",
"]",
"vim",
".",
"ManagedObjectReference",
"\n",
"for",
"id",
":=",
"range",
"s",
".",
"Association",
"[",
"t",
".",
"ID",
"]",
"{",
"ids",
"=",
"append",
"(",
"ids",
",",
"vim",
".",
"ManagedObjectReference",
"(",
"id",
")",
")",
"\n",
"}",
"\n",
"return",
"ids",
",",
"nil",
"\n",
"}"
] | // AttachedObjects is meant for internal use via simulator.Registry.tagManager | [
"AttachedObjects",
"is",
"meant",
"for",
"internal",
"use",
"via",
"simulator",
".",
"Registry",
".",
"tagManager"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L188-L198 | train |
vmware/govmomi | vapi/simulator/simulator.go | AttachedTags | func (s *handler) AttachedTags(ref vim.ManagedObjectReference) ([]vim.VslmTagEntry, vim.BaseMethodFault) {
oid := internal.AssociatedObject(ref)
var tags []vim.VslmTagEntry
for id, objs := range s.Association {
if objs[oid] {
tag := s.Tag[id]
cat := s.Category[tag.CategoryID]
tags = append(tags, vim.VslmTagEntry{
TagName: tag.Name,
ParentCategoryName: cat.Name,
})
}
}
return tags, nil
} | go | func (s *handler) AttachedTags(ref vim.ManagedObjectReference) ([]vim.VslmTagEntry, vim.BaseMethodFault) {
oid := internal.AssociatedObject(ref)
var tags []vim.VslmTagEntry
for id, objs := range s.Association {
if objs[oid] {
tag := s.Tag[id]
cat := s.Category[tag.CategoryID]
tags = append(tags, vim.VslmTagEntry{
TagName: tag.Name,
ParentCategoryName: cat.Name,
})
}
}
return tags, nil
} | [
"func",
"(",
"s",
"*",
"handler",
")",
"AttachedTags",
"(",
"ref",
"vim",
".",
"ManagedObjectReference",
")",
"(",
"[",
"]",
"vim",
".",
"VslmTagEntry",
",",
"vim",
".",
"BaseMethodFault",
")",
"{",
"oid",
":=",
"internal",
".",
"AssociatedObject",
"(",
"ref",
")",
"\n",
"var",
"tags",
"[",
"]",
"vim",
".",
"VslmTagEntry",
"\n",
"for",
"id",
",",
"objs",
":=",
"range",
"s",
".",
"Association",
"{",
"if",
"objs",
"[",
"oid",
"]",
"{",
"tag",
":=",
"s",
".",
"Tag",
"[",
"id",
"]",
"\n",
"cat",
":=",
"s",
".",
"Category",
"[",
"tag",
".",
"CategoryID",
"]",
"\n",
"tags",
"=",
"append",
"(",
"tags",
",",
"vim",
".",
"VslmTagEntry",
"{",
"TagName",
":",
"tag",
".",
"Name",
",",
"ParentCategoryName",
":",
"cat",
".",
"Name",
",",
"}",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"tags",
",",
"nil",
"\n",
"}"
] | // AttachedTags is meant for internal use via simulator.Registry.tagManager | [
"AttachedTags",
"is",
"meant",
"for",
"internal",
"use",
"via",
"simulator",
".",
"Registry",
".",
"tagManager"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L201-L215 | train |
vmware/govmomi | vapi/simulator/simulator.go | AttachTag | func (s *handler) AttachTag(ref vim.ManagedObjectReference, tag vim.VslmTagEntry) vim.BaseMethodFault {
t := s.findTag(tag)
if t == nil {
return new(vim.NotFound)
}
s.Association[t.ID][internal.AssociatedObject(ref)] = true
return nil
} | go | func (s *handler) AttachTag(ref vim.ManagedObjectReference, tag vim.VslmTagEntry) vim.BaseMethodFault {
t := s.findTag(tag)
if t == nil {
return new(vim.NotFound)
}
s.Association[t.ID][internal.AssociatedObject(ref)] = true
return nil
} | [
"func",
"(",
"s",
"*",
"handler",
")",
"AttachTag",
"(",
"ref",
"vim",
".",
"ManagedObjectReference",
",",
"tag",
"vim",
".",
"VslmTagEntry",
")",
"vim",
".",
"BaseMethodFault",
"{",
"t",
":=",
"s",
".",
"findTag",
"(",
"tag",
")",
"\n",
"if",
"t",
"==",
"nil",
"{",
"return",
"new",
"(",
"vim",
".",
"NotFound",
")",
"\n",
"}",
"\n",
"s",
".",
"Association",
"[",
"t",
".",
"ID",
"]",
"[",
"internal",
".",
"AssociatedObject",
"(",
"ref",
")",
"]",
"=",
"true",
"\n",
"return",
"nil",
"\n",
"}"
] | // AttachTag is meant for internal use via simulator.Registry.tagManager | [
"AttachTag",
"is",
"meant",
"for",
"internal",
"use",
"via",
"simulator",
".",
"Registry",
".",
"tagManager"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L218-L225 | train |
vmware/govmomi | vapi/simulator/simulator.go | DetachTag | func (s *handler) DetachTag(id vim.ManagedObjectReference, tag vim.VslmTagEntry) vim.BaseMethodFault {
t := s.findTag(tag)
if t == nil {
return new(vim.NotFound)
}
delete(s.Association[t.ID], internal.AssociatedObject(id))
return nil
} | go | func (s *handler) DetachTag(id vim.ManagedObjectReference, tag vim.VslmTagEntry) vim.BaseMethodFault {
t := s.findTag(tag)
if t == nil {
return new(vim.NotFound)
}
delete(s.Association[t.ID], internal.AssociatedObject(id))
return nil
} | [
"func",
"(",
"s",
"*",
"handler",
")",
"DetachTag",
"(",
"id",
"vim",
".",
"ManagedObjectReference",
",",
"tag",
"vim",
".",
"VslmTagEntry",
")",
"vim",
".",
"BaseMethodFault",
"{",
"t",
":=",
"s",
".",
"findTag",
"(",
"tag",
")",
"\n",
"if",
"t",
"==",
"nil",
"{",
"return",
"new",
"(",
"vim",
".",
"NotFound",
")",
"\n",
"}",
"\n",
"delete",
"(",
"s",
".",
"Association",
"[",
"t",
".",
"ID",
"]",
",",
"internal",
".",
"AssociatedObject",
"(",
"id",
")",
")",
"\n",
"return",
"nil",
"\n",
"}"
] | // DetachTag is meant for internal use via simulator.Registry.tagManager | [
"DetachTag",
"is",
"meant",
"for",
"internal",
"use",
"via",
"simulator",
".",
"Registry",
".",
"tagManager"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L228-L235 | train |
vmware/govmomi | vapi/simulator/simulator.go | ok | func (s *handler) ok(w http.ResponseWriter, val ...interface{}) {
w.WriteHeader(http.StatusOK)
if len(val) == 0 {
return
}
err := json.NewEncoder(w).Encode(struct {
Value interface{} `json:"value,omitempty"`
}{
val[0],
})
if err != nil {
log.Panic(err)
}
} | go | func (s *handler) ok(w http.ResponseWriter, val ...interface{}) {
w.WriteHeader(http.StatusOK)
if len(val) == 0 {
return
}
err := json.NewEncoder(w).Encode(struct {
Value interface{} `json:"value,omitempty"`
}{
val[0],
})
if err != nil {
log.Panic(err)
}
} | [
"func",
"(",
"s",
"*",
"handler",
")",
"ok",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"val",
"...",
"interface",
"{",
"}",
")",
"{",
"w",
".",
"WriteHeader",
"(",
"http",
".",
"StatusOK",
")",
"\n\n",
"if",
"len",
"(",
"val",
")",
"==",
"0",
"{",
"return",
"\n",
"}",
"\n\n",
"err",
":=",
"json",
".",
"NewEncoder",
"(",
"w",
")",
".",
"Encode",
"(",
"struct",
"{",
"Value",
"interface",
"{",
"}",
"`json:\"value,omitempty\"`",
"\n",
"}",
"{",
"val",
"[",
"0",
"]",
",",
"}",
")",
"\n\n",
"if",
"err",
"!=",
"nil",
"{",
"log",
".",
"Panic",
"(",
"err",
")",
"\n",
"}",
"\n",
"}"
] | // ok responds with http.StatusOK and json encodes val if given. | [
"ok",
"responds",
"with",
"http",
".",
"StatusOK",
"and",
"json",
"encodes",
"val",
"if",
"given",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L238-L254 | train |
vmware/govmomi | vapi/simulator/simulator.go | ServeHTTP | func (s *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost, http.MethodDelete, http.MethodGet, http.MethodPatch, http.MethodPut:
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
h, _ := s.Handler(r)
h.ServeHTTP(w, r)
} | go | func (s *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost, http.MethodDelete, http.MethodGet, http.MethodPatch, http.MethodPut:
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
h, _ := s.Handler(r)
h.ServeHTTP(w, r)
} | [
"func",
"(",
"s",
"*",
"handler",
")",
"ServeHTTP",
"(",
"w",
"http",
".",
"ResponseWriter",
",",
"r",
"*",
"http",
".",
"Request",
")",
"{",
"switch",
"r",
".",
"Method",
"{",
"case",
"http",
".",
"MethodPost",
",",
"http",
".",
"MethodDelete",
",",
"http",
".",
"MethodGet",
",",
"http",
".",
"MethodPatch",
",",
"http",
".",
"MethodPut",
":",
"default",
":",
"w",
".",
"WriteHeader",
"(",
"http",
".",
"StatusMethodNotAllowed",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"h",
",",
"_",
":=",
"s",
".",
"Handler",
"(",
"r",
")",
"\n",
"h",
".",
"ServeHTTP",
"(",
"w",
",",
"r",
")",
"\n",
"}"
] | // ServeHTTP handles vAPI requests. | [
"ServeHTTP",
"handles",
"vAPI",
"requests",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L279-L289 | train |
vmware/govmomi | vapi/simulator/simulator.go | libraryPath | func libraryPath(l *library.Library, id string) string {
// DatastoreID (moref) format is "$local-path@$ds-folder-id",
// see simulator.HostDatastoreSystem.CreateLocalDatastore
ds := strings.SplitN(l.Storage[0].DatastoreID, "@", 2)[0]
return path.Join(append([]string{ds, "contentlib-" + l.ID}, id)...)
} | go | func libraryPath(l *library.Library, id string) string {
// DatastoreID (moref) format is "$local-path@$ds-folder-id",
// see simulator.HostDatastoreSystem.CreateLocalDatastore
ds := strings.SplitN(l.Storage[0].DatastoreID, "@", 2)[0]
return path.Join(append([]string{ds, "contentlib-" + l.ID}, id)...)
} | [
"func",
"libraryPath",
"(",
"l",
"*",
"library",
".",
"Library",
",",
"id",
"string",
")",
"string",
"{",
"// DatastoreID (moref) format is \"$local-path@$ds-folder-id\",",
"// see simulator.HostDatastoreSystem.CreateLocalDatastore",
"ds",
":=",
"strings",
".",
"SplitN",
"(",
"l",
".",
"Storage",
"[",
"0",
"]",
".",
"DatastoreID",
",",
"\"",
"\"",
",",
"2",
")",
"[",
"0",
"]",
"\n",
"return",
"path",
".",
"Join",
"(",
"append",
"(",
"[",
"]",
"string",
"{",
"ds",
",",
"\"",
"\"",
"+",
"l",
".",
"ID",
"}",
",",
"id",
")",
"...",
")",
"\n",
"}"
] | // libraryPath returns the local Datastore fs path for a Library or Item if id is specified. | [
"libraryPath",
"returns",
"the",
"local",
"Datastore",
"fs",
"path",
"for",
"a",
"Library",
"or",
"Item",
"if",
"id",
"is",
"specified",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/simulator/simulator.go#L898-L903 | train |
vmware/govmomi | govc/library/ova.go | NewOVAFile | func NewOVAFile(filename string) (*OVAFile, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
tarFile := tar.NewReader(f)
return &OVAFile{filename: filename, file: f, tarFile: tarFile}, nil
} | go | func NewOVAFile(filename string) (*OVAFile, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
tarFile := tar.NewReader(f)
return &OVAFile{filename: filename, file: f, tarFile: tarFile}, nil
} | [
"func",
"NewOVAFile",
"(",
"filename",
"string",
")",
"(",
"*",
"OVAFile",
",",
"error",
")",
"{",
"f",
",",
"err",
":=",
"os",
".",
"Open",
"(",
"filename",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"tarFile",
":=",
"tar",
".",
"NewReader",
"(",
"f",
")",
"\n",
"return",
"&",
"OVAFile",
"{",
"filename",
":",
"filename",
",",
"file",
":",
"f",
",",
"tarFile",
":",
"tarFile",
"}",
",",
"nil",
"\n",
"}"
] | // NewOVAFile creates a new OVA file reader | [
"NewOVAFile",
"creates",
"a",
"new",
"OVA",
"file",
"reader"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/library/ova.go#L87-L94 | train |
vmware/govmomi | govc/library/ova.go | Find | func (of *OVAFile) Find(filename string) (*tar.Header, error) {
for {
header, err := of.tarFile.Next()
if err == io.EOF {
return nil, err
}
if header.Name == filename {
return header, nil
}
}
} | go | func (of *OVAFile) Find(filename string) (*tar.Header, error) {
for {
header, err := of.tarFile.Next()
if err == io.EOF {
return nil, err
}
if header.Name == filename {
return header, nil
}
}
} | [
"func",
"(",
"of",
"*",
"OVAFile",
")",
"Find",
"(",
"filename",
"string",
")",
"(",
"*",
"tar",
".",
"Header",
",",
"error",
")",
"{",
"for",
"{",
"header",
",",
"err",
":=",
"of",
".",
"tarFile",
".",
"Next",
"(",
")",
"\n",
"if",
"err",
"==",
"io",
".",
"EOF",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"if",
"header",
".",
"Name",
"==",
"filename",
"{",
"return",
"header",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // Find looks for a filename match in the OVA file | [
"Find",
"looks",
"for",
"a",
"filename",
"match",
"in",
"the",
"OVA",
"file"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/library/ova.go#L97-L107 | train |
vmware/govmomi | govc/library/ova.go | Read | func (of *OVAFile) Read(b []byte) (int, error) {
if of.tarFile == nil {
return 0, io.EOF
}
return of.tarFile.Read(b)
} | go | func (of *OVAFile) Read(b []byte) (int, error) {
if of.tarFile == nil {
return 0, io.EOF
}
return of.tarFile.Read(b)
} | [
"func",
"(",
"of",
"*",
"OVAFile",
")",
"Read",
"(",
"b",
"[",
"]",
"byte",
")",
"(",
"int",
",",
"error",
")",
"{",
"if",
"of",
".",
"tarFile",
"==",
"nil",
"{",
"return",
"0",
",",
"io",
".",
"EOF",
"\n",
"}",
"\n",
"return",
"of",
".",
"tarFile",
".",
"Read",
"(",
"b",
")",
"\n",
"}"
] | // Read reads from the current file in the OVA file | [
"Read",
"reads",
"from",
"the",
"current",
"file",
"in",
"the",
"OVA",
"file"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/library/ova.go#L115-L120 | train |
vmware/govmomi | govc/library/ova.go | getOVAFileInfo | func getOVAFileInfo(ovafile string, filename string) (int64, string, error) {
of, err := NewOVAFile(ovafile)
if err != nil {
return 0, "", err
}
hdr, err := of.Find(filename)
if err != nil {
return 0, "", err
}
hash := md5.New()
_, err = io.Copy(hash, of)
if err != nil {
return 0, "", err
}
md5String := hex.EncodeToString(hash.Sum(nil)[:16])
return hdr.Size, md5String, nil
} | go | func getOVAFileInfo(ovafile string, filename string) (int64, string, error) {
of, err := NewOVAFile(ovafile)
if err != nil {
return 0, "", err
}
hdr, err := of.Find(filename)
if err != nil {
return 0, "", err
}
hash := md5.New()
_, err = io.Copy(hash, of)
if err != nil {
return 0, "", err
}
md5String := hex.EncodeToString(hash.Sum(nil)[:16])
return hdr.Size, md5String, nil
} | [
"func",
"getOVAFileInfo",
"(",
"ovafile",
"string",
",",
"filename",
"string",
")",
"(",
"int64",
",",
"string",
",",
"error",
")",
"{",
"of",
",",
"err",
":=",
"NewOVAFile",
"(",
"ovafile",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"hdr",
",",
"err",
":=",
"of",
".",
"Find",
"(",
"filename",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"hash",
":=",
"md5",
".",
"New",
"(",
")",
"\n",
"_",
",",
"err",
"=",
"io",
".",
"Copy",
"(",
"hash",
",",
"of",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"md5String",
":=",
"hex",
".",
"EncodeToString",
"(",
"hash",
".",
"Sum",
"(",
"nil",
")",
"[",
":",
"16",
"]",
")",
"\n\n",
"return",
"hdr",
".",
"Size",
",",
"md5String",
",",
"nil",
"\n",
"}"
] | // getOVAFileInfo opens an OVA, finds the file entry, and returns both the size and md5 checksum | [
"getOVAFileInfo",
"opens",
"an",
"OVA",
"finds",
"the",
"file",
"entry",
"and",
"returns",
"both",
"the",
"size",
"and",
"md5",
"checksum"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/library/ova.go#L128-L147 | train |
vmware/govmomi | govc/library/ova.go | uploadFile | func uploadFile(ctx context.Context, m *library.Manager, sessionID string, ovafile string, filename string) error {
var updateFileInfo library.UpdateFile
fmt.Printf("Uploading %s from %s\n", filename, ovafile)
size, md5String, _ := getOVAFileInfo(ovafile, filename)
// Get the URI for the file upload
updateFileInfo.Name = filename
updateFileInfo.Size = &size
updateFileInfo.SourceType = "PUSH"
updateFileInfo.Checksum = &library.Checksum{
Algorithm: "MD5",
Checksum: md5String,
}
addFileInfo, err := m.AddLibraryItemFile(ctx, sessionID, updateFileInfo)
if err != nil {
return err
}
of, err := NewOVAFile(ovafile)
if err != nil {
return err
}
defer of.Close()
// Setup to point to the OVA file to be transferred
_, err = of.Find(filename)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", addFileInfo.UploadEndpoint.URI, of)
if err != nil {
return err
}
req.Header.Set("vmware-api-session-id", sessionID)
return m.Do(ctx, req, nil)
} | go | func uploadFile(ctx context.Context, m *library.Manager, sessionID string, ovafile string, filename string) error {
var updateFileInfo library.UpdateFile
fmt.Printf("Uploading %s from %s\n", filename, ovafile)
size, md5String, _ := getOVAFileInfo(ovafile, filename)
// Get the URI for the file upload
updateFileInfo.Name = filename
updateFileInfo.Size = &size
updateFileInfo.SourceType = "PUSH"
updateFileInfo.Checksum = &library.Checksum{
Algorithm: "MD5",
Checksum: md5String,
}
addFileInfo, err := m.AddLibraryItemFile(ctx, sessionID, updateFileInfo)
if err != nil {
return err
}
of, err := NewOVAFile(ovafile)
if err != nil {
return err
}
defer of.Close()
// Setup to point to the OVA file to be transferred
_, err = of.Find(filename)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", addFileInfo.UploadEndpoint.URI, of)
if err != nil {
return err
}
req.Header.Set("vmware-api-session-id", sessionID)
return m.Do(ctx, req, nil)
} | [
"func",
"uploadFile",
"(",
"ctx",
"context",
".",
"Context",
",",
"m",
"*",
"library",
".",
"Manager",
",",
"sessionID",
"string",
",",
"ovafile",
"string",
",",
"filename",
"string",
")",
"error",
"{",
"var",
"updateFileInfo",
"library",
".",
"UpdateFile",
"\n\n",
"fmt",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"filename",
",",
"ovafile",
")",
"\n",
"size",
",",
"md5String",
",",
"_",
":=",
"getOVAFileInfo",
"(",
"ovafile",
",",
"filename",
")",
"\n\n",
"// Get the URI for the file upload",
"updateFileInfo",
".",
"Name",
"=",
"filename",
"\n",
"updateFileInfo",
".",
"Size",
"=",
"&",
"size",
"\n",
"updateFileInfo",
".",
"SourceType",
"=",
"\"",
"\"",
"\n",
"updateFileInfo",
".",
"Checksum",
"=",
"&",
"library",
".",
"Checksum",
"{",
"Algorithm",
":",
"\"",
"\"",
",",
"Checksum",
":",
"md5String",
",",
"}",
"\n\n",
"addFileInfo",
",",
"err",
":=",
"m",
".",
"AddLibraryItemFile",
"(",
"ctx",
",",
"sessionID",
",",
"updateFileInfo",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"of",
",",
"err",
":=",
"NewOVAFile",
"(",
"ovafile",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"defer",
"of",
".",
"Close",
"(",
")",
"\n\n",
"// Setup to point to the OVA file to be transferred",
"_",
",",
"err",
"=",
"of",
".",
"Find",
"(",
"filename",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"req",
",",
"err",
":=",
"http",
".",
"NewRequest",
"(",
"\"",
"\"",
",",
"addFileInfo",
".",
"UploadEndpoint",
".",
"URI",
",",
"of",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"req",
".",
"Header",
".",
"Set",
"(",
"\"",
"\"",
",",
"sessionID",
")",
"\n\n",
"return",
"m",
".",
"Do",
"(",
"ctx",
",",
"req",
",",
"nil",
")",
"\n",
"}"
] | // uploadFile will upload a single file from an OVA using the sessionID provided | [
"uploadFile",
"will",
"upload",
"a",
"single",
"file",
"from",
"an",
"OVA",
"using",
"the",
"sessionID",
"provided"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/library/ova.go#L150-L190 | train |
vmware/govmomi | simulator/folder.go | hostsWithDatastore | func hostsWithDatastore(hosts []types.ManagedObjectReference, path string) []types.ManagedObjectReference {
attached := hosts[:0]
var p object.DatastorePath
p.FromString(path)
for _, host := range hosts {
h := Map.Get(host).(*HostSystem)
if Map.FindByName(p.Datastore, h.Datastore) != nil {
attached = append(attached, host)
}
}
return attached
} | go | func hostsWithDatastore(hosts []types.ManagedObjectReference, path string) []types.ManagedObjectReference {
attached := hosts[:0]
var p object.DatastorePath
p.FromString(path)
for _, host := range hosts {
h := Map.Get(host).(*HostSystem)
if Map.FindByName(p.Datastore, h.Datastore) != nil {
attached = append(attached, host)
}
}
return attached
} | [
"func",
"hostsWithDatastore",
"(",
"hosts",
"[",
"]",
"types",
".",
"ManagedObjectReference",
",",
"path",
"string",
")",
"[",
"]",
"types",
".",
"ManagedObjectReference",
"{",
"attached",
":=",
"hosts",
"[",
":",
"0",
"]",
"\n",
"var",
"p",
"object",
".",
"DatastorePath",
"\n",
"p",
".",
"FromString",
"(",
"path",
")",
"\n\n",
"for",
"_",
",",
"host",
":=",
"range",
"hosts",
"{",
"h",
":=",
"Map",
".",
"Get",
"(",
"host",
")",
".",
"(",
"*",
"HostSystem",
")",
"\n",
"if",
"Map",
".",
"FindByName",
"(",
"p",
".",
"Datastore",
",",
"h",
".",
"Datastore",
")",
"!=",
"nil",
"{",
"attached",
"=",
"append",
"(",
"attached",
",",
"host",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"attached",
"\n",
"}"
] | // hostsWithDatastore returns hosts that have access to the given datastore path | [
"hostsWithDatastore",
"returns",
"hosts",
"that",
"have",
"access",
"to",
"the",
"given",
"datastore",
"path"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/folder.go#L260-L273 | train |
vmware/govmomi | ovf/env.go | Marshal | func (e Env) Marshal() (string, error) {
x, err := xml.Marshal(e)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s", xml.Header, x), nil
} | go | func (e Env) Marshal() (string, error) {
x, err := xml.Marshal(e)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s", xml.Header, x), nil
} | [
"func",
"(",
"e",
"Env",
")",
"Marshal",
"(",
")",
"(",
"string",
",",
"error",
")",
"{",
"x",
",",
"err",
":=",
"xml",
".",
"Marshal",
"(",
"e",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"xml",
".",
"Header",
",",
"x",
")",
",",
"nil",
"\n",
"}"
] | // Marshal marshals Env to xml by using xml.Marshal. | [
"Marshal",
"marshals",
"Env",
"to",
"xml",
"by",
"using",
"xml",
".",
"Marshal",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/ovf/env.go#L72-L79 | train |
vmware/govmomi | ovf/env.go | MarshalManual | func (e Env) MarshalManual() string {
var buffer bytes.Buffer
buffer.WriteString(xml.Header)
buffer.WriteString(fmt.Sprintf(ovfEnvHeader, e.EsxID))
buffer.WriteString(fmt.Sprintf(ovfEnvPlatformSection, e.Platform.Kind, e.Platform.Version, e.Platform.Vendor, e.Platform.Locale))
buffer.WriteString(fmt.Sprint(ovfEnvPropertyHeader))
for _, p := range e.Property.Properties {
buffer.WriteString(fmt.Sprintf(ovfEnvPropertyEntry, p.Key, p.Value))
}
buffer.WriteString(fmt.Sprint(ovfEnvPropertyFooter))
buffer.WriteString(fmt.Sprint(ovfEnvFooter))
return buffer.String()
} | go | func (e Env) MarshalManual() string {
var buffer bytes.Buffer
buffer.WriteString(xml.Header)
buffer.WriteString(fmt.Sprintf(ovfEnvHeader, e.EsxID))
buffer.WriteString(fmt.Sprintf(ovfEnvPlatformSection, e.Platform.Kind, e.Platform.Version, e.Platform.Vendor, e.Platform.Locale))
buffer.WriteString(fmt.Sprint(ovfEnvPropertyHeader))
for _, p := range e.Property.Properties {
buffer.WriteString(fmt.Sprintf(ovfEnvPropertyEntry, p.Key, p.Value))
}
buffer.WriteString(fmt.Sprint(ovfEnvPropertyFooter))
buffer.WriteString(fmt.Sprint(ovfEnvFooter))
return buffer.String()
} | [
"func",
"(",
"e",
"Env",
")",
"MarshalManual",
"(",
")",
"string",
"{",
"var",
"buffer",
"bytes",
".",
"Buffer",
"\n\n",
"buffer",
".",
"WriteString",
"(",
"xml",
".",
"Header",
")",
"\n",
"buffer",
".",
"WriteString",
"(",
"fmt",
".",
"Sprintf",
"(",
"ovfEnvHeader",
",",
"e",
".",
"EsxID",
")",
")",
"\n",
"buffer",
".",
"WriteString",
"(",
"fmt",
".",
"Sprintf",
"(",
"ovfEnvPlatformSection",
",",
"e",
".",
"Platform",
".",
"Kind",
",",
"e",
".",
"Platform",
".",
"Version",
",",
"e",
".",
"Platform",
".",
"Vendor",
",",
"e",
".",
"Platform",
".",
"Locale",
")",
")",
"\n\n",
"buffer",
".",
"WriteString",
"(",
"fmt",
".",
"Sprint",
"(",
"ovfEnvPropertyHeader",
")",
")",
"\n",
"for",
"_",
",",
"p",
":=",
"range",
"e",
".",
"Property",
".",
"Properties",
"{",
"buffer",
".",
"WriteString",
"(",
"fmt",
".",
"Sprintf",
"(",
"ovfEnvPropertyEntry",
",",
"p",
".",
"Key",
",",
"p",
".",
"Value",
")",
")",
"\n",
"}",
"\n",
"buffer",
".",
"WriteString",
"(",
"fmt",
".",
"Sprint",
"(",
"ovfEnvPropertyFooter",
")",
")",
"\n\n",
"buffer",
".",
"WriteString",
"(",
"fmt",
".",
"Sprint",
"(",
"ovfEnvFooter",
")",
")",
"\n\n",
"return",
"buffer",
".",
"String",
"(",
")",
"\n",
"}"
] | // MarshalManual manually marshals Env to xml suitable for a vApp guest.
// It exists to overcome the lack of expressiveness in Go's XML namespaces. | [
"MarshalManual",
"manually",
"marshals",
"Env",
"to",
"xml",
"suitable",
"for",
"a",
"vApp",
"guest",
".",
"It",
"exists",
"to",
"overcome",
"the",
"lack",
"of",
"expressiveness",
"in",
"Go",
"s",
"XML",
"namespaces",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/ovf/env.go#L83-L99 | train |
vmware/govmomi | vim25/xml/read.go | DecodeElement | func (d *Decoder) DecodeElement(v interface{}, start *StartElement) error {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return errors.New("non-pointer passed to Unmarshal")
}
return d.unmarshal(val.Elem(), start)
} | go | func (d *Decoder) DecodeElement(v interface{}, start *StartElement) error {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Ptr {
return errors.New("non-pointer passed to Unmarshal")
}
return d.unmarshal(val.Elem(), start)
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"DecodeElement",
"(",
"v",
"interface",
"{",
"}",
",",
"start",
"*",
"StartElement",
")",
"error",
"{",
"val",
":=",
"reflect",
".",
"ValueOf",
"(",
"v",
")",
"\n",
"if",
"val",
".",
"Kind",
"(",
")",
"!=",
"reflect",
".",
"Ptr",
"{",
"return",
"errors",
".",
"New",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"return",
"d",
".",
"unmarshal",
"(",
"val",
".",
"Elem",
"(",
")",
",",
"start",
")",
"\n",
"}"
] | // DecodeElement works like xml.Unmarshal except that it takes
// a pointer to the start XML element to decode into v.
// It is useful when a client reads some raw XML tokens itself
// but also wants to defer to Unmarshal for some elements. | [
"DecodeElement",
"works",
"like",
"xml",
".",
"Unmarshal",
"except",
"that",
"it",
"takes",
"a",
"pointer",
"to",
"the",
"start",
"XML",
"element",
"to",
"decode",
"into",
"v",
".",
"It",
"is",
"useful",
"when",
"a",
"client",
"reads",
"some",
"raw",
"XML",
"tokens",
"itself",
"but",
"also",
"wants",
"to",
"defer",
"to",
"Unmarshal",
"for",
"some",
"elements",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/read.go#L128-L134 | train |
vmware/govmomi | vim25/xml/read.go | receiverType | func receiverType(val interface{}) string {
t := reflect.TypeOf(val)
if t.Name() != "" {
return t.String()
}
return "(" + t.String() + ")"
} | go | func receiverType(val interface{}) string {
t := reflect.TypeOf(val)
if t.Name() != "" {
return t.String()
}
return "(" + t.String() + ")"
} | [
"func",
"receiverType",
"(",
"val",
"interface",
"{",
"}",
")",
"string",
"{",
"t",
":=",
"reflect",
".",
"TypeOf",
"(",
"val",
")",
"\n",
"if",
"t",
".",
"Name",
"(",
")",
"!=",
"\"",
"\"",
"{",
"return",
"t",
".",
"String",
"(",
")",
"\n",
"}",
"\n",
"return",
"\"",
"\"",
"+",
"t",
".",
"String",
"(",
")",
"+",
"\"",
"\"",
"\n",
"}"
] | // receiverType returns the receiver type to use in an expression like "%s.MethodName". | [
"receiverType",
"returns",
"the",
"receiver",
"type",
"to",
"use",
"in",
"an",
"expression",
"like",
"%s",
".",
"MethodName",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/read.go#L173-L179 | train |
vmware/govmomi | vim25/xml/read.go | typeForElement | func (p *Decoder) typeForElement(val reflect.Value, start *StartElement) reflect.Type {
t := ""
for i, a := range start.Attr {
if a.Name == xmlSchemaInstance || a.Name == xsiType {
t = a.Value
// HACK: ensure xsi:type is last in the list to avoid using that value for
// a "type" attribute, such as ManagedObjectReference.Type for example.
// Note that xsi:type is already the last attribute in VC/ESX responses.
// This is only an issue with govmomi simulator generated responses.
// Proper fix will require finding a few needles in this xml package haystack.
// Note: govmomi uses xmlSchemaInstance, other clients (e.g. rbvmomi) use xsiType.
// They are the same thing to XML parsers, but not to this hack here.
x := len(start.Attr) - 1
if i != x {
start.Attr[i] = start.Attr[x]
start.Attr[x] = a
}
break
}
}
if t == "" {
// No type attribute; fall back to looking up type by interface name.
t = val.Type().Name()
}
// Maybe the type is a basic xsd:* type.
typ := stringToType(t)
if typ != nil {
return typ
}
// Maybe the type is a custom type.
if p.TypeFunc != nil {
if typ, ok := p.TypeFunc(t); ok {
return typ
}
}
return nil
} | go | func (p *Decoder) typeForElement(val reflect.Value, start *StartElement) reflect.Type {
t := ""
for i, a := range start.Attr {
if a.Name == xmlSchemaInstance || a.Name == xsiType {
t = a.Value
// HACK: ensure xsi:type is last in the list to avoid using that value for
// a "type" attribute, such as ManagedObjectReference.Type for example.
// Note that xsi:type is already the last attribute in VC/ESX responses.
// This is only an issue with govmomi simulator generated responses.
// Proper fix will require finding a few needles in this xml package haystack.
// Note: govmomi uses xmlSchemaInstance, other clients (e.g. rbvmomi) use xsiType.
// They are the same thing to XML parsers, but not to this hack here.
x := len(start.Attr) - 1
if i != x {
start.Attr[i] = start.Attr[x]
start.Attr[x] = a
}
break
}
}
if t == "" {
// No type attribute; fall back to looking up type by interface name.
t = val.Type().Name()
}
// Maybe the type is a basic xsd:* type.
typ := stringToType(t)
if typ != nil {
return typ
}
// Maybe the type is a custom type.
if p.TypeFunc != nil {
if typ, ok := p.TypeFunc(t); ok {
return typ
}
}
return nil
} | [
"func",
"(",
"p",
"*",
"Decoder",
")",
"typeForElement",
"(",
"val",
"reflect",
".",
"Value",
",",
"start",
"*",
"StartElement",
")",
"reflect",
".",
"Type",
"{",
"t",
":=",
"\"",
"\"",
"\n",
"for",
"i",
",",
"a",
":=",
"range",
"start",
".",
"Attr",
"{",
"if",
"a",
".",
"Name",
"==",
"xmlSchemaInstance",
"||",
"a",
".",
"Name",
"==",
"xsiType",
"{",
"t",
"=",
"a",
".",
"Value",
"\n",
"// HACK: ensure xsi:type is last in the list to avoid using that value for",
"// a \"type\" attribute, such as ManagedObjectReference.Type for example.",
"// Note that xsi:type is already the last attribute in VC/ESX responses.",
"// This is only an issue with govmomi simulator generated responses.",
"// Proper fix will require finding a few needles in this xml package haystack.",
"// Note: govmomi uses xmlSchemaInstance, other clients (e.g. rbvmomi) use xsiType.",
"// They are the same thing to XML parsers, but not to this hack here.",
"x",
":=",
"len",
"(",
"start",
".",
"Attr",
")",
"-",
"1",
"\n",
"if",
"i",
"!=",
"x",
"{",
"start",
".",
"Attr",
"[",
"i",
"]",
"=",
"start",
".",
"Attr",
"[",
"x",
"]",
"\n",
"start",
".",
"Attr",
"[",
"x",
"]",
"=",
"a",
"\n",
"}",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"t",
"==",
"\"",
"\"",
"{",
"// No type attribute; fall back to looking up type by interface name.",
"t",
"=",
"val",
".",
"Type",
"(",
")",
".",
"Name",
"(",
")",
"\n",
"}",
"\n\n",
"// Maybe the type is a basic xsd:* type.",
"typ",
":=",
"stringToType",
"(",
"t",
")",
"\n",
"if",
"typ",
"!=",
"nil",
"{",
"return",
"typ",
"\n",
"}",
"\n\n",
"// Maybe the type is a custom type.",
"if",
"p",
".",
"TypeFunc",
"!=",
"nil",
"{",
"if",
"typ",
",",
"ok",
":=",
"p",
".",
"TypeFunc",
"(",
"t",
")",
";",
"ok",
"{",
"return",
"typ",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // Find reflect.Type for an element's type attribute. | [
"Find",
"reflect",
".",
"Type",
"for",
"an",
"element",
"s",
"type",
"attribute",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/read.go#L272-L312 | train |
vmware/govmomi | vim25/xml/read.go | unmarshalPath | func (p *Decoder) unmarshalPath(tinfo *typeInfo, sv reflect.Value, parents []string, start *StartElement) (consumed bool, err error) {
recurse := false
Loop:
for i := range tinfo.fields {
finfo := &tinfo.fields[i]
if finfo.flags&fElement == 0 || len(finfo.parents) < len(parents) || finfo.xmlns != "" && finfo.xmlns != start.Name.Space {
continue
}
for j := range parents {
if parents[j] != finfo.parents[j] {
continue Loop
}
}
if len(finfo.parents) == len(parents) && finfo.name == start.Name.Local {
// It's a perfect match, unmarshal the field.
return true, p.unmarshal(finfo.value(sv), start)
}
if len(finfo.parents) > len(parents) && finfo.parents[len(parents)] == start.Name.Local {
// It's a prefix for the field. Break and recurse
// since it's not ok for one field path to be itself
// the prefix for another field path.
recurse = true
// We can reuse the same slice as long as we
// don't try to append to it.
parents = finfo.parents[:len(parents)+1]
break
}
}
if !recurse {
// We have no business with this element.
return false, nil
}
// The element is not a perfect match for any field, but one
// or more fields have the path to this element as a parent
// prefix. Recurse and attempt to match these.
for {
var tok Token
tok, err = p.Token()
if err != nil {
return true, err
}
switch t := tok.(type) {
case StartElement:
consumed2, err := p.unmarshalPath(tinfo, sv, parents, &t)
if err != nil {
return true, err
}
if !consumed2 {
if err := p.Skip(); err != nil {
return true, err
}
}
case EndElement:
return true, nil
}
}
} | go | func (p *Decoder) unmarshalPath(tinfo *typeInfo, sv reflect.Value, parents []string, start *StartElement) (consumed bool, err error) {
recurse := false
Loop:
for i := range tinfo.fields {
finfo := &tinfo.fields[i]
if finfo.flags&fElement == 0 || len(finfo.parents) < len(parents) || finfo.xmlns != "" && finfo.xmlns != start.Name.Space {
continue
}
for j := range parents {
if parents[j] != finfo.parents[j] {
continue Loop
}
}
if len(finfo.parents) == len(parents) && finfo.name == start.Name.Local {
// It's a perfect match, unmarshal the field.
return true, p.unmarshal(finfo.value(sv), start)
}
if len(finfo.parents) > len(parents) && finfo.parents[len(parents)] == start.Name.Local {
// It's a prefix for the field. Break and recurse
// since it's not ok for one field path to be itself
// the prefix for another field path.
recurse = true
// We can reuse the same slice as long as we
// don't try to append to it.
parents = finfo.parents[:len(parents)+1]
break
}
}
if !recurse {
// We have no business with this element.
return false, nil
}
// The element is not a perfect match for any field, but one
// or more fields have the path to this element as a parent
// prefix. Recurse and attempt to match these.
for {
var tok Token
tok, err = p.Token()
if err != nil {
return true, err
}
switch t := tok.(type) {
case StartElement:
consumed2, err := p.unmarshalPath(tinfo, sv, parents, &t)
if err != nil {
return true, err
}
if !consumed2 {
if err := p.Skip(); err != nil {
return true, err
}
}
case EndElement:
return true, nil
}
}
} | [
"func",
"(",
"p",
"*",
"Decoder",
")",
"unmarshalPath",
"(",
"tinfo",
"*",
"typeInfo",
",",
"sv",
"reflect",
".",
"Value",
",",
"parents",
"[",
"]",
"string",
",",
"start",
"*",
"StartElement",
")",
"(",
"consumed",
"bool",
",",
"err",
"error",
")",
"{",
"recurse",
":=",
"false",
"\n",
"Loop",
":",
"for",
"i",
":=",
"range",
"tinfo",
".",
"fields",
"{",
"finfo",
":=",
"&",
"tinfo",
".",
"fields",
"[",
"i",
"]",
"\n",
"if",
"finfo",
".",
"flags",
"&",
"fElement",
"==",
"0",
"||",
"len",
"(",
"finfo",
".",
"parents",
")",
"<",
"len",
"(",
"parents",
")",
"||",
"finfo",
".",
"xmlns",
"!=",
"\"",
"\"",
"&&",
"finfo",
".",
"xmlns",
"!=",
"start",
".",
"Name",
".",
"Space",
"{",
"continue",
"\n",
"}",
"\n",
"for",
"j",
":=",
"range",
"parents",
"{",
"if",
"parents",
"[",
"j",
"]",
"!=",
"finfo",
".",
"parents",
"[",
"j",
"]",
"{",
"continue",
"Loop",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"len",
"(",
"finfo",
".",
"parents",
")",
"==",
"len",
"(",
"parents",
")",
"&&",
"finfo",
".",
"name",
"==",
"start",
".",
"Name",
".",
"Local",
"{",
"// It's a perfect match, unmarshal the field.",
"return",
"true",
",",
"p",
".",
"unmarshal",
"(",
"finfo",
".",
"value",
"(",
"sv",
")",
",",
"start",
")",
"\n",
"}",
"\n",
"if",
"len",
"(",
"finfo",
".",
"parents",
")",
">",
"len",
"(",
"parents",
")",
"&&",
"finfo",
".",
"parents",
"[",
"len",
"(",
"parents",
")",
"]",
"==",
"start",
".",
"Name",
".",
"Local",
"{",
"// It's a prefix for the field. Break and recurse",
"// since it's not ok for one field path to be itself",
"// the prefix for another field path.",
"recurse",
"=",
"true",
"\n\n",
"// We can reuse the same slice as long as we",
"// don't try to append to it.",
"parents",
"=",
"finfo",
".",
"parents",
"[",
":",
"len",
"(",
"parents",
")",
"+",
"1",
"]",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"if",
"!",
"recurse",
"{",
"// We have no business with this element.",
"return",
"false",
",",
"nil",
"\n",
"}",
"\n",
"// The element is not a perfect match for any field, but one",
"// or more fields have the path to this element as a parent",
"// prefix. Recurse and attempt to match these.",
"for",
"{",
"var",
"tok",
"Token",
"\n",
"tok",
",",
"err",
"=",
"p",
".",
"Token",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"true",
",",
"err",
"\n",
"}",
"\n",
"switch",
"t",
":=",
"tok",
".",
"(",
"type",
")",
"{",
"case",
"StartElement",
":",
"consumed2",
",",
"err",
":=",
"p",
".",
"unmarshalPath",
"(",
"tinfo",
",",
"sv",
",",
"parents",
",",
"&",
"t",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"true",
",",
"err",
"\n",
"}",
"\n",
"if",
"!",
"consumed2",
"{",
"if",
"err",
":=",
"p",
".",
"Skip",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"true",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n",
"case",
"EndElement",
":",
"return",
"true",
",",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // unmarshalPath walks down an XML structure looking for wanted
// paths, and calls unmarshal on them.
// The consumed result tells whether XML elements have been consumed
// from the Decoder until start's matching end element, or if it's
// still untouched because start is uninteresting for sv's fields. | [
"unmarshalPath",
"walks",
"down",
"an",
"XML",
"structure",
"looking",
"for",
"wanted",
"paths",
"and",
"calls",
"unmarshal",
"on",
"them",
".",
"The",
"consumed",
"result",
"tells",
"whether",
"XML",
"elements",
"have",
"been",
"consumed",
"from",
"the",
"Decoder",
"until",
"start",
"s",
"matching",
"end",
"element",
"or",
"if",
"it",
"s",
"still",
"untouched",
"because",
"start",
"is",
"uninteresting",
"for",
"sv",
"s",
"fields",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/read.go#L701-L758 | train |
vmware/govmomi | vim25/xml/read.go | Skip | func (d *Decoder) Skip() error {
for {
tok, err := d.Token()
if err != nil {
return err
}
switch tok.(type) {
case StartElement:
if err := d.Skip(); err != nil {
return err
}
case EndElement:
return nil
}
}
} | go | func (d *Decoder) Skip() error {
for {
tok, err := d.Token()
if err != nil {
return err
}
switch tok.(type) {
case StartElement:
if err := d.Skip(); err != nil {
return err
}
case EndElement:
return nil
}
}
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"Skip",
"(",
")",
"error",
"{",
"for",
"{",
"tok",
",",
"err",
":=",
"d",
".",
"Token",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"switch",
"tok",
".",
"(",
"type",
")",
"{",
"case",
"StartElement",
":",
"if",
"err",
":=",
"d",
".",
"Skip",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"case",
"EndElement",
":",
"return",
"nil",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // Skip reads tokens until it has consumed the end element
// matching the most recent start element already consumed.
// It recurs if it encounters a start element, so it can be used to
// skip nested structures.
// It returns nil if it finds an end element matching the start
// element; otherwise it returns an error describing the problem. | [
"Skip",
"reads",
"tokens",
"until",
"it",
"has",
"consumed",
"the",
"end",
"element",
"matching",
"the",
"most",
"recent",
"start",
"element",
"already",
"consumed",
".",
"It",
"recurs",
"if",
"it",
"encounters",
"a",
"start",
"element",
"so",
"it",
"can",
"be",
"used",
"to",
"skip",
"nested",
"structures",
".",
"It",
"returns",
"nil",
"if",
"it",
"finds",
"an",
"end",
"element",
"matching",
"the",
"start",
"element",
";",
"otherwise",
"it",
"returns",
"an",
"error",
"describing",
"the",
"problem",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/read.go#L766-L781 | train |
vmware/govmomi | vapi/library/library_item_updatesession_file.go | AddLibraryItemFile | func (c *Manager) AddLibraryItemFile(ctx context.Context, sessionID string, updateFile UpdateFile) (*UpdateFileInfo, error) {
url := internal.URL(c, internal.LibraryItemUpdateSessionFile).WithID(sessionID).WithAction("add")
spec := struct {
FileSpec UpdateFile `json:"file_spec"`
}{updateFile}
var res UpdateFileInfo
return &res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | go | func (c *Manager) AddLibraryItemFile(ctx context.Context, sessionID string, updateFile UpdateFile) (*UpdateFileInfo, error) {
url := internal.URL(c, internal.LibraryItemUpdateSessionFile).WithID(sessionID).WithAction("add")
spec := struct {
FileSpec UpdateFile `json:"file_spec"`
}{updateFile}
var res UpdateFileInfo
return &res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"AddLibraryItemFile",
"(",
"ctx",
"context",
".",
"Context",
",",
"sessionID",
"string",
",",
"updateFile",
"UpdateFile",
")",
"(",
"*",
"UpdateFileInfo",
",",
"error",
")",
"{",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"LibraryItemUpdateSessionFile",
")",
".",
"WithID",
"(",
"sessionID",
")",
".",
"WithAction",
"(",
"\"",
"\"",
")",
"\n",
"spec",
":=",
"struct",
"{",
"FileSpec",
"UpdateFile",
"`json:\"file_spec\"`",
"\n",
"}",
"{",
"updateFile",
"}",
"\n",
"var",
"res",
"UpdateFileInfo",
"\n",
"return",
"&",
"res",
",",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodPost",
",",
"spec",
")",
",",
"&",
"res",
")",
"\n",
"}"
] | // AddLibraryItemFile adds a file | [
"AddLibraryItemFile",
"adds",
"a",
"file"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/library/library_item_updatesession_file.go#L59-L66 | train |
vmware/govmomi | vapi/library/library_item_updatesession_file.go | AddLibraryItemFileFromURI | func (c *Manager) AddLibraryItemFileFromURI(
ctx context.Context,
sessionID, fileName, uri string) (*UpdateFileInfo, error) {
n, fingerprint, err := GetContentLengthAndFingerprint(ctx, uri)
if err != nil {
return nil, err
}
info, err := c.AddLibraryItemFile(ctx, sessionID, UpdateFile{
Name: fileName,
SourceType: "PULL",
Size: &n,
SourceEndpoint: &SourceEndpoint{
URI: uri,
SSLCertificateThumbprint: fingerprint,
},
})
if err != nil {
return nil, err
}
return info, c.CompleteLibraryItemUpdateSession(ctx, sessionID)
} | go | func (c *Manager) AddLibraryItemFileFromURI(
ctx context.Context,
sessionID, fileName, uri string) (*UpdateFileInfo, error) {
n, fingerprint, err := GetContentLengthAndFingerprint(ctx, uri)
if err != nil {
return nil, err
}
info, err := c.AddLibraryItemFile(ctx, sessionID, UpdateFile{
Name: fileName,
SourceType: "PULL",
Size: &n,
SourceEndpoint: &SourceEndpoint{
URI: uri,
SSLCertificateThumbprint: fingerprint,
},
})
if err != nil {
return nil, err
}
return info, c.CompleteLibraryItemUpdateSession(ctx, sessionID)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"AddLibraryItemFileFromURI",
"(",
"ctx",
"context",
".",
"Context",
",",
"sessionID",
",",
"fileName",
",",
"uri",
"string",
")",
"(",
"*",
"UpdateFileInfo",
",",
"error",
")",
"{",
"n",
",",
"fingerprint",
",",
"err",
":=",
"GetContentLengthAndFingerprint",
"(",
"ctx",
",",
"uri",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"info",
",",
"err",
":=",
"c",
".",
"AddLibraryItemFile",
"(",
"ctx",
",",
"sessionID",
",",
"UpdateFile",
"{",
"Name",
":",
"fileName",
",",
"SourceType",
":",
"\"",
"\"",
",",
"Size",
":",
"&",
"n",
",",
"SourceEndpoint",
":",
"&",
"SourceEndpoint",
"{",
"URI",
":",
"uri",
",",
"SSLCertificateThumbprint",
":",
"fingerprint",
",",
"}",
",",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"info",
",",
"c",
".",
"CompleteLibraryItemUpdateSession",
"(",
"ctx",
",",
"sessionID",
")",
"\n",
"}"
] | // AddLibraryItemFileFromURI adds a file from a remote URI. | [
"AddLibraryItemFileFromURI",
"adds",
"a",
"file",
"from",
"a",
"remote",
"URI",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/library/library_item_updatesession_file.go#L69-L92 | train |
vmware/govmomi | vapi/library/library_item_updatesession_file.go | GetLibraryItemUpdateSessionFile | func (c *Manager) GetLibraryItemUpdateSessionFile(ctx context.Context, sessionID string, fileName string) (*UpdateFileInfo, error) {
url := internal.URL(c, internal.LibraryItemUpdateSessionFile).WithID(sessionID).WithAction("get")
spec := struct {
Name string `json:"file_name"`
}{fileName}
var res UpdateFileInfo
return &res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | go | func (c *Manager) GetLibraryItemUpdateSessionFile(ctx context.Context, sessionID string, fileName string) (*UpdateFileInfo, error) {
url := internal.URL(c, internal.LibraryItemUpdateSessionFile).WithID(sessionID).WithAction("get")
spec := struct {
Name string `json:"file_name"`
}{fileName}
var res UpdateFileInfo
return &res, c.Do(ctx, url.Request(http.MethodPost, spec), &res)
} | [
"func",
"(",
"c",
"*",
"Manager",
")",
"GetLibraryItemUpdateSessionFile",
"(",
"ctx",
"context",
".",
"Context",
",",
"sessionID",
"string",
",",
"fileName",
"string",
")",
"(",
"*",
"UpdateFileInfo",
",",
"error",
")",
"{",
"url",
":=",
"internal",
".",
"URL",
"(",
"c",
",",
"internal",
".",
"LibraryItemUpdateSessionFile",
")",
".",
"WithID",
"(",
"sessionID",
")",
".",
"WithAction",
"(",
"\"",
"\"",
")",
"\n",
"spec",
":=",
"struct",
"{",
"Name",
"string",
"`json:\"file_name\"`",
"\n",
"}",
"{",
"fileName",
"}",
"\n",
"var",
"res",
"UpdateFileInfo",
"\n",
"return",
"&",
"res",
",",
"c",
".",
"Do",
"(",
"ctx",
",",
"url",
".",
"Request",
"(",
"http",
".",
"MethodPost",
",",
"spec",
")",
",",
"&",
"res",
")",
"\n",
"}"
] | // GetLibraryItemUpdateSessionFile retrieves information about a specific file
// that is a part of an update session. | [
"GetLibraryItemUpdateSessionFile",
"retrieves",
"information",
"about",
"a",
"specific",
"file",
"that",
"is",
"a",
"part",
"of",
"an",
"update",
"session",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/library/library_item_updatesession_file.go#L96-L103 | train |
vmware/govmomi | vapi/library/library_item_updatesession_file.go | GetContentLengthAndFingerprint | func GetContentLengthAndFingerprint(
ctx context.Context, uri string) (int64, string, error) {
resp, err := http.Head(uri)
if err != nil {
return 0, "", err
}
if resp.TLS == nil || len(resp.TLS.PeerCertificates) == 0 {
return resp.ContentLength, "", nil
}
fingerprint := &bytes.Buffer{}
sum := sha1.Sum(resp.TLS.PeerCertificates[0].Raw)
for i, b := range sum {
fmt.Fprintf(fingerprint, "%X", b)
if i < len(sum)-1 {
fmt.Fprint(fingerprint, ":")
}
}
return resp.ContentLength, fingerprint.String(), nil
} | go | func GetContentLengthAndFingerprint(
ctx context.Context, uri string) (int64, string, error) {
resp, err := http.Head(uri)
if err != nil {
return 0, "", err
}
if resp.TLS == nil || len(resp.TLS.PeerCertificates) == 0 {
return resp.ContentLength, "", nil
}
fingerprint := &bytes.Buffer{}
sum := sha1.Sum(resp.TLS.PeerCertificates[0].Raw)
for i, b := range sum {
fmt.Fprintf(fingerprint, "%X", b)
if i < len(sum)-1 {
fmt.Fprint(fingerprint, ":")
}
}
return resp.ContentLength, fingerprint.String(), nil
} | [
"func",
"GetContentLengthAndFingerprint",
"(",
"ctx",
"context",
".",
"Context",
",",
"uri",
"string",
")",
"(",
"int64",
",",
"string",
",",
"error",
")",
"{",
"resp",
",",
"err",
":=",
"http",
".",
"Head",
"(",
"uri",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"0",
",",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n",
"if",
"resp",
".",
"TLS",
"==",
"nil",
"||",
"len",
"(",
"resp",
".",
"TLS",
".",
"PeerCertificates",
")",
"==",
"0",
"{",
"return",
"resp",
".",
"ContentLength",
",",
"\"",
"\"",
",",
"nil",
"\n",
"}",
"\n",
"fingerprint",
":=",
"&",
"bytes",
".",
"Buffer",
"{",
"}",
"\n",
"sum",
":=",
"sha1",
".",
"Sum",
"(",
"resp",
".",
"TLS",
".",
"PeerCertificates",
"[",
"0",
"]",
".",
"Raw",
")",
"\n",
"for",
"i",
",",
"b",
":=",
"range",
"sum",
"{",
"fmt",
".",
"Fprintf",
"(",
"fingerprint",
",",
"\"",
"\"",
",",
"b",
")",
"\n",
"if",
"i",
"<",
"len",
"(",
"sum",
")",
"-",
"1",
"{",
"fmt",
".",
"Fprint",
"(",
"fingerprint",
",",
"\"",
"\"",
")",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"resp",
".",
"ContentLength",
",",
"fingerprint",
".",
"String",
"(",
")",
",",
"nil",
"\n",
"}"
] | // GetContentLengthAndFingerprint gets the number of bytes returned
// by the URI as well as the SHA1 fingerprint of the peer certificate
// if the URI's scheme is https. | [
"GetContentLengthAndFingerprint",
"gets",
"the",
"number",
"of",
"bytes",
"returned",
"by",
"the",
"URI",
"as",
"well",
"as",
"the",
"SHA1",
"fingerprint",
"of",
"the",
"peer",
"certificate",
"if",
"the",
"URI",
"s",
"scheme",
"is",
"https",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vapi/library/library_item_updatesession_file.go#L108-L126 | train |
vmware/govmomi | simulator/event_manager.go | formatMessage | func (m *EventManager) formatMessage(event types.BaseEvent) {
id := reflect.ValueOf(event).Elem().Type().Name()
e := event.GetEvent()
t, ok := m.templates[id]
if !ok {
for _, info := range m.Description.EventInfo {
if info.Key == id {
t = template.Must(template.New(id).Parse(info.FullFormat))
m.templates[id] = t
break
}
}
}
if t != nil {
var buf bytes.Buffer
if err := t.Execute(&buf, event); err != nil {
log.Print(err)
}
e.FullFormattedMessage = buf.String()
}
if logEvents {
log.Printf("[%s] %s", id, e.FullFormattedMessage)
}
} | go | func (m *EventManager) formatMessage(event types.BaseEvent) {
id := reflect.ValueOf(event).Elem().Type().Name()
e := event.GetEvent()
t, ok := m.templates[id]
if !ok {
for _, info := range m.Description.EventInfo {
if info.Key == id {
t = template.Must(template.New(id).Parse(info.FullFormat))
m.templates[id] = t
break
}
}
}
if t != nil {
var buf bytes.Buffer
if err := t.Execute(&buf, event); err != nil {
log.Print(err)
}
e.FullFormattedMessage = buf.String()
}
if logEvents {
log.Printf("[%s] %s", id, e.FullFormattedMessage)
}
} | [
"func",
"(",
"m",
"*",
"EventManager",
")",
"formatMessage",
"(",
"event",
"types",
".",
"BaseEvent",
")",
"{",
"id",
":=",
"reflect",
".",
"ValueOf",
"(",
"event",
")",
".",
"Elem",
"(",
")",
".",
"Type",
"(",
")",
".",
"Name",
"(",
")",
"\n",
"e",
":=",
"event",
".",
"GetEvent",
"(",
")",
"\n\n",
"t",
",",
"ok",
":=",
"m",
".",
"templates",
"[",
"id",
"]",
"\n",
"if",
"!",
"ok",
"{",
"for",
"_",
",",
"info",
":=",
"range",
"m",
".",
"Description",
".",
"EventInfo",
"{",
"if",
"info",
".",
"Key",
"==",
"id",
"{",
"t",
"=",
"template",
".",
"Must",
"(",
"template",
".",
"New",
"(",
"id",
")",
".",
"Parse",
"(",
"info",
".",
"FullFormat",
")",
")",
"\n",
"m",
".",
"templates",
"[",
"id",
"]",
"=",
"t",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"t",
"!=",
"nil",
"{",
"var",
"buf",
"bytes",
".",
"Buffer",
"\n",
"if",
"err",
":=",
"t",
".",
"Execute",
"(",
"&",
"buf",
",",
"event",
")",
";",
"err",
"!=",
"nil",
"{",
"log",
".",
"Print",
"(",
"err",
")",
"\n",
"}",
"\n",
"e",
".",
"FullFormattedMessage",
"=",
"buf",
".",
"String",
"(",
")",
"\n",
"}",
"\n\n",
"if",
"logEvents",
"{",
"log",
".",
"Printf",
"(",
"\"",
"\"",
",",
"id",
",",
"e",
".",
"FullFormattedMessage",
")",
"\n",
"}",
"\n",
"}"
] | // formatMessage applies the EventDescriptionEventDetail.FullFormat template to the given event's FullFormattedMessage field. | [
"formatMessage",
"applies",
"the",
"EventDescriptionEventDetail",
".",
"FullFormat",
"template",
"to",
"the",
"given",
"event",
"s",
"FullFormattedMessage",
"field",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L126-L152 | train |
vmware/govmomi | simulator/event_manager.go | doEntityEventArgument | func doEntityEventArgument(event types.BaseEvent, f func(types.ManagedObjectReference, *types.EntityEventArgument) bool) bool {
e := event.GetEvent()
if arg := e.Vm; arg != nil {
if f(arg.Vm, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Host; arg != nil {
if f(arg.Host, &arg.EntityEventArgument) {
return true
}
}
if arg := e.ComputeResource; arg != nil {
if f(arg.ComputeResource, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Ds; arg != nil {
if f(arg.Datastore, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Net; arg != nil {
if f(arg.Network, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Dvs; arg != nil {
if f(arg.Dvs, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Datacenter; arg != nil {
if f(arg.Datacenter, &arg.EntityEventArgument) {
return true
}
}
return false
} | go | func doEntityEventArgument(event types.BaseEvent, f func(types.ManagedObjectReference, *types.EntityEventArgument) bool) bool {
e := event.GetEvent()
if arg := e.Vm; arg != nil {
if f(arg.Vm, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Host; arg != nil {
if f(arg.Host, &arg.EntityEventArgument) {
return true
}
}
if arg := e.ComputeResource; arg != nil {
if f(arg.ComputeResource, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Ds; arg != nil {
if f(arg.Datastore, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Net; arg != nil {
if f(arg.Network, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Dvs; arg != nil {
if f(arg.Dvs, &arg.EntityEventArgument) {
return true
}
}
if arg := e.Datacenter; arg != nil {
if f(arg.Datacenter, &arg.EntityEventArgument) {
return true
}
}
return false
} | [
"func",
"doEntityEventArgument",
"(",
"event",
"types",
".",
"BaseEvent",
",",
"f",
"func",
"(",
"types",
".",
"ManagedObjectReference",
",",
"*",
"types",
".",
"EntityEventArgument",
")",
"bool",
")",
"bool",
"{",
"e",
":=",
"event",
".",
"GetEvent",
"(",
")",
"\n\n",
"if",
"arg",
":=",
"e",
".",
"Vm",
";",
"arg",
"!=",
"nil",
"{",
"if",
"f",
"(",
"arg",
".",
"Vm",
",",
"&",
"arg",
".",
"EntityEventArgument",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"arg",
":=",
"e",
".",
"Host",
";",
"arg",
"!=",
"nil",
"{",
"if",
"f",
"(",
"arg",
".",
"Host",
",",
"&",
"arg",
".",
"EntityEventArgument",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"arg",
":=",
"e",
".",
"ComputeResource",
";",
"arg",
"!=",
"nil",
"{",
"if",
"f",
"(",
"arg",
".",
"ComputeResource",
",",
"&",
"arg",
".",
"EntityEventArgument",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"arg",
":=",
"e",
".",
"Ds",
";",
"arg",
"!=",
"nil",
"{",
"if",
"f",
"(",
"arg",
".",
"Datastore",
",",
"&",
"arg",
".",
"EntityEventArgument",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"arg",
":=",
"e",
".",
"Net",
";",
"arg",
"!=",
"nil",
"{",
"if",
"f",
"(",
"arg",
".",
"Network",
",",
"&",
"arg",
".",
"EntityEventArgument",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"arg",
":=",
"e",
".",
"Dvs",
";",
"arg",
"!=",
"nil",
"{",
"if",
"f",
"(",
"arg",
".",
"Dvs",
",",
"&",
"arg",
".",
"EntityEventArgument",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"if",
"arg",
":=",
"e",
".",
"Datacenter",
";",
"arg",
"!=",
"nil",
"{",
"if",
"f",
"(",
"arg",
".",
"Datacenter",
",",
"&",
"arg",
".",
"EntityEventArgument",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // doEntityEventArgument calls f for each entity argument in the event.
// If f returns true, the iteration stops. | [
"doEntityEventArgument",
"calls",
"f",
"for",
"each",
"entity",
"argument",
"in",
"the",
"event",
".",
"If",
"f",
"returns",
"true",
"the",
"iteration",
"stops",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L191-L237 | train |
vmware/govmomi | simulator/event_manager.go | eventFilterSelf | func eventFilterSelf(event types.BaseEvent, self types.ManagedObjectReference) bool {
return doEntityEventArgument(event, func(ref types.ManagedObjectReference, _ *types.EntityEventArgument) bool {
return self == ref
})
} | go | func eventFilterSelf(event types.BaseEvent, self types.ManagedObjectReference) bool {
return doEntityEventArgument(event, func(ref types.ManagedObjectReference, _ *types.EntityEventArgument) bool {
return self == ref
})
} | [
"func",
"eventFilterSelf",
"(",
"event",
"types",
".",
"BaseEvent",
",",
"self",
"types",
".",
"ManagedObjectReference",
")",
"bool",
"{",
"return",
"doEntityEventArgument",
"(",
"event",
",",
"func",
"(",
"ref",
"types",
".",
"ManagedObjectReference",
",",
"_",
"*",
"types",
".",
"EntityEventArgument",
")",
"bool",
"{",
"return",
"self",
"==",
"ref",
"\n",
"}",
")",
"\n",
"}"
] | // eventFilterSelf returns true if self is one of the entity arguments in the event. | [
"eventFilterSelf",
"returns",
"true",
"if",
"self",
"is",
"one",
"of",
"the",
"entity",
"arguments",
"in",
"the",
"event",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L240-L244 | train |
vmware/govmomi | simulator/event_manager.go | eventFilterChildren | func eventFilterChildren(event types.BaseEvent, self types.ManagedObjectReference) bool {
return doEntityEventArgument(event, func(ref types.ManagedObjectReference, _ *types.EntityEventArgument) bool {
seen := false
var match func(types.ManagedObjectReference)
match = func(child types.ManagedObjectReference) {
if child == self {
seen = true
return
}
walk(child, match)
}
walk(ref, match)
return seen
})
} | go | func eventFilterChildren(event types.BaseEvent, self types.ManagedObjectReference) bool {
return doEntityEventArgument(event, func(ref types.ManagedObjectReference, _ *types.EntityEventArgument) bool {
seen := false
var match func(types.ManagedObjectReference)
match = func(child types.ManagedObjectReference) {
if child == self {
seen = true
return
}
walk(child, match)
}
walk(ref, match)
return seen
})
} | [
"func",
"eventFilterChildren",
"(",
"event",
"types",
".",
"BaseEvent",
",",
"self",
"types",
".",
"ManagedObjectReference",
")",
"bool",
"{",
"return",
"doEntityEventArgument",
"(",
"event",
",",
"func",
"(",
"ref",
"types",
".",
"ManagedObjectReference",
",",
"_",
"*",
"types",
".",
"EntityEventArgument",
")",
"bool",
"{",
"seen",
":=",
"false",
"\n\n",
"var",
"match",
"func",
"(",
"types",
".",
"ManagedObjectReference",
")",
"\n\n",
"match",
"=",
"func",
"(",
"child",
"types",
".",
"ManagedObjectReference",
")",
"{",
"if",
"child",
"==",
"self",
"{",
"seen",
"=",
"true",
"\n",
"return",
"\n",
"}",
"\n\n",
"walk",
"(",
"child",
",",
"match",
")",
"\n",
"}",
"\n\n",
"walk",
"(",
"ref",
",",
"match",
")",
"\n\n",
"return",
"seen",
"\n",
"}",
")",
"\n",
"}"
] | // eventFilterChildren returns true if a child of self is one of the entity arguments in the event. | [
"eventFilterChildren",
"returns",
"true",
"if",
"a",
"child",
"of",
"self",
"is",
"one",
"of",
"the",
"entity",
"arguments",
"in",
"the",
"event",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L247-L266 | train |
vmware/govmomi | simulator/event_manager.go | entityMatches | func (c *EventHistoryCollector) entityMatches(event types.BaseEvent, spec *types.EventFilterSpec) bool {
e := spec.Entity
if e == nil {
return true
}
isRootFolder := c.m.root == e.Entity
switch e.Recursion {
case types.EventFilterSpecRecursionOptionSelf:
return isRootFolder || eventFilterSelf(event, e.Entity)
case types.EventFilterSpecRecursionOptionChildren:
return eventFilterChildren(event, e.Entity)
case types.EventFilterSpecRecursionOptionAll:
if isRootFolder || eventFilterSelf(event, e.Entity) {
return true
}
return eventFilterChildren(event, e.Entity)
}
return false
} | go | func (c *EventHistoryCollector) entityMatches(event types.BaseEvent, spec *types.EventFilterSpec) bool {
e := spec.Entity
if e == nil {
return true
}
isRootFolder := c.m.root == e.Entity
switch e.Recursion {
case types.EventFilterSpecRecursionOptionSelf:
return isRootFolder || eventFilterSelf(event, e.Entity)
case types.EventFilterSpecRecursionOptionChildren:
return eventFilterChildren(event, e.Entity)
case types.EventFilterSpecRecursionOptionAll:
if isRootFolder || eventFilterSelf(event, e.Entity) {
return true
}
return eventFilterChildren(event, e.Entity)
}
return false
} | [
"func",
"(",
"c",
"*",
"EventHistoryCollector",
")",
"entityMatches",
"(",
"event",
"types",
".",
"BaseEvent",
",",
"spec",
"*",
"types",
".",
"EventFilterSpec",
")",
"bool",
"{",
"e",
":=",
"spec",
".",
"Entity",
"\n",
"if",
"e",
"==",
"nil",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"isRootFolder",
":=",
"c",
".",
"m",
".",
"root",
"==",
"e",
".",
"Entity",
"\n\n",
"switch",
"e",
".",
"Recursion",
"{",
"case",
"types",
".",
"EventFilterSpecRecursionOptionSelf",
":",
"return",
"isRootFolder",
"||",
"eventFilterSelf",
"(",
"event",
",",
"e",
".",
"Entity",
")",
"\n",
"case",
"types",
".",
"EventFilterSpecRecursionOptionChildren",
":",
"return",
"eventFilterChildren",
"(",
"event",
",",
"e",
".",
"Entity",
")",
"\n",
"case",
"types",
".",
"EventFilterSpecRecursionOptionAll",
":",
"if",
"isRootFolder",
"||",
"eventFilterSelf",
"(",
"event",
",",
"e",
".",
"Entity",
")",
"{",
"return",
"true",
"\n",
"}",
"\n",
"return",
"eventFilterChildren",
"(",
"event",
",",
"e",
".",
"Entity",
")",
"\n",
"}",
"\n\n",
"return",
"false",
"\n",
"}"
] | // entityMatches returns true if the spec Entity filter matches the event. | [
"entityMatches",
"returns",
"true",
"if",
"the",
"spec",
"Entity",
"filter",
"matches",
"the",
"event",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L269-L290 | train |
vmware/govmomi | simulator/event_manager.go | typeMatches | func (c *EventHistoryCollector) typeMatches(event types.BaseEvent, spec *types.EventFilterSpec) bool {
if len(spec.EventTypeId) == 0 {
return true
}
matches := func(name string) bool {
for _, id := range spec.EventTypeId {
if id == name {
return true
}
}
return false
}
kind := reflect.ValueOf(event).Elem().Type()
if matches(kind.Name()) {
return true // concrete type
}
field, ok := kind.FieldByNameFunc(matches)
if ok {
return field.Anonymous // base type (embedded field)
}
return false
} | go | func (c *EventHistoryCollector) typeMatches(event types.BaseEvent, spec *types.EventFilterSpec) bool {
if len(spec.EventTypeId) == 0 {
return true
}
matches := func(name string) bool {
for _, id := range spec.EventTypeId {
if id == name {
return true
}
}
return false
}
kind := reflect.ValueOf(event).Elem().Type()
if matches(kind.Name()) {
return true // concrete type
}
field, ok := kind.FieldByNameFunc(matches)
if ok {
return field.Anonymous // base type (embedded field)
}
return false
} | [
"func",
"(",
"c",
"*",
"EventHistoryCollector",
")",
"typeMatches",
"(",
"event",
"types",
".",
"BaseEvent",
",",
"spec",
"*",
"types",
".",
"EventFilterSpec",
")",
"bool",
"{",
"if",
"len",
"(",
"spec",
".",
"EventTypeId",
")",
"==",
"0",
"{",
"return",
"true",
"\n",
"}",
"\n\n",
"matches",
":=",
"func",
"(",
"name",
"string",
")",
"bool",
"{",
"for",
"_",
",",
"id",
":=",
"range",
"spec",
".",
"EventTypeId",
"{",
"if",
"id",
"==",
"name",
"{",
"return",
"true",
"\n",
"}",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}",
"\n",
"kind",
":=",
"reflect",
".",
"ValueOf",
"(",
"event",
")",
".",
"Elem",
"(",
")",
".",
"Type",
"(",
")",
"\n\n",
"if",
"matches",
"(",
"kind",
".",
"Name",
"(",
")",
")",
"{",
"return",
"true",
"// concrete type",
"\n",
"}",
"\n\n",
"field",
",",
"ok",
":=",
"kind",
".",
"FieldByNameFunc",
"(",
"matches",
")",
"\n",
"if",
"ok",
"{",
"return",
"field",
".",
"Anonymous",
"// base type (embedded field)",
"\n",
"}",
"\n",
"return",
"false",
"\n",
"}"
] | // typeMatches returns true if one of the spec EventTypeId types matches the event. | [
"typeMatches",
"returns",
"true",
"if",
"one",
"of",
"the",
"spec",
"EventTypeId",
"types",
"matches",
"the",
"event",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L293-L317 | train |
vmware/govmomi | simulator/event_manager.go | eventMatches | func (c *EventHistoryCollector) eventMatches(event types.BaseEvent) bool {
spec := c.Filter.(types.EventFilterSpec)
if !c.typeMatches(event, &spec) {
return false
}
// TODO: spec.Time, spec.UserName, etc
return c.entityMatches(event, &spec)
} | go | func (c *EventHistoryCollector) eventMatches(event types.BaseEvent) bool {
spec := c.Filter.(types.EventFilterSpec)
if !c.typeMatches(event, &spec) {
return false
}
// TODO: spec.Time, spec.UserName, etc
return c.entityMatches(event, &spec)
} | [
"func",
"(",
"c",
"*",
"EventHistoryCollector",
")",
"eventMatches",
"(",
"event",
"types",
".",
"BaseEvent",
")",
"bool",
"{",
"spec",
":=",
"c",
".",
"Filter",
".",
"(",
"types",
".",
"EventFilterSpec",
")",
"\n\n",
"if",
"!",
"c",
".",
"typeMatches",
"(",
"event",
",",
"&",
"spec",
")",
"{",
"return",
"false",
"\n",
"}",
"\n\n",
"// TODO: spec.Time, spec.UserName, etc",
"return",
"c",
".",
"entityMatches",
"(",
"event",
",",
"&",
"spec",
")",
"\n",
"}"
] | // eventMatches returns true one of the filters matches the event. | [
"eventMatches",
"returns",
"true",
"one",
"of",
"the",
"filters",
"matches",
"the",
"event",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L320-L330 | train |
vmware/govmomi | simulator/event_manager.go | fillPage | func (c *EventHistoryCollector) fillPage(size int) {
c.pos = 0
l := c.page.Len()
delta := size - l
if delta < 0 {
// Shrink ring size
c.page = c.page.Unlink(-delta)
return
}
matches := 0
mpage := c.m.page
page := c.page
if delta != 0 {
// Grow ring size
c.page = c.page.Link(ring.New(delta))
}
for i := 0; i < maxPageSize; i++ {
event, ok := mpage.Value.(types.BaseEvent)
mpage = mpage.Prev()
if !ok {
continue
}
if c.eventMatches(event) {
page.Value = event
page = page.Prev()
matches++
if matches == size {
break
}
}
}
} | go | func (c *EventHistoryCollector) fillPage(size int) {
c.pos = 0
l := c.page.Len()
delta := size - l
if delta < 0 {
// Shrink ring size
c.page = c.page.Unlink(-delta)
return
}
matches := 0
mpage := c.m.page
page := c.page
if delta != 0 {
// Grow ring size
c.page = c.page.Link(ring.New(delta))
}
for i := 0; i < maxPageSize; i++ {
event, ok := mpage.Value.(types.BaseEvent)
mpage = mpage.Prev()
if !ok {
continue
}
if c.eventMatches(event) {
page.Value = event
page = page.Prev()
matches++
if matches == size {
break
}
}
}
} | [
"func",
"(",
"c",
"*",
"EventHistoryCollector",
")",
"fillPage",
"(",
"size",
"int",
")",
"{",
"c",
".",
"pos",
"=",
"0",
"\n",
"l",
":=",
"c",
".",
"page",
".",
"Len",
"(",
")",
"\n",
"delta",
":=",
"size",
"-",
"l",
"\n\n",
"if",
"delta",
"<",
"0",
"{",
"// Shrink ring size",
"c",
".",
"page",
"=",
"c",
".",
"page",
".",
"Unlink",
"(",
"-",
"delta",
")",
"\n",
"return",
"\n",
"}",
"\n\n",
"matches",
":=",
"0",
"\n",
"mpage",
":=",
"c",
".",
"m",
".",
"page",
"\n",
"page",
":=",
"c",
".",
"page",
"\n\n",
"if",
"delta",
"!=",
"0",
"{",
"// Grow ring size",
"c",
".",
"page",
"=",
"c",
".",
"page",
".",
"Link",
"(",
"ring",
".",
"New",
"(",
"delta",
")",
")",
"\n",
"}",
"\n\n",
"for",
"i",
":=",
"0",
";",
"i",
"<",
"maxPageSize",
";",
"i",
"++",
"{",
"event",
",",
"ok",
":=",
"mpage",
".",
"Value",
".",
"(",
"types",
".",
"BaseEvent",
")",
"\n",
"mpage",
"=",
"mpage",
".",
"Prev",
"(",
")",
"\n",
"if",
"!",
"ok",
"{",
"continue",
"\n",
"}",
"\n\n",
"if",
"c",
".",
"eventMatches",
"(",
"event",
")",
"{",
"page",
".",
"Value",
"=",
"event",
"\n",
"page",
"=",
"page",
".",
"Prev",
"(",
")",
"\n",
"matches",
"++",
"\n",
"if",
"matches",
"==",
"size",
"{",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // filePage copies the manager's latest events into the collector's page with Filter applied. | [
"filePage",
"copies",
"the",
"manager",
"s",
"latest",
"events",
"into",
"the",
"collector",
"s",
"page",
"with",
"Filter",
"applied",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/simulator/event_manager.go#L333-L369 | train |
vmware/govmomi | object/virtual_disk_manager.go | CopyVirtualDisk | func (m VirtualDiskManager) CopyVirtualDisk(
ctx context.Context,
sourceName string, sourceDatacenter *Datacenter,
destName string, destDatacenter *Datacenter,
destSpec *types.VirtualDiskSpec, force bool) (*Task, error) {
req := types.CopyVirtualDisk_Task{
This: m.Reference(),
SourceName: sourceName,
DestName: destName,
DestSpec: destSpec,
Force: types.NewBool(force),
}
if sourceDatacenter != nil {
ref := sourceDatacenter.Reference()
req.SourceDatacenter = &ref
}
if destDatacenter != nil {
ref := destDatacenter.Reference()
req.DestDatacenter = &ref
}
res, err := methods.CopyVirtualDisk_Task(ctx, m.c, &req)
if err != nil {
return nil, err
}
return NewTask(m.c, res.Returnval), nil
} | go | func (m VirtualDiskManager) CopyVirtualDisk(
ctx context.Context,
sourceName string, sourceDatacenter *Datacenter,
destName string, destDatacenter *Datacenter,
destSpec *types.VirtualDiskSpec, force bool) (*Task, error) {
req := types.CopyVirtualDisk_Task{
This: m.Reference(),
SourceName: sourceName,
DestName: destName,
DestSpec: destSpec,
Force: types.NewBool(force),
}
if sourceDatacenter != nil {
ref := sourceDatacenter.Reference()
req.SourceDatacenter = &ref
}
if destDatacenter != nil {
ref := destDatacenter.Reference()
req.DestDatacenter = &ref
}
res, err := methods.CopyVirtualDisk_Task(ctx, m.c, &req)
if err != nil {
return nil, err
}
return NewTask(m.c, res.Returnval), nil
} | [
"func",
"(",
"m",
"VirtualDiskManager",
")",
"CopyVirtualDisk",
"(",
"ctx",
"context",
".",
"Context",
",",
"sourceName",
"string",
",",
"sourceDatacenter",
"*",
"Datacenter",
",",
"destName",
"string",
",",
"destDatacenter",
"*",
"Datacenter",
",",
"destSpec",
"*",
"types",
".",
"VirtualDiskSpec",
",",
"force",
"bool",
")",
"(",
"*",
"Task",
",",
"error",
")",
"{",
"req",
":=",
"types",
".",
"CopyVirtualDisk_Task",
"{",
"This",
":",
"m",
".",
"Reference",
"(",
")",
",",
"SourceName",
":",
"sourceName",
",",
"DestName",
":",
"destName",
",",
"DestSpec",
":",
"destSpec",
",",
"Force",
":",
"types",
".",
"NewBool",
"(",
"force",
")",
",",
"}",
"\n\n",
"if",
"sourceDatacenter",
"!=",
"nil",
"{",
"ref",
":=",
"sourceDatacenter",
".",
"Reference",
"(",
")",
"\n",
"req",
".",
"SourceDatacenter",
"=",
"&",
"ref",
"\n",
"}",
"\n\n",
"if",
"destDatacenter",
"!=",
"nil",
"{",
"ref",
":=",
"destDatacenter",
".",
"Reference",
"(",
")",
"\n",
"req",
".",
"DestDatacenter",
"=",
"&",
"ref",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"methods",
".",
"CopyVirtualDisk_Task",
"(",
"ctx",
",",
"m",
".",
"c",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"NewTask",
"(",
"m",
".",
"c",
",",
"res",
".",
"Returnval",
")",
",",
"nil",
"\n",
"}"
] | // CopyVirtualDisk copies a virtual disk, performing conversions as specified in the spec. | [
"CopyVirtualDisk",
"copies",
"a",
"virtual",
"disk",
"performing",
"conversions",
"as",
"specified",
"in",
"the",
"spec",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/virtual_disk_manager.go#L40-L70 | train |
vmware/govmomi | object/virtual_disk_manager.go | CreateVirtualDisk | func (m VirtualDiskManager) CreateVirtualDisk(
ctx context.Context,
name string, datacenter *Datacenter,
spec types.BaseVirtualDiskSpec) (*Task, error) {
req := types.CreateVirtualDisk_Task{
This: m.Reference(),
Name: name,
Spec: spec,
}
if datacenter != nil {
ref := datacenter.Reference()
req.Datacenter = &ref
}
res, err := methods.CreateVirtualDisk_Task(ctx, m.c, &req)
if err != nil {
return nil, err
}
return NewTask(m.c, res.Returnval), nil
} | go | func (m VirtualDiskManager) CreateVirtualDisk(
ctx context.Context,
name string, datacenter *Datacenter,
spec types.BaseVirtualDiskSpec) (*Task, error) {
req := types.CreateVirtualDisk_Task{
This: m.Reference(),
Name: name,
Spec: spec,
}
if datacenter != nil {
ref := datacenter.Reference()
req.Datacenter = &ref
}
res, err := methods.CreateVirtualDisk_Task(ctx, m.c, &req)
if err != nil {
return nil, err
}
return NewTask(m.c, res.Returnval), nil
} | [
"func",
"(",
"m",
"VirtualDiskManager",
")",
"CreateVirtualDisk",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
",",
"datacenter",
"*",
"Datacenter",
",",
"spec",
"types",
".",
"BaseVirtualDiskSpec",
")",
"(",
"*",
"Task",
",",
"error",
")",
"{",
"req",
":=",
"types",
".",
"CreateVirtualDisk_Task",
"{",
"This",
":",
"m",
".",
"Reference",
"(",
")",
",",
"Name",
":",
"name",
",",
"Spec",
":",
"spec",
",",
"}",
"\n\n",
"if",
"datacenter",
"!=",
"nil",
"{",
"ref",
":=",
"datacenter",
".",
"Reference",
"(",
")",
"\n",
"req",
".",
"Datacenter",
"=",
"&",
"ref",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"methods",
".",
"CreateVirtualDisk_Task",
"(",
"ctx",
",",
"m",
".",
"c",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"NewTask",
"(",
"m",
".",
"c",
",",
"res",
".",
"Returnval",
")",
",",
"nil",
"\n",
"}"
] | // CreateVirtualDisk creates a new virtual disk. | [
"CreateVirtualDisk",
"creates",
"a",
"new",
"virtual",
"disk",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/virtual_disk_manager.go#L73-L95 | train |
vmware/govmomi | object/virtual_disk_manager.go | ShrinkVirtualDisk | func (m VirtualDiskManager) ShrinkVirtualDisk(ctx context.Context, name string, dc *Datacenter, copy *bool) (*Task, error) {
req := types.ShrinkVirtualDisk_Task{
This: m.Reference(),
Name: name,
Copy: copy,
}
if dc != nil {
ref := dc.Reference()
req.Datacenter = &ref
}
res, err := methods.ShrinkVirtualDisk_Task(ctx, m.c, &req)
if err != nil {
return nil, err
}
return NewTask(m.c, res.Returnval), nil
} | go | func (m VirtualDiskManager) ShrinkVirtualDisk(ctx context.Context, name string, dc *Datacenter, copy *bool) (*Task, error) {
req := types.ShrinkVirtualDisk_Task{
This: m.Reference(),
Name: name,
Copy: copy,
}
if dc != nil {
ref := dc.Reference()
req.Datacenter = &ref
}
res, err := methods.ShrinkVirtualDisk_Task(ctx, m.c, &req)
if err != nil {
return nil, err
}
return NewTask(m.c, res.Returnval), nil
} | [
"func",
"(",
"m",
"VirtualDiskManager",
")",
"ShrinkVirtualDisk",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
",",
"dc",
"*",
"Datacenter",
",",
"copy",
"*",
"bool",
")",
"(",
"*",
"Task",
",",
"error",
")",
"{",
"req",
":=",
"types",
".",
"ShrinkVirtualDisk_Task",
"{",
"This",
":",
"m",
".",
"Reference",
"(",
")",
",",
"Name",
":",
"name",
",",
"Copy",
":",
"copy",
",",
"}",
"\n\n",
"if",
"dc",
"!=",
"nil",
"{",
"ref",
":=",
"dc",
".",
"Reference",
"(",
")",
"\n",
"req",
".",
"Datacenter",
"=",
"&",
"ref",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"methods",
".",
"ShrinkVirtualDisk_Task",
"(",
"ctx",
",",
"m",
".",
"c",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"NewTask",
"(",
"m",
".",
"c",
",",
"res",
".",
"Returnval",
")",
",",
"nil",
"\n",
"}"
] | // ShrinkVirtualDisk shrinks a virtual disk. | [
"ShrinkVirtualDisk",
"shrinks",
"a",
"virtual",
"disk",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/virtual_disk_manager.go#L169-L187 | train |
vmware/govmomi | object/virtual_disk_manager.go | QueryVirtualDiskUuid | func (m VirtualDiskManager) QueryVirtualDiskUuid(ctx context.Context, name string, dc *Datacenter) (string, error) {
req := types.QueryVirtualDiskUuid{
This: m.Reference(),
Name: name,
}
if dc != nil {
ref := dc.Reference()
req.Datacenter = &ref
}
res, err := methods.QueryVirtualDiskUuid(ctx, m.c, &req)
if err != nil {
return "", err
}
if res == nil {
return "", nil
}
return res.Returnval, nil
} | go | func (m VirtualDiskManager) QueryVirtualDiskUuid(ctx context.Context, name string, dc *Datacenter) (string, error) {
req := types.QueryVirtualDiskUuid{
This: m.Reference(),
Name: name,
}
if dc != nil {
ref := dc.Reference()
req.Datacenter = &ref
}
res, err := methods.QueryVirtualDiskUuid(ctx, m.c, &req)
if err != nil {
return "", err
}
if res == nil {
return "", nil
}
return res.Returnval, nil
} | [
"func",
"(",
"m",
"VirtualDiskManager",
")",
"QueryVirtualDiskUuid",
"(",
"ctx",
"context",
".",
"Context",
",",
"name",
"string",
",",
"dc",
"*",
"Datacenter",
")",
"(",
"string",
",",
"error",
")",
"{",
"req",
":=",
"types",
".",
"QueryVirtualDiskUuid",
"{",
"This",
":",
"m",
".",
"Reference",
"(",
")",
",",
"Name",
":",
"name",
",",
"}",
"\n\n",
"if",
"dc",
"!=",
"nil",
"{",
"ref",
":=",
"dc",
".",
"Reference",
"(",
")",
"\n",
"req",
".",
"Datacenter",
"=",
"&",
"ref",
"\n",
"}",
"\n\n",
"res",
",",
"err",
":=",
"methods",
".",
"QueryVirtualDiskUuid",
"(",
"ctx",
",",
"m",
".",
"c",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"res",
"==",
"nil",
"{",
"return",
"\"",
"\"",
",",
"nil",
"\n",
"}",
"\n\n",
"return",
"res",
".",
"Returnval",
",",
"nil",
"\n",
"}"
] | // Queries virtual disk uuid | [
"Queries",
"virtual",
"disk",
"uuid"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/virtual_disk_manager.go#L190-L211 | train |
vmware/govmomi | toolbox/service.go | NewService | func NewService(rpcIn Channel, rpcOut Channel) *Service {
s := &Service{
name: "toolbox", // Same name used by vmtoolsd
in: NewTraceChannel(rpcIn),
out: &ChannelOut{NewTraceChannel(rpcOut)},
handlers: make(map[string]Handler),
wg: new(sync.WaitGroup),
stop: make(chan struct{}),
PrimaryIP: DefaultIP,
}
s.RegisterHandler("reset", s.Reset)
s.RegisterHandler("ping", s.Ping)
s.RegisterHandler("Set_Option", s.SetOption)
s.RegisterHandler("Capabilities_Register", s.CapabilitiesRegister)
s.Command = registerCommandServer(s)
s.Command.FileServer = hgfs.NewServer()
s.Command.FileServer.RegisterFileHandler("proc", s.Command.ProcessManager)
s.Command.FileServer.RegisterFileHandler(hgfs.ArchiveScheme, hgfs.NewArchiveHandler())
s.Power = registerPowerCommandHandler(s)
return s
} | go | func NewService(rpcIn Channel, rpcOut Channel) *Service {
s := &Service{
name: "toolbox", // Same name used by vmtoolsd
in: NewTraceChannel(rpcIn),
out: &ChannelOut{NewTraceChannel(rpcOut)},
handlers: make(map[string]Handler),
wg: new(sync.WaitGroup),
stop: make(chan struct{}),
PrimaryIP: DefaultIP,
}
s.RegisterHandler("reset", s.Reset)
s.RegisterHandler("ping", s.Ping)
s.RegisterHandler("Set_Option", s.SetOption)
s.RegisterHandler("Capabilities_Register", s.CapabilitiesRegister)
s.Command = registerCommandServer(s)
s.Command.FileServer = hgfs.NewServer()
s.Command.FileServer.RegisterFileHandler("proc", s.Command.ProcessManager)
s.Command.FileServer.RegisterFileHandler(hgfs.ArchiveScheme, hgfs.NewArchiveHandler())
s.Power = registerPowerCommandHandler(s)
return s
} | [
"func",
"NewService",
"(",
"rpcIn",
"Channel",
",",
"rpcOut",
"Channel",
")",
"*",
"Service",
"{",
"s",
":=",
"&",
"Service",
"{",
"name",
":",
"\"",
"\"",
",",
"// Same name used by vmtoolsd",
"in",
":",
"NewTraceChannel",
"(",
"rpcIn",
")",
",",
"out",
":",
"&",
"ChannelOut",
"{",
"NewTraceChannel",
"(",
"rpcOut",
")",
"}",
",",
"handlers",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"Handler",
")",
",",
"wg",
":",
"new",
"(",
"sync",
".",
"WaitGroup",
")",
",",
"stop",
":",
"make",
"(",
"chan",
"struct",
"{",
"}",
")",
",",
"PrimaryIP",
":",
"DefaultIP",
",",
"}",
"\n\n",
"s",
".",
"RegisterHandler",
"(",
"\"",
"\"",
",",
"s",
".",
"Reset",
")",
"\n",
"s",
".",
"RegisterHandler",
"(",
"\"",
"\"",
",",
"s",
".",
"Ping",
")",
"\n",
"s",
".",
"RegisterHandler",
"(",
"\"",
"\"",
",",
"s",
".",
"SetOption",
")",
"\n",
"s",
".",
"RegisterHandler",
"(",
"\"",
"\"",
",",
"s",
".",
"CapabilitiesRegister",
")",
"\n\n",
"s",
".",
"Command",
"=",
"registerCommandServer",
"(",
"s",
")",
"\n",
"s",
".",
"Command",
".",
"FileServer",
"=",
"hgfs",
".",
"NewServer",
"(",
")",
"\n",
"s",
".",
"Command",
".",
"FileServer",
".",
"RegisterFileHandler",
"(",
"\"",
"\"",
",",
"s",
".",
"Command",
".",
"ProcessManager",
")",
"\n",
"s",
".",
"Command",
".",
"FileServer",
".",
"RegisterFileHandler",
"(",
"hgfs",
".",
"ArchiveScheme",
",",
"hgfs",
".",
"NewArchiveHandler",
"(",
")",
")",
"\n\n",
"s",
".",
"Power",
"=",
"registerPowerCommandHandler",
"(",
"s",
")",
"\n\n",
"return",
"s",
"\n",
"}"
] | // NewService initializes a Service instance | [
"NewService",
"initializes",
"a",
"Service",
"instance"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L75-L100 | train |
vmware/govmomi | toolbox/service.go | backoff | func (s *Service) backoff() {
if s.delay < maxDelay {
if s.delay > 0 {
d := s.delay * 2
if d > s.delay && d < maxDelay {
s.delay = d
} else {
s.delay = maxDelay
}
} else {
s.delay = 1
}
}
} | go | func (s *Service) backoff() {
if s.delay < maxDelay {
if s.delay > 0 {
d := s.delay * 2
if d > s.delay && d < maxDelay {
s.delay = d
} else {
s.delay = maxDelay
}
} else {
s.delay = 1
}
}
} | [
"func",
"(",
"s",
"*",
"Service",
")",
"backoff",
"(",
")",
"{",
"if",
"s",
".",
"delay",
"<",
"maxDelay",
"{",
"if",
"s",
".",
"delay",
">",
"0",
"{",
"d",
":=",
"s",
".",
"delay",
"*",
"2",
"\n",
"if",
"d",
">",
"s",
".",
"delay",
"&&",
"d",
"<",
"maxDelay",
"{",
"s",
".",
"delay",
"=",
"d",
"\n",
"}",
"else",
"{",
"s",
".",
"delay",
"=",
"maxDelay",
"\n",
"}",
"\n",
"}",
"else",
"{",
"s",
".",
"delay",
"=",
"1",
"\n",
"}",
"\n",
"}",
"\n",
"}"
] | // backoff exponentially increases the RPC poll delay up to maxDelay | [
"backoff",
"exponentially",
"increases",
"the",
"RPC",
"poll",
"delay",
"up",
"to",
"maxDelay"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L103-L116 | train |
vmware/govmomi | toolbox/service.go | Start | func (s *Service) Start() error {
err := s.startChannel()
if err != nil {
return err
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
// Same polling interval and backoff logic as vmtoolsd.
// Required in our case at startup at least, otherwise it is possible
// we miss the 1 Capabilities_Register call for example.
// Note we Send(response) even when nil, to let the VMX know we are here
var response []byte
for {
select {
case <-s.stop:
s.stopChannel()
return
case <-time.After(time.Millisecond * 10 * s.delay):
if err = s.checkReset(); err != nil {
continue
}
err = s.in.Send(response)
response = nil
if err != nil {
s.delay = resetDelay
s.rpcError = true
continue
}
request, _ := s.in.Receive()
if len(request) > 0 {
response = s.Dispatch(request)
s.delay = 0
} else {
s.backoff()
}
}
}
}()
return nil
} | go | func (s *Service) Start() error {
err := s.startChannel()
if err != nil {
return err
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
// Same polling interval and backoff logic as vmtoolsd.
// Required in our case at startup at least, otherwise it is possible
// we miss the 1 Capabilities_Register call for example.
// Note we Send(response) even when nil, to let the VMX know we are here
var response []byte
for {
select {
case <-s.stop:
s.stopChannel()
return
case <-time.After(time.Millisecond * 10 * s.delay):
if err = s.checkReset(); err != nil {
continue
}
err = s.in.Send(response)
response = nil
if err != nil {
s.delay = resetDelay
s.rpcError = true
continue
}
request, _ := s.in.Receive()
if len(request) > 0 {
response = s.Dispatch(request)
s.delay = 0
} else {
s.backoff()
}
}
}
}()
return nil
} | [
"func",
"(",
"s",
"*",
"Service",
")",
"Start",
"(",
")",
"error",
"{",
"err",
":=",
"s",
".",
"startChannel",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"s",
".",
"wg",
".",
"Add",
"(",
"1",
")",
"\n",
"go",
"func",
"(",
")",
"{",
"defer",
"s",
".",
"wg",
".",
"Done",
"(",
")",
"\n\n",
"// Same polling interval and backoff logic as vmtoolsd.",
"// Required in our case at startup at least, otherwise it is possible",
"// we miss the 1 Capabilities_Register call for example.",
"// Note we Send(response) even when nil, to let the VMX know we are here",
"var",
"response",
"[",
"]",
"byte",
"\n\n",
"for",
"{",
"select",
"{",
"case",
"<-",
"s",
".",
"stop",
":",
"s",
".",
"stopChannel",
"(",
")",
"\n",
"return",
"\n",
"case",
"<-",
"time",
".",
"After",
"(",
"time",
".",
"Millisecond",
"*",
"10",
"*",
"s",
".",
"delay",
")",
":",
"if",
"err",
"=",
"s",
".",
"checkReset",
"(",
")",
";",
"err",
"!=",
"nil",
"{",
"continue",
"\n",
"}",
"\n\n",
"err",
"=",
"s",
".",
"in",
".",
"Send",
"(",
"response",
")",
"\n",
"response",
"=",
"nil",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"s",
".",
"delay",
"=",
"resetDelay",
"\n",
"s",
".",
"rpcError",
"=",
"true",
"\n",
"continue",
"\n",
"}",
"\n\n",
"request",
",",
"_",
":=",
"s",
".",
"in",
".",
"Receive",
"(",
")",
"\n\n",
"if",
"len",
"(",
"request",
")",
">",
"0",
"{",
"response",
"=",
"s",
".",
"Dispatch",
"(",
"request",
")",
"\n\n",
"s",
".",
"delay",
"=",
"0",
"\n",
"}",
"else",
"{",
"s",
".",
"backoff",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"(",
")",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // Start initializes the RPC channels and starts a goroutine to listen for incoming RPC requests | [
"Start",
"initializes",
"the",
"RPC",
"channels",
"and",
"starts",
"a",
"goroutine",
"to",
"listen",
"for",
"incoming",
"RPC",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L147-L196 | train |
vmware/govmomi | toolbox/service.go | RegisterHandler | func (s *Service) RegisterHandler(name string, handler Handler) {
s.handlers[name] = handler
} | go | func (s *Service) RegisterHandler(name string, handler Handler) {
s.handlers[name] = handler
} | [
"func",
"(",
"s",
"*",
"Service",
")",
"RegisterHandler",
"(",
"name",
"string",
",",
"handler",
"Handler",
")",
"{",
"s",
".",
"handlers",
"[",
"name",
"]",
"=",
"handler",
"\n",
"}"
] | // RegisterHandler for the given RPC name | [
"RegisterHandler",
"for",
"the",
"given",
"RPC",
"name"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L212-L214 | train |
vmware/govmomi | toolbox/service.go | Dispatch | func (s *Service) Dispatch(request []byte) []byte {
msg := bytes.SplitN(request, []byte{' '}, 2)
name := msg[0]
// Trim NULL byte terminator
name = bytes.TrimRight(name, "\x00")
handler, ok := s.handlers[string(name)]
if !ok {
log.Printf("unknown command: %q\n", name)
return []byte("Unknown Command")
}
var args []byte
if len(msg) == 2 {
args = msg[1]
}
response, err := handler(args)
if err == nil {
response = append([]byte("OK "), response...)
} else {
log.Printf("error calling %s: %s\n", name, err)
response = append([]byte("ERR "), response...)
}
return response
} | go | func (s *Service) Dispatch(request []byte) []byte {
msg := bytes.SplitN(request, []byte{' '}, 2)
name := msg[0]
// Trim NULL byte terminator
name = bytes.TrimRight(name, "\x00")
handler, ok := s.handlers[string(name)]
if !ok {
log.Printf("unknown command: %q\n", name)
return []byte("Unknown Command")
}
var args []byte
if len(msg) == 2 {
args = msg[1]
}
response, err := handler(args)
if err == nil {
response = append([]byte("OK "), response...)
} else {
log.Printf("error calling %s: %s\n", name, err)
response = append([]byte("ERR "), response...)
}
return response
} | [
"func",
"(",
"s",
"*",
"Service",
")",
"Dispatch",
"(",
"request",
"[",
"]",
"byte",
")",
"[",
"]",
"byte",
"{",
"msg",
":=",
"bytes",
".",
"SplitN",
"(",
"request",
",",
"[",
"]",
"byte",
"{",
"' '",
"}",
",",
"2",
")",
"\n",
"name",
":=",
"msg",
"[",
"0",
"]",
"\n\n",
"// Trim NULL byte terminator",
"name",
"=",
"bytes",
".",
"TrimRight",
"(",
"name",
",",
"\"",
"\\x00",
"\"",
")",
"\n\n",
"handler",
",",
"ok",
":=",
"s",
".",
"handlers",
"[",
"string",
"(",
"name",
")",
"]",
"\n\n",
"if",
"!",
"ok",
"{",
"log",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"name",
")",
"\n",
"return",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
"\n",
"}",
"\n\n",
"var",
"args",
"[",
"]",
"byte",
"\n",
"if",
"len",
"(",
"msg",
")",
"==",
"2",
"{",
"args",
"=",
"msg",
"[",
"1",
"]",
"\n",
"}",
"\n\n",
"response",
",",
"err",
":=",
"handler",
"(",
"args",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"response",
"=",
"append",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"response",
"...",
")",
"\n",
"}",
"else",
"{",
"log",
".",
"Printf",
"(",
"\"",
"\\n",
"\"",
",",
"name",
",",
"err",
")",
"\n",
"response",
"=",
"append",
"(",
"[",
"]",
"byte",
"(",
"\"",
"\"",
")",
",",
"response",
"...",
")",
"\n",
"}",
"\n\n",
"return",
"response",
"\n",
"}"
] | // Dispatch an incoming RPC request to a Handler | [
"Dispatch",
"an",
"incoming",
"RPC",
"request",
"to",
"a",
"Handler"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L217-L245 | train |
vmware/govmomi | toolbox/service.go | Reset | func (s *Service) Reset([]byte) ([]byte, error) {
s.SendGuestInfo() // Send the IP info ASAP
return []byte("ATR " + s.name), nil
} | go | func (s *Service) Reset([]byte) ([]byte, error) {
s.SendGuestInfo() // Send the IP info ASAP
return []byte("ATR " + s.name), nil
} | [
"func",
"(",
"s",
"*",
"Service",
")",
"Reset",
"(",
"[",
"]",
"byte",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"s",
".",
"SendGuestInfo",
"(",
")",
"// Send the IP info ASAP",
"\n\n",
"return",
"[",
"]",
"byte",
"(",
"\"",
"\"",
"+",
"s",
".",
"name",
")",
",",
"nil",
"\n",
"}"
] | // Reset is the default Handler for reset requests | [
"Reset",
"is",
"the",
"default",
"Handler",
"for",
"reset",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L248-L252 | train |
vmware/govmomi | toolbox/service.go | SetOption | func (s *Service) SetOption(args []byte) ([]byte, error) {
opts := bytes.SplitN(args, []byte{' '}, 2)
key := string(opts[0])
val := string(opts[1])
if Trace {
fmt.Fprintf(os.Stderr, "set option %q=%q\n", key, val)
}
switch key {
case "broadcastIP": // TODO: const-ify
if val == "1" {
ip := s.PrimaryIP()
if ip == "" {
log.Printf("failed to find primary IP")
return nil, nil
}
msg := fmt.Sprintf("info-set guestinfo.ip %s", ip)
_, err := s.out.Request([]byte(msg))
if err != nil {
return nil, err
}
s.SendGuestInfo()
}
default:
// TODO: handle other options...
}
return nil, nil
} | go | func (s *Service) SetOption(args []byte) ([]byte, error) {
opts := bytes.SplitN(args, []byte{' '}, 2)
key := string(opts[0])
val := string(opts[1])
if Trace {
fmt.Fprintf(os.Stderr, "set option %q=%q\n", key, val)
}
switch key {
case "broadcastIP": // TODO: const-ify
if val == "1" {
ip := s.PrimaryIP()
if ip == "" {
log.Printf("failed to find primary IP")
return nil, nil
}
msg := fmt.Sprintf("info-set guestinfo.ip %s", ip)
_, err := s.out.Request([]byte(msg))
if err != nil {
return nil, err
}
s.SendGuestInfo()
}
default:
// TODO: handle other options...
}
return nil, nil
} | [
"func",
"(",
"s",
"*",
"Service",
")",
"SetOption",
"(",
"args",
"[",
"]",
"byte",
")",
"(",
"[",
"]",
"byte",
",",
"error",
")",
"{",
"opts",
":=",
"bytes",
".",
"SplitN",
"(",
"args",
",",
"[",
"]",
"byte",
"{",
"' '",
"}",
",",
"2",
")",
"\n",
"key",
":=",
"string",
"(",
"opts",
"[",
"0",
"]",
")",
"\n",
"val",
":=",
"string",
"(",
"opts",
"[",
"1",
"]",
")",
"\n\n",
"if",
"Trace",
"{",
"fmt",
".",
"Fprintf",
"(",
"os",
".",
"Stderr",
",",
"\"",
"\\n",
"\"",
",",
"key",
",",
"val",
")",
"\n",
"}",
"\n\n",
"switch",
"key",
"{",
"case",
"\"",
"\"",
":",
"// TODO: const-ify",
"if",
"val",
"==",
"\"",
"\"",
"{",
"ip",
":=",
"s",
".",
"PrimaryIP",
"(",
")",
"\n",
"if",
"ip",
"==",
"\"",
"\"",
"{",
"log",
".",
"Printf",
"(",
"\"",
"\"",
")",
"\n",
"return",
"nil",
",",
"nil",
"\n",
"}",
"\n",
"msg",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"ip",
")",
"\n",
"_",
",",
"err",
":=",
"s",
".",
"out",
".",
"Request",
"(",
"[",
"]",
"byte",
"(",
"msg",
")",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"s",
".",
"SendGuestInfo",
"(",
")",
"\n",
"}",
"\n",
"default",
":",
"// TODO: handle other options...",
"}",
"\n\n",
"return",
"nil",
",",
"nil",
"\n",
"}"
] | // SetOption is the default Handler for Set_Option requests | [
"SetOption",
"is",
"the",
"default",
"Handler",
"for",
"Set_Option",
"requests"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L260-L290 | train |
vmware/govmomi | toolbox/service.go | DefaultIP | func DefaultIP() string {
addrs, err := netInterfaceAddrs()
if err == nil {
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() {
if ip.IP.To4() != nil {
return ip.IP.String()
}
}
}
}
return ""
} | go | func DefaultIP() string {
addrs, err := netInterfaceAddrs()
if err == nil {
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() {
if ip.IP.To4() != nil {
return ip.IP.String()
}
}
}
}
return ""
} | [
"func",
"DefaultIP",
"(",
")",
"string",
"{",
"addrs",
",",
"err",
":=",
"netInterfaceAddrs",
"(",
")",
"\n",
"if",
"err",
"==",
"nil",
"{",
"for",
"_",
",",
"addr",
":=",
"range",
"addrs",
"{",
"if",
"ip",
",",
"ok",
":=",
"addr",
".",
"(",
"*",
"net",
".",
"IPNet",
")",
";",
"ok",
"&&",
"!",
"ip",
".",
"IP",
".",
"IsLoopback",
"(",
")",
"{",
"if",
"ip",
".",
"IP",
".",
"To4",
"(",
")",
"!=",
"nil",
"{",
"return",
"ip",
".",
"IP",
".",
"String",
"(",
")",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"\"",
"\"",
"\n",
"}"
] | // DefaultIP is used by default when responding to a Set_Option broadcastIP request
// It can be overridden with the Service.PrimaryIP field | [
"DefaultIP",
"is",
"used",
"by",
"default",
"when",
"responding",
"to",
"a",
"Set_Option",
"broadcastIP",
"request",
"It",
"can",
"be",
"overridden",
"with",
"the",
"Service",
".",
"PrimaryIP",
"field"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/toolbox/service.go#L294-L307 | train |
vmware/govmomi | object/namespace_manager.go | CreateDirectory | func (nm DatastoreNamespaceManager) CreateDirectory(ctx context.Context, ds *Datastore, displayName string, policy string) (string, error) {
req := &types.CreateDirectory{
This: nm.Reference(),
Datastore: ds.Reference(),
DisplayName: displayName,
Policy: policy,
}
resp, err := methods.CreateDirectory(ctx, nm.c, req)
if err != nil {
return "", err
}
return resp.Returnval, nil
} | go | func (nm DatastoreNamespaceManager) CreateDirectory(ctx context.Context, ds *Datastore, displayName string, policy string) (string, error) {
req := &types.CreateDirectory{
This: nm.Reference(),
Datastore: ds.Reference(),
DisplayName: displayName,
Policy: policy,
}
resp, err := methods.CreateDirectory(ctx, nm.c, req)
if err != nil {
return "", err
}
return resp.Returnval, nil
} | [
"func",
"(",
"nm",
"DatastoreNamespaceManager",
")",
"CreateDirectory",
"(",
"ctx",
"context",
".",
"Context",
",",
"ds",
"*",
"Datastore",
",",
"displayName",
"string",
",",
"policy",
"string",
")",
"(",
"string",
",",
"error",
")",
"{",
"req",
":=",
"&",
"types",
".",
"CreateDirectory",
"{",
"This",
":",
"nm",
".",
"Reference",
"(",
")",
",",
"Datastore",
":",
"ds",
".",
"Reference",
"(",
")",
",",
"DisplayName",
":",
"displayName",
",",
"Policy",
":",
"policy",
",",
"}",
"\n\n",
"resp",
",",
"err",
":=",
"methods",
".",
"CreateDirectory",
"(",
"ctx",
",",
"nm",
".",
"c",
",",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"\"",
"\"",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"resp",
".",
"Returnval",
",",
"nil",
"\n",
"}"
] | // CreateDirectory creates a top-level directory on the given vsan datastore, using
// the given user display name hint and opaque storage policy. | [
"CreateDirectory",
"creates",
"a",
"top",
"-",
"level",
"directory",
"on",
"the",
"given",
"vsan",
"datastore",
"using",
"the",
"given",
"user",
"display",
"name",
"hint",
"and",
"opaque",
"storage",
"policy",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/namespace_manager.go#L41-L56 | train |
vmware/govmomi | object/namespace_manager.go | DeleteDirectory | func (nm DatastoreNamespaceManager) DeleteDirectory(ctx context.Context, dc *Datacenter, datastorePath string) error {
req := &types.DeleteDirectory{
This: nm.Reference(),
DatastorePath: datastorePath,
}
if dc != nil {
ref := dc.Reference()
req.Datacenter = &ref
}
if _, err := methods.DeleteDirectory(ctx, nm.c, req); err != nil {
return err
}
return nil
} | go | func (nm DatastoreNamespaceManager) DeleteDirectory(ctx context.Context, dc *Datacenter, datastorePath string) error {
req := &types.DeleteDirectory{
This: nm.Reference(),
DatastorePath: datastorePath,
}
if dc != nil {
ref := dc.Reference()
req.Datacenter = &ref
}
if _, err := methods.DeleteDirectory(ctx, nm.c, req); err != nil {
return err
}
return nil
} | [
"func",
"(",
"nm",
"DatastoreNamespaceManager",
")",
"DeleteDirectory",
"(",
"ctx",
"context",
".",
"Context",
",",
"dc",
"*",
"Datacenter",
",",
"datastorePath",
"string",
")",
"error",
"{",
"req",
":=",
"&",
"types",
".",
"DeleteDirectory",
"{",
"This",
":",
"nm",
".",
"Reference",
"(",
")",
",",
"DatastorePath",
":",
"datastorePath",
",",
"}",
"\n\n",
"if",
"dc",
"!=",
"nil",
"{",
"ref",
":=",
"dc",
".",
"Reference",
"(",
")",
"\n",
"req",
".",
"Datacenter",
"=",
"&",
"ref",
"\n",
"}",
"\n\n",
"if",
"_",
",",
"err",
":=",
"methods",
".",
"DeleteDirectory",
"(",
"ctx",
",",
"nm",
".",
"c",
",",
"req",
")",
";",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // DeleteDirectory deletes the given top-level directory from a vsan datastore. | [
"DeleteDirectory",
"deletes",
"the",
"given",
"top",
"-",
"level",
"directory",
"from",
"a",
"vsan",
"datastore",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/namespace_manager.go#L59-L76 | train |
vmware/govmomi | performance/manager.go | NewManager | func NewManager(client *vim25.Client) *Manager {
m := Manager{
Common: object.NewCommon(client, *client.ServiceContent.PerfManager),
}
m.pm.PerformanceManager = new(mo.PerformanceManager)
return &m
} | go | func NewManager(client *vim25.Client) *Manager {
m := Manager{
Common: object.NewCommon(client, *client.ServiceContent.PerfManager),
}
m.pm.PerformanceManager = new(mo.PerformanceManager)
return &m
} | [
"func",
"NewManager",
"(",
"client",
"*",
"vim25",
".",
"Client",
")",
"*",
"Manager",
"{",
"m",
":=",
"Manager",
"{",
"Common",
":",
"object",
".",
"NewCommon",
"(",
"client",
",",
"*",
"client",
".",
"ServiceContent",
".",
"PerfManager",
")",
",",
"}",
"\n\n",
"m",
".",
"pm",
".",
"PerformanceManager",
"=",
"new",
"(",
"mo",
".",
"PerformanceManager",
")",
"\n\n",
"return",
"&",
"m",
"\n",
"}"
] | // NewManager creates a new Manager instance. | [
"NewManager",
"creates",
"a",
"new",
"Manager",
"instance",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L58-L66 | train |
vmware/govmomi | performance/manager.go | HistoricalInterval | func (m *Manager) HistoricalInterval(ctx context.Context) (IntervalList, error) {
var pm mo.PerformanceManager
err := m.Properties(ctx, m.Reference(), []string{"historicalInterval"}, &pm)
if err != nil {
return nil, err
}
return IntervalList(pm.HistoricalInterval), nil
} | go | func (m *Manager) HistoricalInterval(ctx context.Context) (IntervalList, error) {
var pm mo.PerformanceManager
err := m.Properties(ctx, m.Reference(), []string{"historicalInterval"}, &pm)
if err != nil {
return nil, err
}
return IntervalList(pm.HistoricalInterval), nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"HistoricalInterval",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"IntervalList",
",",
"error",
")",
"{",
"var",
"pm",
"mo",
".",
"PerformanceManager",
"\n\n",
"err",
":=",
"m",
".",
"Properties",
"(",
"ctx",
",",
"m",
".",
"Reference",
"(",
")",
",",
"[",
"]",
"string",
"{",
"\"",
"\"",
"}",
",",
"&",
"pm",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"IntervalList",
"(",
"pm",
".",
"HistoricalInterval",
")",
",",
"nil",
"\n",
"}"
] | // HistoricalInterval gets the PerformanceManager.HistoricalInterval property and wraps as an IntervalList. | [
"HistoricalInterval",
"gets",
"the",
"PerformanceManager",
".",
"HistoricalInterval",
"property",
"and",
"wraps",
"as",
"an",
"IntervalList",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L91-L100 | train |
vmware/govmomi | performance/manager.go | CounterInfo | func (m *Manager) CounterInfo(ctx context.Context) ([]types.PerfCounterInfo, error) {
m.pm.Lock()
defer m.pm.Unlock()
if len(m.pm.PerfCounter) == 0 {
err := m.Properties(ctx, m.Reference(), []string{"perfCounter"}, m.pm.PerformanceManager)
if err != nil {
return nil, err
}
}
return m.pm.PerfCounter, nil
} | go | func (m *Manager) CounterInfo(ctx context.Context) ([]types.PerfCounterInfo, error) {
m.pm.Lock()
defer m.pm.Unlock()
if len(m.pm.PerfCounter) == 0 {
err := m.Properties(ctx, m.Reference(), []string{"perfCounter"}, m.pm.PerformanceManager)
if err != nil {
return nil, err
}
}
return m.pm.PerfCounter, nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"CounterInfo",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"[",
"]",
"types",
".",
"PerfCounterInfo",
",",
"error",
")",
"{",
"m",
".",
"pm",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"pm",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"len",
"(",
"m",
".",
"pm",
".",
"PerfCounter",
")",
"==",
"0",
"{",
"err",
":=",
"m",
".",
"Properties",
"(",
"ctx",
",",
"m",
".",
"Reference",
"(",
")",
",",
"[",
"]",
"string",
"{",
"\"",
"\"",
"}",
",",
"m",
".",
"pm",
".",
"PerformanceManager",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"m",
".",
"pm",
".",
"PerfCounter",
",",
"nil",
"\n",
"}"
] | // CounterInfo gets the PerformanceManager.PerfCounter property.
// The property value is only collected once, subsequent calls return the cached value. | [
"CounterInfo",
"gets",
"the",
"PerformanceManager",
".",
"PerfCounter",
"property",
".",
"The",
"property",
"value",
"is",
"only",
"collected",
"once",
"subsequent",
"calls",
"return",
"the",
"cached",
"value",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L104-L116 | train |
vmware/govmomi | performance/manager.go | CounterInfoByKey | func (m *Manager) CounterInfoByKey(ctx context.Context) (map[int32]*types.PerfCounterInfo, error) {
m.infoByKey.Lock()
defer m.infoByKey.Unlock()
if m.infoByKey.m != nil {
return m.infoByKey.m, nil
}
info, err := m.CounterInfo(ctx)
if err != nil {
return nil, err
}
m.infoByKey.m = make(map[int32]*types.PerfCounterInfo)
for i := range info {
c := &info[i]
m.infoByKey.m[c.Key] = c
}
return m.infoByKey.m, nil
} | go | func (m *Manager) CounterInfoByKey(ctx context.Context) (map[int32]*types.PerfCounterInfo, error) {
m.infoByKey.Lock()
defer m.infoByKey.Unlock()
if m.infoByKey.m != nil {
return m.infoByKey.m, nil
}
info, err := m.CounterInfo(ctx)
if err != nil {
return nil, err
}
m.infoByKey.m = make(map[int32]*types.PerfCounterInfo)
for i := range info {
c := &info[i]
m.infoByKey.m[c.Key] = c
}
return m.infoByKey.m, nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"CounterInfoByKey",
"(",
"ctx",
"context",
".",
"Context",
")",
"(",
"map",
"[",
"int32",
"]",
"*",
"types",
".",
"PerfCounterInfo",
",",
"error",
")",
"{",
"m",
".",
"infoByKey",
".",
"Lock",
"(",
")",
"\n",
"defer",
"m",
".",
"infoByKey",
".",
"Unlock",
"(",
")",
"\n\n",
"if",
"m",
".",
"infoByKey",
".",
"m",
"!=",
"nil",
"{",
"return",
"m",
".",
"infoByKey",
".",
"m",
",",
"nil",
"\n",
"}",
"\n\n",
"info",
",",
"err",
":=",
"m",
".",
"CounterInfo",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"m",
".",
"infoByKey",
".",
"m",
"=",
"make",
"(",
"map",
"[",
"int32",
"]",
"*",
"types",
".",
"PerfCounterInfo",
")",
"\n\n",
"for",
"i",
":=",
"range",
"info",
"{",
"c",
":=",
"&",
"info",
"[",
"i",
"]",
"\n\n",
"m",
".",
"infoByKey",
".",
"m",
"[",
"c",
".",
"Key",
"]",
"=",
"c",
"\n",
"}",
"\n\n",
"return",
"m",
".",
"infoByKey",
".",
"m",
",",
"nil",
"\n",
"}"
] | // CounterInfoByKey converts the PerformanceManager.PerfCounter property to a map,
// where key is types.PerfCounterInfo.Key. | [
"CounterInfoByKey",
"converts",
"the",
"PerformanceManager",
".",
"PerfCounter",
"property",
"to",
"a",
"map",
"where",
"key",
"is",
"types",
".",
"PerfCounterInfo",
".",
"Key",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L146-L168 | train |
vmware/govmomi | performance/manager.go | ProviderSummary | func (m *Manager) ProviderSummary(ctx context.Context, entity types.ManagedObjectReference) (*types.PerfProviderSummary, error) {
req := types.QueryPerfProviderSummary{
This: m.Reference(),
Entity: entity,
}
res, err := methods.QueryPerfProviderSummary(ctx, m.Client(), &req)
if err != nil {
return nil, err
}
return &res.Returnval, nil
} | go | func (m *Manager) ProviderSummary(ctx context.Context, entity types.ManagedObjectReference) (*types.PerfProviderSummary, error) {
req := types.QueryPerfProviderSummary{
This: m.Reference(),
Entity: entity,
}
res, err := methods.QueryPerfProviderSummary(ctx, m.Client(), &req)
if err != nil {
return nil, err
}
return &res.Returnval, nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"ProviderSummary",
"(",
"ctx",
"context",
".",
"Context",
",",
"entity",
"types",
".",
"ManagedObjectReference",
")",
"(",
"*",
"types",
".",
"PerfProviderSummary",
",",
"error",
")",
"{",
"req",
":=",
"types",
".",
"QueryPerfProviderSummary",
"{",
"This",
":",
"m",
".",
"Reference",
"(",
")",
",",
"Entity",
":",
"entity",
",",
"}",
"\n\n",
"res",
",",
"err",
":=",
"methods",
".",
"QueryPerfProviderSummary",
"(",
"ctx",
",",
"m",
".",
"Client",
"(",
")",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"&",
"res",
".",
"Returnval",
",",
"nil",
"\n",
"}"
] | // ProviderSummary wraps the QueryPerfProviderSummary method, caching the value based on entity.Type. | [
"ProviderSummary",
"wraps",
"the",
"QueryPerfProviderSummary",
"method",
"caching",
"the",
"value",
"based",
"on",
"entity",
".",
"Type",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L171-L183 | train |
vmware/govmomi | performance/manager.go | AvailableMetric | func (m *Manager) AvailableMetric(ctx context.Context, entity types.ManagedObjectReference, interval int32) (MetricList, error) {
req := types.QueryAvailablePerfMetric{
This: m.Reference(),
Entity: entity.Reference(),
IntervalId: interval,
}
res, err := methods.QueryAvailablePerfMetric(ctx, m.Client(), &req)
if err != nil {
return nil, err
}
if m.Sort {
info, err := m.CounterInfoByKey(ctx)
if err != nil {
return nil, err
}
sort.Sort(groupPerfCounterInfo{info, res.Returnval})
}
return MetricList(res.Returnval), nil
} | go | func (m *Manager) AvailableMetric(ctx context.Context, entity types.ManagedObjectReference, interval int32) (MetricList, error) {
req := types.QueryAvailablePerfMetric{
This: m.Reference(),
Entity: entity.Reference(),
IntervalId: interval,
}
res, err := methods.QueryAvailablePerfMetric(ctx, m.Client(), &req)
if err != nil {
return nil, err
}
if m.Sort {
info, err := m.CounterInfoByKey(ctx)
if err != nil {
return nil, err
}
sort.Sort(groupPerfCounterInfo{info, res.Returnval})
}
return MetricList(res.Returnval), nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"AvailableMetric",
"(",
"ctx",
"context",
".",
"Context",
",",
"entity",
"types",
".",
"ManagedObjectReference",
",",
"interval",
"int32",
")",
"(",
"MetricList",
",",
"error",
")",
"{",
"req",
":=",
"types",
".",
"QueryAvailablePerfMetric",
"{",
"This",
":",
"m",
".",
"Reference",
"(",
")",
",",
"Entity",
":",
"entity",
".",
"Reference",
"(",
")",
",",
"IntervalId",
":",
"interval",
",",
"}",
"\n\n",
"res",
",",
"err",
":=",
"methods",
".",
"QueryAvailablePerfMetric",
"(",
"ctx",
",",
"m",
".",
"Client",
"(",
")",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"m",
".",
"Sort",
"{",
"info",
",",
"err",
":=",
"m",
".",
"CounterInfoByKey",
"(",
"ctx",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"sort",
".",
"Sort",
"(",
"groupPerfCounterInfo",
"{",
"info",
",",
"res",
".",
"Returnval",
"}",
")",
"\n",
"}",
"\n\n",
"return",
"MetricList",
"(",
"res",
".",
"Returnval",
")",
",",
"nil",
"\n",
"}"
] | // AvailableMetric wraps the QueryAvailablePerfMetric method.
// The MetricList is sorted by PerfCounterInfo.GroupInfo.Key if Manager.Sort == true. | [
"AvailableMetric",
"wraps",
"the",
"QueryAvailablePerfMetric",
"method",
".",
"The",
"MetricList",
"is",
"sorted",
"by",
"PerfCounterInfo",
".",
"GroupInfo",
".",
"Key",
"if",
"Manager",
".",
"Sort",
"==",
"true",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L224-L246 | train |
vmware/govmomi | performance/manager.go | Query | func (m *Manager) Query(ctx context.Context, spec []types.PerfQuerySpec) ([]types.BasePerfEntityMetricBase, error) {
req := types.QueryPerf{
This: m.Reference(),
QuerySpec: spec,
}
res, err := methods.QueryPerf(ctx, m.Client(), &req)
if err != nil {
return nil, err
}
return res.Returnval, nil
} | go | func (m *Manager) Query(ctx context.Context, spec []types.PerfQuerySpec) ([]types.BasePerfEntityMetricBase, error) {
req := types.QueryPerf{
This: m.Reference(),
QuerySpec: spec,
}
res, err := methods.QueryPerf(ctx, m.Client(), &req)
if err != nil {
return nil, err
}
return res.Returnval, nil
} | [
"func",
"(",
"m",
"*",
"Manager",
")",
"Query",
"(",
"ctx",
"context",
".",
"Context",
",",
"spec",
"[",
"]",
"types",
".",
"PerfQuerySpec",
")",
"(",
"[",
"]",
"types",
".",
"BasePerfEntityMetricBase",
",",
"error",
")",
"{",
"req",
":=",
"types",
".",
"QueryPerf",
"{",
"This",
":",
"m",
".",
"Reference",
"(",
")",
",",
"QuerySpec",
":",
"spec",
",",
"}",
"\n\n",
"res",
",",
"err",
":=",
"methods",
".",
"QueryPerf",
"(",
"ctx",
",",
"m",
".",
"Client",
"(",
")",
",",
"&",
"req",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"err",
"\n",
"}",
"\n\n",
"return",
"res",
".",
"Returnval",
",",
"nil",
"\n",
"}"
] | // Query wraps the QueryPerf method. | [
"Query",
"wraps",
"the",
"QueryPerf",
"method",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L249-L261 | train |
vmware/govmomi | performance/manager.go | ValueCSV | func (s *MetricSeries) ValueCSV() string {
vals := make([]string, len(s.Value))
for i := range s.Value {
vals[i] = s.Format(s.Value[i])
}
return strings.Join(vals, ",")
} | go | func (s *MetricSeries) ValueCSV() string {
vals := make([]string, len(s.Value))
for i := range s.Value {
vals[i] = s.Format(s.Value[i])
}
return strings.Join(vals, ",")
} | [
"func",
"(",
"s",
"*",
"MetricSeries",
")",
"ValueCSV",
"(",
")",
"string",
"{",
"vals",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"s",
".",
"Value",
")",
")",
"\n\n",
"for",
"i",
":=",
"range",
"s",
".",
"Value",
"{",
"vals",
"[",
"i",
"]",
"=",
"s",
".",
"Format",
"(",
"s",
".",
"Value",
"[",
"i",
"]",
")",
"\n",
"}",
"\n\n",
"return",
"strings",
".",
"Join",
"(",
"vals",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // ValueCSV converts the Value field to a CSV string | [
"ValueCSV",
"converts",
"the",
"Value",
"field",
"to",
"a",
"CSV",
"string"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L341-L349 | train |
vmware/govmomi | performance/manager.go | SampleInfoCSV | func (m *EntityMetric) SampleInfoCSV() string {
vals := make([]string, len(m.SampleInfo)*2)
i := 0
for _, s := range m.SampleInfo {
vals[i] = s.Timestamp.Format(time.RFC3339)
i++
vals[i] = strconv.Itoa(int(s.Interval))
i++
}
return strings.Join(vals, ",")
} | go | func (m *EntityMetric) SampleInfoCSV() string {
vals := make([]string, len(m.SampleInfo)*2)
i := 0
for _, s := range m.SampleInfo {
vals[i] = s.Timestamp.Format(time.RFC3339)
i++
vals[i] = strconv.Itoa(int(s.Interval))
i++
}
return strings.Join(vals, ",")
} | [
"func",
"(",
"m",
"*",
"EntityMetric",
")",
"SampleInfoCSV",
"(",
")",
"string",
"{",
"vals",
":=",
"make",
"(",
"[",
"]",
"string",
",",
"len",
"(",
"m",
".",
"SampleInfo",
")",
"*",
"2",
")",
"\n\n",
"i",
":=",
"0",
"\n\n",
"for",
"_",
",",
"s",
":=",
"range",
"m",
".",
"SampleInfo",
"{",
"vals",
"[",
"i",
"]",
"=",
"s",
".",
"Timestamp",
".",
"Format",
"(",
"time",
".",
"RFC3339",
")",
"\n",
"i",
"++",
"\n",
"vals",
"[",
"i",
"]",
"=",
"strconv",
".",
"Itoa",
"(",
"int",
"(",
"s",
".",
"Interval",
")",
")",
"\n",
"i",
"++",
"\n",
"}",
"\n\n",
"return",
"strings",
".",
"Join",
"(",
"vals",
",",
"\"",
"\"",
")",
"\n",
"}"
] | // SampleInfoCSV converts the SampleInfo field to a CSV string | [
"SampleInfoCSV",
"converts",
"the",
"SampleInfo",
"field",
"to",
"a",
"CSV",
"string"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/performance/manager.go#L360-L373 | train |
vmware/govmomi | govc/flags/host_connect.go | Spec | func (flag *HostConnectFlag) Spec(c *vim25.Client) types.HostConnectSpec {
spec := flag.HostConnectSpec
if spec.SslThumbprint == "" {
spec.SslThumbprint = c.Thumbprint(spec.HostName)
if spec.SslThumbprint == "" && flag.noverify {
var info object.HostCertificateInfo
t := c.Transport.(*http.Transport)
_ = info.FromURL(&url.URL{Host: spec.HostName}, t.TLSClientConfig)
spec.SslThumbprint = info.ThumbprintSHA1
}
}
return spec
} | go | func (flag *HostConnectFlag) Spec(c *vim25.Client) types.HostConnectSpec {
spec := flag.HostConnectSpec
if spec.SslThumbprint == "" {
spec.SslThumbprint = c.Thumbprint(spec.HostName)
if spec.SslThumbprint == "" && flag.noverify {
var info object.HostCertificateInfo
t := c.Transport.(*http.Transport)
_ = info.FromURL(&url.URL{Host: spec.HostName}, t.TLSClientConfig)
spec.SslThumbprint = info.ThumbprintSHA1
}
}
return spec
} | [
"func",
"(",
"flag",
"*",
"HostConnectFlag",
")",
"Spec",
"(",
"c",
"*",
"vim25",
".",
"Client",
")",
"types",
".",
"HostConnectSpec",
"{",
"spec",
":=",
"flag",
".",
"HostConnectSpec",
"\n\n",
"if",
"spec",
".",
"SslThumbprint",
"==",
"\"",
"\"",
"{",
"spec",
".",
"SslThumbprint",
"=",
"c",
".",
"Thumbprint",
"(",
"spec",
".",
"HostName",
")",
"\n\n",
"if",
"spec",
".",
"SslThumbprint",
"==",
"\"",
"\"",
"&&",
"flag",
".",
"noverify",
"{",
"var",
"info",
"object",
".",
"HostCertificateInfo",
"\n",
"t",
":=",
"c",
".",
"Transport",
".",
"(",
"*",
"http",
".",
"Transport",
")",
"\n",
"_",
"=",
"info",
".",
"FromURL",
"(",
"&",
"url",
".",
"URL",
"{",
"Host",
":",
"spec",
".",
"HostName",
"}",
",",
"t",
".",
"TLSClientConfig",
")",
"\n",
"spec",
".",
"SslThumbprint",
"=",
"info",
".",
"ThumbprintSHA1",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"spec",
"\n",
"}"
] | // Spec attempts to fill in SslThumbprint if empty.
// First checks GOVC_TLS_KNOWN_HOSTS, if not found and noverify=true then
// use object.HostCertificateInfo to get the thumbprint. | [
"Spec",
"attempts",
"to",
"fill",
"in",
"SslThumbprint",
"if",
"empty",
".",
"First",
"checks",
"GOVC_TLS_KNOWN_HOSTS",
"if",
"not",
"found",
"and",
"noverify",
"=",
"true",
"then",
"use",
"object",
".",
"HostCertificateInfo",
"to",
"get",
"the",
"thumbprint",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/flags/host_connect.go#L70-L85 | train |
vmware/govmomi | govc/flags/host_connect.go | Fault | func (flag *HostConnectFlag) Fault(err error) error {
if err == nil {
return nil
}
if f, ok := err.(types.HasFault); ok {
switch fault := f.Fault().(type) {
case *types.SSLVerifyFault:
return fmt.Errorf("%s thumbprint=%s", err, fault.Thumbprint)
}
}
return err
} | go | func (flag *HostConnectFlag) Fault(err error) error {
if err == nil {
return nil
}
if f, ok := err.(types.HasFault); ok {
switch fault := f.Fault().(type) {
case *types.SSLVerifyFault:
return fmt.Errorf("%s thumbprint=%s", err, fault.Thumbprint)
}
}
return err
} | [
"func",
"(",
"flag",
"*",
"HostConnectFlag",
")",
"Fault",
"(",
"err",
"error",
")",
"error",
"{",
"if",
"err",
"==",
"nil",
"{",
"return",
"nil",
"\n",
"}",
"\n\n",
"if",
"f",
",",
"ok",
":=",
"err",
".",
"(",
"types",
".",
"HasFault",
")",
";",
"ok",
"{",
"switch",
"fault",
":=",
"f",
".",
"Fault",
"(",
")",
".",
"(",
"type",
")",
"{",
"case",
"*",
"types",
".",
"SSLVerifyFault",
":",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"err",
",",
"fault",
".",
"Thumbprint",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"err",
"\n",
"}"
] | // Fault checks if error is SSLVerifyFault, including the thumbprint if so | [
"Fault",
"checks",
"if",
"error",
"is",
"SSLVerifyFault",
"including",
"the",
"thumbprint",
"if",
"so"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/govc/flags/host_connect.go#L88-L101 | train |
vmware/govmomi | guest/toolbox/client.go | Run | func (c *Client) Run(ctx context.Context, cmd *exec.Cmd) error {
vc := c.ProcessManager.Client()
spec := types.GuestProgramSpec{
ProgramPath: cmd.Path,
Arguments: strings.Join(cmd.Args, " "),
EnvVariables: cmd.Env,
WorkingDirectory: cmd.Dir,
}
pid, serr := c.ProcessManager.StartProgram(ctx, c.Authentication, &spec)
if serr != nil {
return serr
}
if cmd.Stdin != nil {
dst := fmt.Sprintf("/proc/%d/stdin", pid)
var buf bytes.Buffer
size, err := io.Copy(&buf, cmd.Stdin)
if err != nil {
return err
}
attr := new(types.GuestPosixFileAttributes)
url, err := c.FileManager.InitiateFileTransferToGuest(ctx, c.Authentication, dst, attr, size, true)
if err != nil {
return err
}
u, err := c.FileManager.TransferURL(ctx, url)
if err != nil {
return err
}
p := soap.DefaultUpload
p.ContentLength = size
err = vc.Client.Upload(ctx, &buf, u, &p)
if err != nil {
return err
}
}
names := []string{"out", "err"}
for i, w := range []io.Writer{cmd.Stdout, cmd.Stderr} {
if w == nil {
continue
}
src := fmt.Sprintf("/proc/%d/std%s", pid, names[i])
info, err := c.procReader(ctx, src)
if err != nil {
return err
}
u, err := c.FileManager.TransferURL(ctx, info.Url)
if err != nil {
return err
}
f, _, err := vc.Client.Download(ctx, u, &soap.DefaultDownload)
if err != nil {
return err
}
_, err = io.Copy(w, f)
_ = f.Close()
if err != nil {
return err
}
}
procs, err := c.ProcessManager.ListProcesses(ctx, c.Authentication, []int64{pid})
if err != nil {
return err
}
if len(procs) == 1 {
rc := procs[0].ExitCode
if rc != 0 {
return fmt.Errorf("%s: exit %d", cmd.Path, rc)
}
}
return nil
} | go | func (c *Client) Run(ctx context.Context, cmd *exec.Cmd) error {
vc := c.ProcessManager.Client()
spec := types.GuestProgramSpec{
ProgramPath: cmd.Path,
Arguments: strings.Join(cmd.Args, " "),
EnvVariables: cmd.Env,
WorkingDirectory: cmd.Dir,
}
pid, serr := c.ProcessManager.StartProgram(ctx, c.Authentication, &spec)
if serr != nil {
return serr
}
if cmd.Stdin != nil {
dst := fmt.Sprintf("/proc/%d/stdin", pid)
var buf bytes.Buffer
size, err := io.Copy(&buf, cmd.Stdin)
if err != nil {
return err
}
attr := new(types.GuestPosixFileAttributes)
url, err := c.FileManager.InitiateFileTransferToGuest(ctx, c.Authentication, dst, attr, size, true)
if err != nil {
return err
}
u, err := c.FileManager.TransferURL(ctx, url)
if err != nil {
return err
}
p := soap.DefaultUpload
p.ContentLength = size
err = vc.Client.Upload(ctx, &buf, u, &p)
if err != nil {
return err
}
}
names := []string{"out", "err"}
for i, w := range []io.Writer{cmd.Stdout, cmd.Stderr} {
if w == nil {
continue
}
src := fmt.Sprintf("/proc/%d/std%s", pid, names[i])
info, err := c.procReader(ctx, src)
if err != nil {
return err
}
u, err := c.FileManager.TransferURL(ctx, info.Url)
if err != nil {
return err
}
f, _, err := vc.Client.Download(ctx, u, &soap.DefaultDownload)
if err != nil {
return err
}
_, err = io.Copy(w, f)
_ = f.Close()
if err != nil {
return err
}
}
procs, err := c.ProcessManager.ListProcesses(ctx, c.Authentication, []int64{pid})
if err != nil {
return err
}
if len(procs) == 1 {
rc := procs[0].ExitCode
if rc != 0 {
return fmt.Errorf("%s: exit %d", cmd.Path, rc)
}
}
return nil
} | [
"func",
"(",
"c",
"*",
"Client",
")",
"Run",
"(",
"ctx",
"context",
".",
"Context",
",",
"cmd",
"*",
"exec",
".",
"Cmd",
")",
"error",
"{",
"vc",
":=",
"c",
".",
"ProcessManager",
".",
"Client",
"(",
")",
"\n\n",
"spec",
":=",
"types",
".",
"GuestProgramSpec",
"{",
"ProgramPath",
":",
"cmd",
".",
"Path",
",",
"Arguments",
":",
"strings",
".",
"Join",
"(",
"cmd",
".",
"Args",
",",
"\"",
"\"",
")",
",",
"EnvVariables",
":",
"cmd",
".",
"Env",
",",
"WorkingDirectory",
":",
"cmd",
".",
"Dir",
",",
"}",
"\n\n",
"pid",
",",
"serr",
":=",
"c",
".",
"ProcessManager",
".",
"StartProgram",
"(",
"ctx",
",",
"c",
".",
"Authentication",
",",
"&",
"spec",
")",
"\n",
"if",
"serr",
"!=",
"nil",
"{",
"return",
"serr",
"\n",
"}",
"\n\n",
"if",
"cmd",
".",
"Stdin",
"!=",
"nil",
"{",
"dst",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"pid",
")",
"\n\n",
"var",
"buf",
"bytes",
".",
"Buffer",
"\n",
"size",
",",
"err",
":=",
"io",
".",
"Copy",
"(",
"&",
"buf",
",",
"cmd",
".",
"Stdin",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"attr",
":=",
"new",
"(",
"types",
".",
"GuestPosixFileAttributes",
")",
"\n\n",
"url",
",",
"err",
":=",
"c",
".",
"FileManager",
".",
"InitiateFileTransferToGuest",
"(",
"ctx",
",",
"c",
".",
"Authentication",
",",
"dst",
",",
"attr",
",",
"size",
",",
"true",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"u",
",",
"err",
":=",
"c",
".",
"FileManager",
".",
"TransferURL",
"(",
"ctx",
",",
"url",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"p",
":=",
"soap",
".",
"DefaultUpload",
"\n",
"p",
".",
"ContentLength",
"=",
"size",
"\n\n",
"err",
"=",
"vc",
".",
"Client",
".",
"Upload",
"(",
"ctx",
",",
"&",
"buf",
",",
"u",
",",
"&",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"names",
":=",
"[",
"]",
"string",
"{",
"\"",
"\"",
",",
"\"",
"\"",
"}",
"\n\n",
"for",
"i",
",",
"w",
":=",
"range",
"[",
"]",
"io",
".",
"Writer",
"{",
"cmd",
".",
"Stdout",
",",
"cmd",
".",
"Stderr",
"}",
"{",
"if",
"w",
"==",
"nil",
"{",
"continue",
"\n",
"}",
"\n\n",
"src",
":=",
"fmt",
".",
"Sprintf",
"(",
"\"",
"\"",
",",
"pid",
",",
"names",
"[",
"i",
"]",
")",
"\n\n",
"info",
",",
"err",
":=",
"c",
".",
"procReader",
"(",
"ctx",
",",
"src",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"u",
",",
"err",
":=",
"c",
".",
"FileManager",
".",
"TransferURL",
"(",
"ctx",
",",
"info",
".",
"Url",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"f",
",",
"_",
",",
"err",
":=",
"vc",
".",
"Client",
".",
"Download",
"(",
"ctx",
",",
"u",
",",
"&",
"soap",
".",
"DefaultDownload",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"_",
",",
"err",
"=",
"io",
".",
"Copy",
"(",
"w",
",",
"f",
")",
"\n",
"_",
"=",
"f",
".",
"Close",
"(",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n",
"}",
"\n\n",
"procs",
",",
"err",
":=",
"c",
".",
"ProcessManager",
".",
"ListProcesses",
"(",
"ctx",
",",
"c",
".",
"Authentication",
",",
"[",
"]",
"int64",
"{",
"pid",
"}",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"if",
"len",
"(",
"procs",
")",
"==",
"1",
"{",
"rc",
":=",
"procs",
"[",
"0",
"]",
".",
"ExitCode",
"\n",
"if",
"rc",
"!=",
"0",
"{",
"return",
"fmt",
".",
"Errorf",
"(",
"\"",
"\"",
",",
"cmd",
".",
"Path",
",",
"rc",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"nil",
"\n",
"}"
] | // Run implements exec.Cmd.Run over vmx guest RPC. | [
"Run",
"implements",
"exec",
".",
"Cmd",
".",
"Run",
"over",
"vmx",
"guest",
"RPC",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/guest/toolbox/client.go#L141-L230 | train |
vmware/govmomi | guest/toolbox/client.go | Download | func (c *Client) Download(ctx context.Context, src string) (io.ReadCloser, int64, error) {
vc := c.ProcessManager.Client()
info, err := c.FileManager.InitiateFileTransferFromGuest(ctx, c.Authentication, src)
if err != nil {
return nil, 0, err
}
u, err := c.FileManager.TransferURL(ctx, info.Url)
if err != nil {
return nil, 0, err
}
p := soap.DefaultDownload
f, n, err := vc.Download(ctx, u, &p)
if err != nil {
return nil, n, err
}
if strings.HasPrefix(src, "/archive:/") || isDir(src) {
f = &archiveReader{ReadCloser: f} // look for the gzip trailer
}
return f, n, nil
} | go | func (c *Client) Download(ctx context.Context, src string) (io.ReadCloser, int64, error) {
vc := c.ProcessManager.Client()
info, err := c.FileManager.InitiateFileTransferFromGuest(ctx, c.Authentication, src)
if err != nil {
return nil, 0, err
}
u, err := c.FileManager.TransferURL(ctx, info.Url)
if err != nil {
return nil, 0, err
}
p := soap.DefaultDownload
f, n, err := vc.Download(ctx, u, &p)
if err != nil {
return nil, n, err
}
if strings.HasPrefix(src, "/archive:/") || isDir(src) {
f = &archiveReader{ReadCloser: f} // look for the gzip trailer
}
return f, n, nil
} | [
"func",
"(",
"c",
"*",
"Client",
")",
"Download",
"(",
"ctx",
"context",
".",
"Context",
",",
"src",
"string",
")",
"(",
"io",
".",
"ReadCloser",
",",
"int64",
",",
"error",
")",
"{",
"vc",
":=",
"c",
".",
"ProcessManager",
".",
"Client",
"(",
")",
"\n\n",
"info",
",",
"err",
":=",
"c",
".",
"FileManager",
".",
"InitiateFileTransferFromGuest",
"(",
"ctx",
",",
"c",
".",
"Authentication",
",",
"src",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"0",
",",
"err",
"\n",
"}",
"\n\n",
"u",
",",
"err",
":=",
"c",
".",
"FileManager",
".",
"TransferURL",
"(",
"ctx",
",",
"info",
".",
"Url",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"0",
",",
"err",
"\n",
"}",
"\n\n",
"p",
":=",
"soap",
".",
"DefaultDownload",
"\n\n",
"f",
",",
"n",
",",
"err",
":=",
"vc",
".",
"Download",
"(",
"ctx",
",",
"u",
",",
"&",
"p",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"nil",
",",
"n",
",",
"err",
"\n",
"}",
"\n\n",
"if",
"strings",
".",
"HasPrefix",
"(",
"src",
",",
"\"",
"\"",
")",
"||",
"isDir",
"(",
"src",
")",
"{",
"f",
"=",
"&",
"archiveReader",
"{",
"ReadCloser",
":",
"f",
"}",
"// look for the gzip trailer",
"\n",
"}",
"\n\n",
"return",
"f",
",",
"n",
",",
"nil",
"\n",
"}"
] | // Download initiates a file transfer from the guest | [
"Download",
"initiates",
"a",
"file",
"transfer",
"from",
"the",
"guest"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/guest/toolbox/client.go#L272-L297 | train |
vmware/govmomi | guest/toolbox/client.go | Upload | func (c *Client) Upload(ctx context.Context, src io.Reader, dst string, p soap.Upload, attr types.BaseGuestFileAttributes, force bool) error {
vc := c.ProcessManager.Client()
var err error
if p.ContentLength == 0 { // Content-Length is required
switch r := src.(type) {
case *bytes.Buffer:
p.ContentLength = int64(r.Len())
case *bytes.Reader:
p.ContentLength = int64(r.Len())
case *strings.Reader:
p.ContentLength = int64(r.Len())
case *os.File:
info, serr := r.Stat()
if serr != nil {
return serr
}
p.ContentLength = info.Size()
}
if p.ContentLength == 0 { // os.File for example could be a device (stdin)
buf := new(bytes.Buffer)
p.ContentLength, err = io.Copy(buf, src)
if err != nil {
return err
}
src = buf
}
}
url, err := c.FileManager.InitiateFileTransferToGuest(ctx, c.Authentication, dst, attr, p.ContentLength, force)
if err != nil {
return err
}
u, err := c.FileManager.TransferURL(ctx, url)
if err != nil {
return err
}
return vc.Client.Upload(ctx, src, u, &p)
} | go | func (c *Client) Upload(ctx context.Context, src io.Reader, dst string, p soap.Upload, attr types.BaseGuestFileAttributes, force bool) error {
vc := c.ProcessManager.Client()
var err error
if p.ContentLength == 0 { // Content-Length is required
switch r := src.(type) {
case *bytes.Buffer:
p.ContentLength = int64(r.Len())
case *bytes.Reader:
p.ContentLength = int64(r.Len())
case *strings.Reader:
p.ContentLength = int64(r.Len())
case *os.File:
info, serr := r.Stat()
if serr != nil {
return serr
}
p.ContentLength = info.Size()
}
if p.ContentLength == 0 { // os.File for example could be a device (stdin)
buf := new(bytes.Buffer)
p.ContentLength, err = io.Copy(buf, src)
if err != nil {
return err
}
src = buf
}
}
url, err := c.FileManager.InitiateFileTransferToGuest(ctx, c.Authentication, dst, attr, p.ContentLength, force)
if err != nil {
return err
}
u, err := c.FileManager.TransferURL(ctx, url)
if err != nil {
return err
}
return vc.Client.Upload(ctx, src, u, &p)
} | [
"func",
"(",
"c",
"*",
"Client",
")",
"Upload",
"(",
"ctx",
"context",
".",
"Context",
",",
"src",
"io",
".",
"Reader",
",",
"dst",
"string",
",",
"p",
"soap",
".",
"Upload",
",",
"attr",
"types",
".",
"BaseGuestFileAttributes",
",",
"force",
"bool",
")",
"error",
"{",
"vc",
":=",
"c",
".",
"ProcessManager",
".",
"Client",
"(",
")",
"\n\n",
"var",
"err",
"error",
"\n\n",
"if",
"p",
".",
"ContentLength",
"==",
"0",
"{",
"// Content-Length is required",
"switch",
"r",
":=",
"src",
".",
"(",
"type",
")",
"{",
"case",
"*",
"bytes",
".",
"Buffer",
":",
"p",
".",
"ContentLength",
"=",
"int64",
"(",
"r",
".",
"Len",
"(",
")",
")",
"\n",
"case",
"*",
"bytes",
".",
"Reader",
":",
"p",
".",
"ContentLength",
"=",
"int64",
"(",
"r",
".",
"Len",
"(",
")",
")",
"\n",
"case",
"*",
"strings",
".",
"Reader",
":",
"p",
".",
"ContentLength",
"=",
"int64",
"(",
"r",
".",
"Len",
"(",
")",
")",
"\n",
"case",
"*",
"os",
".",
"File",
":",
"info",
",",
"serr",
":=",
"r",
".",
"Stat",
"(",
")",
"\n",
"if",
"serr",
"!=",
"nil",
"{",
"return",
"serr",
"\n",
"}",
"\n\n",
"p",
".",
"ContentLength",
"=",
"info",
".",
"Size",
"(",
")",
"\n",
"}",
"\n\n",
"if",
"p",
".",
"ContentLength",
"==",
"0",
"{",
"// os.File for example could be a device (stdin)",
"buf",
":=",
"new",
"(",
"bytes",
".",
"Buffer",
")",
"\n\n",
"p",
".",
"ContentLength",
",",
"err",
"=",
"io",
".",
"Copy",
"(",
"buf",
",",
"src",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"src",
"=",
"buf",
"\n",
"}",
"\n",
"}",
"\n\n",
"url",
",",
"err",
":=",
"c",
".",
"FileManager",
".",
"InitiateFileTransferToGuest",
"(",
"ctx",
",",
"c",
".",
"Authentication",
",",
"dst",
",",
"attr",
",",
"p",
".",
"ContentLength",
",",
"force",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"u",
",",
"err",
":=",
"c",
".",
"FileManager",
".",
"TransferURL",
"(",
"ctx",
",",
"url",
")",
"\n",
"if",
"err",
"!=",
"nil",
"{",
"return",
"err",
"\n",
"}",
"\n\n",
"return",
"vc",
".",
"Client",
".",
"Upload",
"(",
"ctx",
",",
"src",
",",
"u",
",",
"&",
"p",
")",
"\n",
"}"
] | // Upload transfers a file to the guest | [
"Upload",
"transfers",
"a",
"file",
"to",
"the",
"guest"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/guest/toolbox/client.go#L300-L345 | train |
vmware/govmomi | object/host_firewall_system.go | ByRule | func (l HostFirewallRulesetList) ByRule(rule types.HostFirewallRule) HostFirewallRulesetList {
var matches HostFirewallRulesetList
for _, rs := range l {
for _, r := range rs.Rule {
if r.PortType != rule.PortType ||
r.Protocol != rule.Protocol ||
r.Direction != rule.Direction {
continue
}
if r.EndPort == 0 && rule.Port == r.Port ||
rule.Port >= r.Port && rule.Port <= r.EndPort {
matches = append(matches, rs)
break
}
}
}
return matches
} | go | func (l HostFirewallRulesetList) ByRule(rule types.HostFirewallRule) HostFirewallRulesetList {
var matches HostFirewallRulesetList
for _, rs := range l {
for _, r := range rs.Rule {
if r.PortType != rule.PortType ||
r.Protocol != rule.Protocol ||
r.Direction != rule.Direction {
continue
}
if r.EndPort == 0 && rule.Port == r.Port ||
rule.Port >= r.Port && rule.Port <= r.EndPort {
matches = append(matches, rs)
break
}
}
}
return matches
} | [
"func",
"(",
"l",
"HostFirewallRulesetList",
")",
"ByRule",
"(",
"rule",
"types",
".",
"HostFirewallRule",
")",
"HostFirewallRulesetList",
"{",
"var",
"matches",
"HostFirewallRulesetList",
"\n\n",
"for",
"_",
",",
"rs",
":=",
"range",
"l",
"{",
"for",
"_",
",",
"r",
":=",
"range",
"rs",
".",
"Rule",
"{",
"if",
"r",
".",
"PortType",
"!=",
"rule",
".",
"PortType",
"||",
"r",
".",
"Protocol",
"!=",
"rule",
".",
"Protocol",
"||",
"r",
".",
"Direction",
"!=",
"rule",
".",
"Direction",
"{",
"continue",
"\n",
"}",
"\n\n",
"if",
"r",
".",
"EndPort",
"==",
"0",
"&&",
"rule",
".",
"Port",
"==",
"r",
".",
"Port",
"||",
"rule",
".",
"Port",
">=",
"r",
".",
"Port",
"&&",
"rule",
".",
"Port",
"<=",
"r",
".",
"EndPort",
"{",
"matches",
"=",
"append",
"(",
"matches",
",",
"rs",
")",
"\n",
"break",
"\n",
"}",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"matches",
"\n",
"}"
] | // ByRule returns a HostFirewallRulesetList where Direction, PortType and Protocol are equal and Port is within range | [
"ByRule",
"returns",
"a",
"HostFirewallRulesetList",
"where",
"Direction",
"PortType",
"and",
"Protocol",
"are",
"equal",
"and",
"Port",
"is",
"within",
"range"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/host_firewall_system.go#L85-L105 | train |
vmware/govmomi | object/host_firewall_system.go | Enabled | func (l HostFirewallRulesetList) Enabled() HostFirewallRulesetList {
var matches HostFirewallRulesetList
for _, rs := range l {
if rs.Enabled {
matches = append(matches, rs)
}
}
return matches
} | go | func (l HostFirewallRulesetList) Enabled() HostFirewallRulesetList {
var matches HostFirewallRulesetList
for _, rs := range l {
if rs.Enabled {
matches = append(matches, rs)
}
}
return matches
} | [
"func",
"(",
"l",
"HostFirewallRulesetList",
")",
"Enabled",
"(",
")",
"HostFirewallRulesetList",
"{",
"var",
"matches",
"HostFirewallRulesetList",
"\n\n",
"for",
"_",
",",
"rs",
":=",
"range",
"l",
"{",
"if",
"rs",
".",
"Enabled",
"{",
"matches",
"=",
"append",
"(",
"matches",
",",
"rs",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"matches",
"\n",
"}"
] | // Enabled returns a HostFirewallRulesetList with enabled rules | [
"Enabled",
"returns",
"a",
"HostFirewallRulesetList",
"with",
"enabled",
"rules"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/host_firewall_system.go#L147-L157 | train |
vmware/govmomi | object/host_firewall_system.go | Keys | func (l HostFirewallRulesetList) Keys() []string {
var keys []string
for _, rs := range l {
keys = append(keys, rs.Key)
}
return keys
} | go | func (l HostFirewallRulesetList) Keys() []string {
var keys []string
for _, rs := range l {
keys = append(keys, rs.Key)
}
return keys
} | [
"func",
"(",
"l",
"HostFirewallRulesetList",
")",
"Keys",
"(",
")",
"[",
"]",
"string",
"{",
"var",
"keys",
"[",
"]",
"string",
"\n\n",
"for",
"_",
",",
"rs",
":=",
"range",
"l",
"{",
"keys",
"=",
"append",
"(",
"keys",
",",
"rs",
".",
"Key",
")",
"\n",
"}",
"\n\n",
"return",
"keys",
"\n",
"}"
] | // Keys returns the HostFirewallRuleset.Key for each ruleset in the list | [
"Keys",
"returns",
"the",
"HostFirewallRuleset",
".",
"Key",
"for",
"each",
"ruleset",
"in",
"the",
"list"
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/object/host_firewall_system.go#L173-L181 | train |
vmware/govmomi | vim25/xml/xml.go | CopyToken | func CopyToken(t Token) Token {
switch v := t.(type) {
case CharData:
return v.Copy()
case Comment:
return v.Copy()
case Directive:
return v.Copy()
case ProcInst:
return v.Copy()
case StartElement:
return v.Copy()
}
return t
} | go | func CopyToken(t Token) Token {
switch v := t.(type) {
case CharData:
return v.Copy()
case Comment:
return v.Copy()
case Directive:
return v.Copy()
case ProcInst:
return v.Copy()
case StartElement:
return v.Copy()
}
return t
} | [
"func",
"CopyToken",
"(",
"t",
"Token",
")",
"Token",
"{",
"switch",
"v",
":=",
"t",
".",
"(",
"type",
")",
"{",
"case",
"CharData",
":",
"return",
"v",
".",
"Copy",
"(",
")",
"\n",
"case",
"Comment",
":",
"return",
"v",
".",
"Copy",
"(",
")",
"\n",
"case",
"Directive",
":",
"return",
"v",
".",
"Copy",
"(",
")",
"\n",
"case",
"ProcInst",
":",
"return",
"v",
".",
"Copy",
"(",
")",
"\n",
"case",
"StartElement",
":",
"return",
"v",
".",
"Copy",
"(",
")",
"\n",
"}",
"\n",
"return",
"t",
"\n",
"}"
] | // CopyToken returns a copy of a Token. | [
"CopyToken",
"returns",
"a",
"copy",
"of",
"a",
"Token",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/xml.go#L118-L132 | train |
vmware/govmomi | vim25/xml/xml.go | NewDecoder | func NewDecoder(r io.Reader) *Decoder {
d := &Decoder{
ns: make(map[string]string),
nextByte: -1,
line: 1,
Strict: true,
}
d.switchToReader(r)
return d
} | go | func NewDecoder(r io.Reader) *Decoder {
d := &Decoder{
ns: make(map[string]string),
nextByte: -1,
line: 1,
Strict: true,
}
d.switchToReader(r)
return d
} | [
"func",
"NewDecoder",
"(",
"r",
"io",
".",
"Reader",
")",
"*",
"Decoder",
"{",
"d",
":=",
"&",
"Decoder",
"{",
"ns",
":",
"make",
"(",
"map",
"[",
"string",
"]",
"string",
")",
",",
"nextByte",
":",
"-",
"1",
",",
"line",
":",
"1",
",",
"Strict",
":",
"true",
",",
"}",
"\n",
"d",
".",
"switchToReader",
"(",
"r",
")",
"\n",
"return",
"d",
"\n",
"}"
] | // NewDecoder creates a new XML parser reading from r.
// If r does not implement io.ByteReader, NewDecoder will
// do its own buffering. | [
"NewDecoder",
"creates",
"a",
"new",
"XML",
"parser",
"reading",
"from",
"r",
".",
"If",
"r",
"does",
"not",
"implement",
"io",
".",
"ByteReader",
"NewDecoder",
"will",
"do",
"its",
"own",
"buffering",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/xml.go#L209-L218 | train |
vmware/govmomi | vim25/xml/xml.go | popEOF | func (d *Decoder) popEOF() bool {
if d.stk == nil || d.stk.kind != stkEOF {
return false
}
d.pop()
return true
} | go | func (d *Decoder) popEOF() bool {
if d.stk == nil || d.stk.kind != stkEOF {
return false
}
d.pop()
return true
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"popEOF",
"(",
")",
"bool",
"{",
"if",
"d",
".",
"stk",
"==",
"nil",
"||",
"d",
".",
"stk",
".",
"kind",
"!=",
"stkEOF",
"{",
"return",
"false",
"\n",
"}",
"\n",
"d",
".",
"pop",
"(",
")",
"\n",
"return",
"true",
"\n",
"}"
] | // Undo a pushEOF.
// The element must have been finished, so the EOF should be at the top of the stack. | [
"Undo",
"a",
"pushEOF",
".",
"The",
"element",
"must",
"have",
"been",
"finished",
"so",
"the",
"EOF",
"should",
"be",
"at",
"the",
"top",
"of",
"the",
"stack",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/xml.go#L401-L407 | train |
vmware/govmomi | vim25/xml/xml.go | pushElement | func (d *Decoder) pushElement(name Name) {
s := d.push(stkStart)
s.name = name
} | go | func (d *Decoder) pushElement(name Name) {
s := d.push(stkStart)
s.name = name
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"pushElement",
"(",
"name",
"Name",
")",
"{",
"s",
":=",
"d",
".",
"push",
"(",
"stkStart",
")",
"\n",
"s",
".",
"name",
"=",
"name",
"\n",
"}"
] | // Record that we are starting an element with the given name. | [
"Record",
"that",
"we",
"are",
"starting",
"an",
"element",
"with",
"the",
"given",
"name",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/xml.go#L410-L413 | train |
vmware/govmomi | vim25/xml/xml.go | syntaxError | func (d *Decoder) syntaxError(msg string) error {
return &SyntaxError{Msg: msg, Line: d.line}
} | go | func (d *Decoder) syntaxError(msg string) error {
return &SyntaxError{Msg: msg, Line: d.line}
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"syntaxError",
"(",
"msg",
"string",
")",
"error",
"{",
"return",
"&",
"SyntaxError",
"{",
"Msg",
":",
"msg",
",",
"Line",
":",
"d",
".",
"line",
"}",
"\n",
"}"
] | // Creates a SyntaxError with the current line number. | [
"Creates",
"a",
"SyntaxError",
"with",
"the",
"current",
"line",
"number",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/xml.go#L425-L427 | train |
vmware/govmomi | vim25/xml/xml.go | popElement | func (d *Decoder) popElement(t *EndElement) bool {
s := d.pop()
name := t.Name
switch {
case s == nil || s.kind != stkStart:
d.err = d.syntaxError("unexpected end element </" + name.Local + ">")
return false
case s.name.Local != name.Local:
if !d.Strict {
d.needClose = true
d.toClose = t.Name
t.Name = s.name
return true
}
d.err = d.syntaxError("element <" + s.name.Local + "> closed by </" + name.Local + ">")
return false
case s.name.Space != name.Space:
d.err = d.syntaxError("element <" + s.name.Local + "> in space " + s.name.Space +
"closed by </" + name.Local + "> in space " + name.Space)
return false
}
// Pop stack until a Start or EOF is on the top, undoing the
// translations that were associated with the element we just closed.
for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF {
s := d.pop()
if s.ok {
d.ns[s.name.Local] = s.name.Space
} else {
delete(d.ns, s.name.Local)
}
}
return true
} | go | func (d *Decoder) popElement(t *EndElement) bool {
s := d.pop()
name := t.Name
switch {
case s == nil || s.kind != stkStart:
d.err = d.syntaxError("unexpected end element </" + name.Local + ">")
return false
case s.name.Local != name.Local:
if !d.Strict {
d.needClose = true
d.toClose = t.Name
t.Name = s.name
return true
}
d.err = d.syntaxError("element <" + s.name.Local + "> closed by </" + name.Local + ">")
return false
case s.name.Space != name.Space:
d.err = d.syntaxError("element <" + s.name.Local + "> in space " + s.name.Space +
"closed by </" + name.Local + "> in space " + name.Space)
return false
}
// Pop stack until a Start or EOF is on the top, undoing the
// translations that were associated with the element we just closed.
for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF {
s := d.pop()
if s.ok {
d.ns[s.name.Local] = s.name.Space
} else {
delete(d.ns, s.name.Local)
}
}
return true
} | [
"func",
"(",
"d",
"*",
"Decoder",
")",
"popElement",
"(",
"t",
"*",
"EndElement",
")",
"bool",
"{",
"s",
":=",
"d",
".",
"pop",
"(",
")",
"\n",
"name",
":=",
"t",
".",
"Name",
"\n",
"switch",
"{",
"case",
"s",
"==",
"nil",
"||",
"s",
".",
"kind",
"!=",
"stkStart",
":",
"d",
".",
"err",
"=",
"d",
".",
"syntaxError",
"(",
"\"",
"\"",
"+",
"name",
".",
"Local",
"+",
"\"",
"\"",
")",
"\n",
"return",
"false",
"\n",
"case",
"s",
".",
"name",
".",
"Local",
"!=",
"name",
".",
"Local",
":",
"if",
"!",
"d",
".",
"Strict",
"{",
"d",
".",
"needClose",
"=",
"true",
"\n",
"d",
".",
"toClose",
"=",
"t",
".",
"Name",
"\n",
"t",
".",
"Name",
"=",
"s",
".",
"name",
"\n",
"return",
"true",
"\n",
"}",
"\n",
"d",
".",
"err",
"=",
"d",
".",
"syntaxError",
"(",
"\"",
"\"",
"+",
"s",
".",
"name",
".",
"Local",
"+",
"\"",
"\"",
"+",
"name",
".",
"Local",
"+",
"\"",
"\"",
")",
"\n",
"return",
"false",
"\n",
"case",
"s",
".",
"name",
".",
"Space",
"!=",
"name",
".",
"Space",
":",
"d",
".",
"err",
"=",
"d",
".",
"syntaxError",
"(",
"\"",
"\"",
"+",
"s",
".",
"name",
".",
"Local",
"+",
"\"",
"\"",
"+",
"s",
".",
"name",
".",
"Space",
"+",
"\"",
"\"",
"+",
"name",
".",
"Local",
"+",
"\"",
"\"",
"+",
"name",
".",
"Space",
")",
"\n",
"return",
"false",
"\n",
"}",
"\n\n",
"// Pop stack until a Start or EOF is on the top, undoing the",
"// translations that were associated with the element we just closed.",
"for",
"d",
".",
"stk",
"!=",
"nil",
"&&",
"d",
".",
"stk",
".",
"kind",
"!=",
"stkStart",
"&&",
"d",
".",
"stk",
".",
"kind",
"!=",
"stkEOF",
"{",
"s",
":=",
"d",
".",
"pop",
"(",
")",
"\n",
"if",
"s",
".",
"ok",
"{",
"d",
".",
"ns",
"[",
"s",
".",
"name",
".",
"Local",
"]",
"=",
"s",
".",
"name",
".",
"Space",
"\n",
"}",
"else",
"{",
"delete",
"(",
"d",
".",
"ns",
",",
"s",
".",
"name",
".",
"Local",
")",
"\n",
"}",
"\n",
"}",
"\n\n",
"return",
"true",
"\n",
"}"
] | // Record that we are ending an element with the given name.
// The name must match the record at the top of the stack,
// which must be a pushElement record.
// After popping the element, apply any undo records from
// the stack to restore the name translations that existed
// before we saw this element. | [
"Record",
"that",
"we",
"are",
"ending",
"an",
"element",
"with",
"the",
"given",
"name",
".",
"The",
"name",
"must",
"match",
"the",
"record",
"at",
"the",
"top",
"of",
"the",
"stack",
"which",
"must",
"be",
"a",
"pushElement",
"record",
".",
"After",
"popping",
"the",
"element",
"apply",
"any",
"undo",
"records",
"from",
"the",
"stack",
"to",
"restore",
"the",
"name",
"translations",
"that",
"existed",
"before",
"we",
"saw",
"this",
"element",
"."
] | fc3f0e9d2275df0e497a80917807a7c72d3c35bc | https://github.com/vmware/govmomi/blob/fc3f0e9d2275df0e497a80917807a7c72d3c35bc/vim25/xml/xml.go#L435-L469 | train |