From f51e520e99c8d958c7538acdeabd9e50e65d1522 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Wed, 15 Mar 2017 16:44:52 -0400 Subject: [PATCH] Add a library function for listing all builders Add OpenAllBuilders(), for listing all open builders. Signed-off-by: Nalin Dahyabhai --- buildah.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/buildah.go b/buildah.go index b8412cc90..910816fa3 100644 --- a/buildah.go +++ b/buildah.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" "path/filepath" "github.com/containers/storage/pkg/ioutils" @@ -216,6 +217,32 @@ func OpenBuilderByPath(store storage.Store, path string) (*Builder, error) { return nil, storage.ErrContainerUnknown } +// OpenAllBuilders loads all containers which have a state file that we use in +// their data directory, typically so that they can be listed. +func OpenAllBuilders(store storage.Store) (builders []*Builder, err error) { + containers, err := store.Containers() + if err != nil { + return nil, err + } + for _, container := range containers { + cdir, err := store.GetContainerDirectory(container.ID) + if err != nil { + return nil, err + } + buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile)) + if err != nil && os.IsNotExist(err) { + continue + } + b := &Builder{} + err = json.Unmarshal([]byte(buildstate), &b) + if err == nil && b.Type == containerType { + b.store = store + builders = append(builders, b) + } + } + return builders, nil +} + // Save saves the builder's current state to the build container's metadata. // This should not need to be called directly, as other methods of the Builder // object take care of saving their state.