本文整理汇总了Golang中github.com/docker/docker/pkg/parsers/filters.FromParam函数的典型用法代码示例。如果您正苦于以下问题:Golang FromParam函数的具体用法?Golang FromParam怎么用?Golang FromParam使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FromParam函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getImagesJSON
// GET /images/json
func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
all := boolValue(r, "all")
filters, err := dockerfilters.FromParam(r.Form.Get("filters"))
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
// TODO: apply node filter in engine?
accepteds, _ := filters["node"]
// this struct helps grouping images
// but still keeps their Engine infos as an array.
groupImages := make(map[string]dockerclient.Image)
for _, image := range c.cluster.Images(all, filters) {
if len(accepteds) != 0 {
found := false
for _, accepted := range accepteds {
if accepted == image.Engine.Name || accepted == image.Engine.ID {
found = true
break
}
}
if !found {
continue
}
}
// grouping images by Id, and concat their RepoTags
if entry, existed := groupImages[image.Id]; existed {
entry.RepoTags = append(entry.RepoTags, image.RepoTags...)
groupImages[image.Id] = entry
} else {
groupImages[image.Id] = image.Image
}
}
images := []dockerclient.Image{}
for _, image := range groupImages {
// de-duplicate RepoTags
result := []string{}
seen := map[string]bool{}
for _, val := range image.RepoTags {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = true
}
}
image.RepoTags = result
images = append(images, image)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(images)
}
示例2: Volumes
// Volumes lists known volumes, using the filter to restrict the range
// of volumes returned.
func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, error) {
var volumesOut []*types.Volume
volFilters, err := filters.FromParam(filter)
if err != nil {
return nil, err
}
filterUsed := false
if i, ok := volFilters["dangling"]; ok {
if len(i) > 1 {
return nil, derr.ErrorCodeDanglingOne
}
filterValue := i[0]
if strings.ToLower(filterValue) == "true" || filterValue == "1" {
filterUsed = true
}
}
volumes := daemon.volumes.List()
for _, v := range volumes {
if filterUsed && daemon.volumes.Count(v) > 0 {
continue
}
volumesOut = append(volumesOut, volumeToAPIType(v))
}
return volumesOut, nil
}
示例3: getImagesJSON
// GET /images/json
func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
filters, err := dockerfilters.FromParam(r.Form.Get("filters"))
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
accepteds, _ := filters["node"]
images := []*cluster.Image{}
for _, image := range c.cluster.Images() {
if len(accepteds) != 0 {
found := false
for _, accepted := range accepteds {
if accepted == image.Engine.Name || accepted == image.Engine.ID {
found = true
break
}
}
if !found {
continue
}
}
images = append(images, image)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(images)
}
示例4: Get
func (e *Events) Get(job *engine.Job) error {
var (
since = job.GetenvInt64("since")
until = job.GetenvInt64("until")
timeout = time.NewTimer(time.Unix(until, 0).Sub(time.Now()))
)
eventFilters, err := filters.FromParam(job.Getenv("filters"))
if err != nil {
return err
}
// If no until, disable timeout
if until == 0 {
timeout.Stop()
}
listener := make(chan *jsonmessage.JSONMessage)
e.subscribe(listener)
defer e.unsubscribe(listener)
job.Stdout.Write(nil)
// Resend every event in the [since, until] time interval.
if since != 0 {
if err := e.writeCurrent(job, since, until, eventFilters); err != nil {
return err
}
}
for {
select {
case event, ok := <-listener:
if !ok {
return nil
}
if err := writeEvent(job, event, eventFilters); err != nil {
return err
}
case <-timeout.C:
return nil
}
}
}
示例5: Volumes
// Volumes lists known volumes, using the filter to restrict the range
// of volumes returned.
func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, error) {
var volumesOut []*types.Volume
volFilters, err := filters.FromParam(filter)
if err != nil {
return nil, err
}
filterUsed := volFilters.Include("dangling") &&
(volFilters.ExactMatch("dangling", "true") || volFilters.ExactMatch("dangling", "1"))
volumes := daemon.volumes.List()
for _, v := range volumes {
if filterUsed && daemon.volumes.Count(v) > 0 {
continue
}
volumesOut = append(volumesOut, volumeToAPIType(v))
}
return volumesOut, nil
}
示例6: getNetworksList
func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
filter := r.Form.Get("filters")
netFilters, err := filters.FromParam(filter)
if err != nil {
return err
}
list := []*types.NetworkResource{}
var nameFilter, idFilter bool
var names, ids []string
if names, nameFilter = netFilters["name"]; nameFilter {
for _, name := range names {
if nw, err := n.daemon.GetNetwork(name, daemon.NetworkByName); err == nil {
list = append(list, buildNetworkResource(nw))
} else {
logrus.Errorf("failed to get network for filter=%s : %v", name, err)
}
}
}
if ids, idFilter = netFilters["id"]; idFilter {
for _, id := range ids {
for _, nw := range n.daemon.GetNetworksByID(id) {
list = append(list, buildNetworkResource(nw))
}
}
}
if !nameFilter && !idFilter {
nwList := n.daemon.GetNetworksByID("")
for _, nw := range nwList {
list = append(list, buildNetworkResource(nw))
}
}
return httputils.WriteJSON(w, http.StatusOK, list)
}
示例7: getNetworks
// GET /networks
func getNetworks(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
filters, err := dockerfilters.FromParam(r.Form.Get("filters"))
if err != nil {
httpError(w, err.Error(), http.StatusBadRequest)
return
}
out := []*dockerclient.NetworkResource{}
networks := c.cluster.Networks().Filter(filters["name"], filters["id"])
for _, network := range networks {
tmp := (*network).NetworkResource
if tmp.Scope == "local" {
tmp.Name = network.Engine.Name + "/" + network.Name
}
out = append(out, &tmp)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(out)
}
示例8: getNetworksList
func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
filter := r.Form.Get("filters")
netFilters, err := filters.FromParam(filter)
if err != nil {
return err
}
list := []*types.NetworkResource{}
netFilters.WalkValues("name", func(name string) error {
if nw, err := n.backend.GetNetwork(name, daemon.NetworkByName); err == nil {
list = append(list, buildNetworkResource(nw))
} else {
logrus.Errorf("failed to get network for filter=%s : %v", name, err)
}
return nil
})
netFilters.WalkValues("id", func(id string) error {
for _, nw := range n.backend.GetNetworksByID(id) {
list = append(list, buildNetworkResource(nw))
}
return nil
})
if !netFilters.Include("name") && !netFilters.Include("id") {
nwList := n.backend.GetNetworksByID("")
for _, nw := range nwList {
list = append(list, buildNetworkResource(nw))
}
}
return httputils.WriteJSON(w, http.StatusOK, list)
}
示例9: getEvents
func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
since, err := httputils.Int64ValueOrDefault(r, "since", -1)
if err != nil {
return err
}
until, err := httputils.Int64ValueOrDefault(r, "until", -1)
if err != nil {
return err
}
timer := time.NewTimer(0)
timer.Stop()
if until > 0 {
dur := time.Unix(until, 0).Sub(time.Now())
timer = time.NewTimer(dur)
}
ef, err := filters.FromParam(r.Form.Get("filters"))
if err != nil {
return err
}
enc := buildOutputEncoder(w)
d := s.daemon
es := d.EventsService
current, l := es.Subscribe()
defer es.Evict(l)
eventFilter := d.GetEventFilter(ef)
handleEvent := func(ev *jsonmessage.JSONMessage) error {
if eventFilter.Include(ev) {
if err := enc.Encode(ev); err != nil {
return err
}
}
return nil
}
if since == -1 {
current = nil
}
for _, ev := range current {
if ev.Time < since {
continue
}
if err := handleEvent(ev); err != nil {
return err
}
}
var closeNotify <-chan bool
if closeNotifier, ok := w.(http.CloseNotifier); ok {
closeNotify = closeNotifier.CloseNotify()
}
for {
select {
case ev := <-l:
jev, ok := ev.(*jsonmessage.JSONMessage)
if !ok {
continue
}
if err := handleEvent(jev); err != nil {
return err
}
case <-timer.C:
return nil
case <-closeNotify:
logrus.Debug("Client disconnected, stop sending events")
return nil
}
}
}
示例10: getEvents
func (s *router) getEvents(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
if err := httputils.ParseForm(r); err != nil {
return err
}
since, sinceNano, err := timeutils.ParseTimestamps(r.Form.Get("since"), -1)
if err != nil {
return err
}
until, untilNano, err := timeutils.ParseTimestamps(r.Form.Get("until"), -1)
if err != nil {
return err
}
timer := time.NewTimer(0)
timer.Stop()
if until > 0 || untilNano > 0 {
dur := time.Unix(until, untilNano).Sub(time.Now())
timer = time.NewTimer(dur)
}
ef, err := filters.FromParam(r.Form.Get("filters"))
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
// This is to ensure that the HTTP status code is sent immediately,
// so that it will not block the receiver.
w.WriteHeader(http.StatusOK)
if flusher, ok := w.(http.Flusher); ok {
flusher.Flush()
}
output := ioutils.NewWriteFlusher(w)
defer output.Close()
enc := json.NewEncoder(output)
current, l, cancel := s.daemon.SubscribeToEvents()
defer cancel()
eventFilter := s.daemon.GetEventFilter(ef)
handleEvent := func(ev *jsonmessage.JSONMessage) error {
if eventFilter.Include(ev) {
if err := enc.Encode(ev); err != nil {
return err
}
}
return nil
}
if since == -1 {
current = nil
}
for _, ev := range current {
if ev.Time < since || ((ev.Time == since) && (ev.TimeNano < sinceNano)) {
continue
}
if err := handleEvent(ev); err != nil {
return err
}
}
var closeNotify <-chan bool
if closeNotifier, ok := w.(http.CloseNotifier); ok {
closeNotify = closeNotifier.CloseNotify()
}
for {
select {
case ev := <-l:
jev, ok := ev.(*jsonmessage.JSONMessage)
if !ok {
continue
}
if err := handleEvent(jev); err != nil {
return err
}
case <-timer.C:
return nil
case <-closeNotify:
logrus.Debug("Client disconnected, stop sending events")
return nil
}
}
}
示例11: foldFilter
// foldFilter generates the container filter based in the user's filtering options.
func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error) {
psFilters, err := filters.FromParam(config.Filters)
if err != nil {
return nil, err
}
var filtExited []int
if i, ok := psFilters["exited"]; ok {
for _, value := range i {
code, err := strconv.Atoi(value)
if err != nil {
return nil, err
}
filtExited = append(filtExited, code)
}
}
if i, ok := psFilters["status"]; ok {
for _, value := range i {
if !isValidStateString(value) {
return nil, errors.New("Unrecognised filter value for status")
}
if value == "exited" || value == "created" {
config.All = true
}
}
}
imagesFilter := map[string]bool{}
var ancestorFilter bool
if ancestors, ok := psFilters["ancestor"]; ok {
ancestorFilter = true
byParents := daemon.Graph().ByParent()
// The idea is to walk the graph down the most "efficient" way.
for _, ancestor := range ancestors {
// First, get the imageId of the ancestor filter (yay)
image, err := daemon.repositories.LookupImage(ancestor)
if err != nil {
logrus.Warnf("Error while looking up for image %v", ancestor)
continue
}
if imagesFilter[ancestor] {
// Already seen this ancestor, skip it
continue
}
// Then walk down the graph and put the imageIds in imagesFilter
populateImageFilterByParents(imagesFilter, image.ID, byParents)
}
}
names := make(map[string][]string)
daemon.containerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
names[e.ID()] = append(names[e.ID()], p)
return nil
}, 1)
var beforeCont, sinceCont *Container
if config.Before != "" {
beforeCont, err = daemon.Get(config.Before)
if err != nil {
return nil, err
}
}
if config.Since != "" {
sinceCont, err = daemon.Get(config.Since)
if err != nil {
return nil, err
}
}
return &listContext{
filters: psFilters,
ancestorFilter: ancestorFilter,
names: names,
images: imagesFilter,
exitAllowed: filtExited,
beforeContainer: beforeCont,
sinceContainer: sinceCont,
ContainersConfig: config,
}, nil
}
示例12: Containers
func (daemon *Daemon) Containers(job *engine.Job) error {
var (
foundBefore bool
displayed int
all = job.GetenvBool("all")
since = job.Getenv("since")
before = job.Getenv("before")
n = job.GetenvInt("limit")
size = job.GetenvBool("size")
psFilters filters.Args
filtExited []int
)
containers := []types.Container{}
psFilters, err := filters.FromParam(job.Getenv("filters"))
if err != nil {
return err
}
if i, ok := psFilters["exited"]; ok {
for _, value := range i {
code, err := strconv.Atoi(value)
if err != nil {
return err
}
filtExited = append(filtExited, code)
}
}
if i, ok := psFilters["status"]; ok {
for _, value := range i {
if value == "exited" {
all = true
}
}
}
names := map[string][]string{}
daemon.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
names[e.ID()] = append(names[e.ID()], p)
return nil
}, 1)
var beforeCont, sinceCont *Container
if before != "" {
beforeCont, err = daemon.Get(before)
if err != nil {
return err
}
}
if since != "" {
sinceCont, err = daemon.Get(since)
if err != nil {
return err
}
}
errLast := errors.New("last container")
writeCont := func(container *Container) error {
container.Lock()
defer container.Unlock()
if !container.Running && !all && n <= 0 && since == "" && before == "" {
return nil
}
if !psFilters.Match("name", container.Name) {
return nil
}
if !psFilters.Match("id", container.ID) {
return nil
}
if !psFilters.MatchKVList("label", container.Config.Labels) {
return nil
}
if before != "" && !foundBefore {
if container.ID == beforeCont.ID {
foundBefore = true
}
return nil
}
if n > 0 && displayed == n {
return errLast
}
if since != "" {
if container.ID == sinceCont.ID {
return errLast
}
}
if len(filtExited) > 0 {
shouldSkip := true
for _, code := range filtExited {
if code == container.ExitCode && !container.Running {
shouldSkip = false
break
}
}
if shouldSkip {
return nil
}
//.........这里部分代码省略.........
示例13: Containers
// Containers returns a list of all the containers.
func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container, error) {
var (
foundBefore bool
displayed int
ancestorFilter bool
all = config.All
n = config.Limit
psFilters filters.Args
filtExited []int
)
imagesFilter := map[string]bool{}
containers := []*types.Container{}
psFilters, err := filters.FromParam(config.Filters)
if err != nil {
return nil, err
}
if i, ok := psFilters["exited"]; ok {
for _, value := range i {
code, err := strconv.Atoi(value)
if err != nil {
return nil, err
}
filtExited = append(filtExited, code)
}
}
if i, ok := psFilters["status"]; ok {
for _, value := range i {
if !isValidStateString(value) {
return nil, errors.New("Unrecognised filter value for status")
}
if value == "exited" || value == "created" {
all = true
}
}
}
if ancestors, ok := psFilters["ancestor"]; ok {
ancestorFilter = true
byParents := daemon.Graph().ByParent()
// The idea is to walk the graph down the most "efficient" way.
for _, ancestor := range ancestors {
// First, get the imageId of the ancestor filter (yay)
image, err := daemon.Repositories().LookupImage(ancestor)
if err != nil {
logrus.Warnf("Error while looking up for image %v", ancestor)
continue
}
if imagesFilter[ancestor] {
// Already seen this ancestor, skip it
continue
}
// Then walk down the graph and put the imageIds in imagesFilter
populateImageFilterByParents(imagesFilter, image.ID, byParents)
}
}
names := map[string][]string{}
daemon.containerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
names[e.ID()] = append(names[e.ID()], p)
return nil
}, 1)
var beforeCont, sinceCont *Container
if config.Before != "" {
beforeCont, err = daemon.Get(config.Before)
if err != nil {
return nil, err
}
}
if config.Since != "" {
sinceCont, err = daemon.Get(config.Since)
if err != nil {
return nil, err
}
}
errLast := errors.New("last container")
writeCont := func(container *Container) error {
container.Lock()
defer container.Unlock()
if !container.Running && !all && n <= 0 && config.Since == "" && config.Before == "" {
return nil
}
if !psFilters.Match("name", container.Name) {
return nil
}
if !psFilters.Match("id", container.ID) {
return nil
}
if !psFilters.MatchKVList("label", container.Config.Labels) {
return nil
}
if config.Before != "" && !foundBefore {
//.........这里部分代码省略.........
示例14: Containers
func (daemon *Daemon) Containers(job *engine.Job) engine.Status {
var (
foundBefore bool
displayed int
all = job.GetenvBool("all")
since = job.Getenv("since")
before = job.Getenv("before")
n = job.GetenvInt("limit")
size = job.GetenvBool("size")
psFilters filters.Args
filt_exited []int
)
outs := engine.NewTable("Created", 0)
psFilters, err := filters.FromParam(job.Getenv("filters"))
if err != nil {
return job.Error(err)
}
if i, ok := psFilters["exited"]; ok {
for _, value := range i {
code, err := strconv.Atoi(value)
if err != nil {
return job.Error(err)
}
filt_exited = append(filt_exited, code)
}
}
if i, ok := psFilters["status"]; ok {
for _, value := range i {
if value == "exited" {
all = true
}
}
}
names := map[string][]string{}
daemon.ContainerGraph().Walk("/", func(p string, e *graphdb.Entity) error {
names[e.ID()] = append(names[e.ID()], p)
return nil
}, 1)
var beforeCont, sinceCont *Container
if before != "" {
beforeCont, err = daemon.Get(before)
if err != nil {
return job.Error(err)
}
}
if since != "" {
sinceCont, err = daemon.Get(since)
if err != nil {
return job.Error(err)
}
}
errLast := errors.New("last container")
writeCont := func(container *Container) error {
container.Lock()
defer container.Unlock()
if !container.Running && !all && n <= 0 && since == "" && before == "" {
return nil
}
if !psFilters.Match("name", container.Name) {
return nil
}
if !psFilters.Match("id", container.ID) {
return nil
}
if !psFilters.MatchKVList("label", container.Config.Labels) {
return nil
}
if before != "" && !foundBefore {
if container.ID == beforeCont.ID {
foundBefore = true
}
return nil
}
if n > 0 && displayed == n {
return errLast
}
if since != "" {
if container.ID == sinceCont.ID {
return errLast
}
}
if len(filt_exited) > 0 {
should_skip := true
for _, code := range filt_exited {
if code == container.ExitCode && !container.Running {
should_skip = false
break
}
}
if should_skip {
return nil
}
//.........这里部分代码省略.........
示例15: getContainersJSON
// GET /containers/ps
// GET /containers/json
func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
// Parse flags.
all := boolValue(r, "all")
limit := intValueOrZero(r, "limit")
// Parse filters.
filters, err := dockerfilters.FromParam(r.Form.Get("filters"))
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
filtExited := []int{}
if i, ok := filters["exited"]; ok {
for _, value := range i {
code, err := strconv.Atoi(value)
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
filtExited = append(filtExited, code)
}
}
if i, ok := filters["status"]; ok {
for _, value := range i {
if value == "exited" {
all = true
}
}
}
// Filtering: select the containers we want to return.
candidates := []*cluster.Container{}
for _, container := range c.cluster.Containers() {
// Skip stopped containers unless -a was specified.
if !container.Info.State.Running && !all && limit <= 0 {
continue
}
// Skip swarm containers unless -a was specified.
if strings.Split(container.Image, ":")[0] == "swarm" && !all {
continue
}
// Apply filters.
if !filters.Match("name", strings.TrimPrefix(container.Names[0], "/")) {
continue
}
if !filters.Match("id", container.Id) {
continue
}
if !filters.MatchKVList("label", container.Config.Labels) {
continue
}
if !filters.Match("status", container.Info.State.StateString()) {
continue
}
if len(filtExited) > 0 {
shouldSkip := true
for _, code := range filtExited {
if code == container.Info.State.ExitCode && !container.Info.State.Running {
shouldSkip = false
break
}
}
if shouldSkip {
continue
}
}
candidates = append(candidates, container)
}
// Sort the candidates and apply limits.
sort.Sort(sort.Reverse(ContainerSorter(candidates)))
if limit > 0 && limit < len(candidates) {
candidates = candidates[:limit]
}
// Convert cluster.Container back into dockerclient.Container.
out := []*dockerclient.Container{}
for _, container := range candidates {
// Create a copy of the underlying dockerclient.Container so we can
// make changes without messing with cluster.Container.
tmp := (*container).Container
// Update the Status. The one we have is stale from the last `docker ps` the engine sent.
// `Status()` will generate a new one
tmp.Status = container.Info.State.String()
if !container.Engine.IsHealthy() {
tmp.Status = "Host Down"
}
//.........这里部分代码省略.........