github.com/dnephin/dobi@v0.15.0/tasks/context/execcontext.go (about)

     1  package context
     2  
     3  import (
     4  	"github.com/dnephin/dobi/config"
     5  	"github.com/dnephin/dobi/execenv"
     6  	"github.com/dnephin/dobi/logging"
     7  	"github.com/dnephin/dobi/tasks/client"
     8  	"github.com/dnephin/dobi/tasks/task"
     9  	docker "github.com/fsouza/go-dockerclient"
    10  )
    11  
    12  // ExecuteContext contains all the context for task execution
    13  type ExecuteContext struct {
    14  	modified    map[string]bool
    15  	Resources   *ResourceCollection
    16  	Client      client.DockerClient
    17  	authConfigs *docker.AuthConfigurations
    18  	WorkingDir  string
    19  	ConfigFile  string
    20  	Env         *execenv.ExecEnv
    21  	Settings    Settings
    22  }
    23  
    24  // IsModified returns true if any of the tasks named in names has been modified
    25  // during this execution
    26  func (ctx *ExecuteContext) IsModified(names ...task.Name) bool {
    27  	for _, name := range names {
    28  		if modified := ctx.modified[name.MapKey()]; modified {
    29  			return true
    30  		}
    31  	}
    32  	return false
    33  }
    34  
    35  // SetModified sets the task name as modified
    36  func (ctx *ExecuteContext) SetModified(name task.Name) {
    37  	// Add both the key and the string name so that it matches against
    38  	// dependencies specified with or without an action
    39  	ctx.modified[name.MapKey()] = true
    40  	ctx.modified[name.Name()] = true
    41  }
    42  
    43  // GetAuthConfig returns the auth configuration for the repo
    44  func (ctx *ExecuteContext) GetAuthConfig(repo string) docker.AuthConfiguration {
    45  	if ctx.authConfigs == nil {
    46  		return docker.AuthConfiguration{}
    47  	}
    48  	auth, ok := ctx.authConfigs.Configs[repo]
    49  	if !ok {
    50  		logging.Log.Warnf("Missing auth config for %q", repo)
    51  	}
    52  	return auth
    53  }
    54  
    55  // GetAuthConfigs returns all the authorization configs in the config file. This
    56  // is used by build, because the repo isn't known until after the Dockerfile is
    57  // parsed.
    58  func (ctx *ExecuteContext) GetAuthConfigs() docker.AuthConfigurations {
    59  	if ctx.authConfigs == nil {
    60  		return docker.AuthConfigurations{}
    61  	}
    62  	return *ctx.authConfigs
    63  }
    64  
    65  // NewExecuteContext craetes a new empty ExecuteContext
    66  func NewExecuteContext(
    67  	config *config.Config,
    68  	client client.DockerClient,
    69  	execEnv *execenv.ExecEnv,
    70  	settings Settings,
    71  ) *ExecuteContext {
    72  	authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
    73  	if err != nil {
    74  		logging.Log.Warnf("Failed to load auth config: %s", err)
    75  	}
    76  
    77  	return &ExecuteContext{
    78  		modified:    make(map[string]bool),
    79  		Resources:   newResourceCollection(),
    80  		WorkingDir:  config.WorkingDir,
    81  		Client:      client,
    82  		authConfigs: authConfigs,
    83  		ConfigFile:  config.FilePath,
    84  		Env:         execEnv,
    85  		Settings:    settings,
    86  	}
    87  }