github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/jujud/agent/util/agent.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package util
     5  
     6  import (
     7  	"github.com/juju/juju/agent"
     8  	"github.com/juju/juju/worker"
     9  	"github.com/juju/juju/worker/dependency"
    10  )
    11  
    12  // Some manifolds just depend on an agent; this type configures them.
    13  type AgentManifoldConfig struct {
    14  	AgentName string
    15  }
    16  
    17  // AgentStartFunc encapsulates the behaviour that varies among AgentManifolds.
    18  type AgentStartFunc func(agent.Agent) (worker.Worker, error)
    19  
    20  // AgentManifold returns a dependency.Manifold that calls the supplied start
    21  // func with the agent resource defined in the config (once it's present).
    22  func AgentManifold(config AgentManifoldConfig, start AgentStartFunc) dependency.Manifold {
    23  	return dependency.Manifold{
    24  		Inputs: []string{
    25  			config.AgentName,
    26  		},
    27  		Start: func(context dependency.Context) (worker.Worker, error) {
    28  			var agent agent.Agent
    29  			if err := context.Get(config.AgentName, &agent); err != nil {
    30  				return nil, err
    31  			}
    32  			return start(agent)
    33  		},
    34  	}
    35  }