github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/remotecontext/git.go (about)

     1  package remotecontext // import "github.com/demonoid81/moby/builder/remotecontext"
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/demonoid81/moby/builder"
     7  	"github.com/demonoid81/moby/builder/remotecontext/git"
     8  	"github.com/demonoid81/moby/pkg/archive"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  // MakeGitContext returns a Context from gitURL that is cloned in a temporary directory.
    13  func MakeGitContext(gitURL string) (builder.Source, error) {
    14  	root, err := git.Clone(gitURL)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  
    19  	c, err := archive.Tar(root, archive.Uncompressed)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	defer func() {
    25  		err := c.Close()
    26  		if err != nil {
    27  			logrus.WithField("action", "MakeGitContext").WithField("module", "builder").WithField("url", gitURL).WithError(err).Error("error while closing git context")
    28  		}
    29  		err = os.RemoveAll(root)
    30  		if err != nil {
    31  			logrus.WithField("action", "MakeGitContext").WithField("module", "builder").WithField("url", gitURL).WithError(err).Error("error while removing path and children of root")
    32  		}
    33  	}()
    34  	return FromArchive(c)
    35  }