agent: move registerWatches out of the run method

This commit is contained in:
Frank Schroeder 2017-06-02 15:34:49 +02:00 committed by Frank Schröder
parent 5c08953449
commit b3e94082b0

View file

@ -630,6 +630,39 @@ func startupTelemetry(config *Config) error {
return nil return nil
} }
func (cmd *Command) registerWatches(config *Config) error {
var err error
var httpAddr net.Addr
if config.Ports.HTTP != -1 {
httpAddr, err = config.ClientListener(config.Addresses.HTTP, config.Ports.HTTP)
} else if config.Ports.HTTPS != -1 {
httpAddr, err = config.ClientListener(config.Addresses.HTTPS, config.Ports.HTTPS)
} else if len(config.WatchPlans) > 0 {
return fmt.Errorf("Error: cannot use watches if both HTTP and HTTPS are disabled")
}
if err != nil {
cmd.UI.Error(fmt.Sprintf("Failed to determine HTTP address: %v", err))
}
// Register the watches
for _, wp := range config.WatchPlans {
go func(wp *watch.Plan) {
wp.Handler = makeWatchHandler(cmd.logOutput, wp.Exempt["handler"])
wp.LogOutput = cmd.logOutput
addr := httpAddr.String()
// If it's a unix socket, prefix with unix:// so the client initializes correctly
if httpAddr.Network() == "unix" {
addr = "unix://" + addr
}
if err := wp.Run(addr); err != nil {
cmd.UI.Error(fmt.Sprintf("Error running watch: %v", err))
}
}(wp)
}
return nil
}
func (cmd *Command) Run(args []string) int { func (cmd *Command) Run(args []string) int {
cmd.UI = &cli.PrefixedUi{ cmd.UI = &cli.PrefixedUi{
OutputPrefix: "==> ", OutputPrefix: "==> ",
@ -696,35 +729,10 @@ func (cmd *Command) Run(args []string) int {
return 1 return 1
} }
// Get the new client http listener addr if err := cmd.registerWatches(config); err != nil {
var httpAddr net.Addr cmd.UI.Error(err.Error())
if config.Ports.HTTP != -1 {
httpAddr, err = config.ClientListener(config.Addresses.HTTP, config.Ports.HTTP)
} else if config.Ports.HTTPS != -1 {
httpAddr, err = config.ClientListener(config.Addresses.HTTPS, config.Ports.HTTPS)
} else if len(config.WatchPlans) > 0 {
cmd.UI.Error("Error: cannot use watches if both HTTP and HTTPS are disabled")
return 1 return 1
} }
if err != nil {
cmd.UI.Error(fmt.Sprintf("Failed to determine HTTP address: %v", err))
}
// Register the watches
for _, wp := range config.WatchPlans {
go func(wp *watch.Plan) {
wp.Handler = makeWatchHandler(logOutput, wp.Exempt["handler"])
wp.LogOutput = cmd.logOutput
addr := httpAddr.String()
// If it's a unix socket, prefix with unix:// so the client initializes correctly
if httpAddr.Network() == "unix" {
addr = "unix://" + addr
}
if err := wp.Run(addr); err != nil {
cmd.UI.Error(fmt.Sprintf("Error running watch: %v", err))
}
}(wp)
}
// Let the agent know we've finished registration // Let the agent know we've finished registration
agent.StartSync() agent.StartSync()