<commit_before>package services

import (
	"fmt"
	"strings"

	"github.com/golang/glog"
	"github.com/namely/broadway/deployment"
	"github.com/namely/broadway/env"
	"github.com/namely/broadway/store"
)

// SlackCommand represents a user command that came in from Slack
type SlackCommand interface {
	Execute() (string, error)
}

type deployCommand struct {
	pID string
	ID  string
	is  *InstanceService
}

func (c *deployCommand) Execute() (string, error) {
	// todo: Load these from deployment package like playbooks
	ms := NewManifestService(env.ManifestsPath)
	AllManifests, err := ms.LoadManifestFolder()
	if err != nil {
		glog.Error(err)
	}

	ds := NewDeploymentService(store.New(), deployment.AllPlaybooks, AllManifests)

	i, err := c.is.Show(c.pID, c.ID)
	if err != nil {
		msg := fmt.Sprintf("Failed to deploy instance %s/%s: Instance not found", i.PlaybookID, i.ID)
		glog.Error(msg)
		return msg, err
	}

	go func() {
		glog.Infof("Asynchronously deploying %s/%s...", i.PlaybookID, i.ID)
		err := ds.DeployAndNotify(i)
		if err != nil {
			glog.Errorf("Slack command failed to deploy instance %s/%s:\n%s\n", i.PlaybookID, i.ID, err)
			return
		}
		glog.Infof("Slack command successfully deployed instance %s/%s", i.PlaybookID, i.ID)
		return
	}()

	return fmt.Sprintf("Started deployment of %s/%s", i.PlaybookID, i.ID), nil
}

// InvalidSetVar error presentation for invalid setvar syntax
type InvalidSetVar struct{}

func (e *InvalidSetVar) Error() string {
	return "Syntax error, example: setvar playbook1 instance10 var1=value"
}

// InvalidDeploy represents an error error for invalid deploy syntax
type InvalidDeploy struct{}
<commit_msg>Avoid using instance properties when we fail to get the instance (#55)<commit_after>package services

import (
	"fmt"
	"strings"

	"github.com/golang/glog"
	"github.com/namely/broadway/deployment"
	"github.com/namely/broadway/env"
	"github.com/namely/broadway/store"
)

// SlackCommand represents a user command that came in from Slack
type SlackCommand interface {
	Execute() (string, error)
}

type deployCommand struct {
	pID string
	ID  string
	is  *InstanceService
}

func (c *deployCommand) Execute() (string, error) {
	// todo: Load these from deployment package like playbooks
	ms := NewManifestService(env.ManifestsPath)
	AllManifests, err := ms.LoadManifestFolder()
	if err != nil {
		glog.Error(err)
	}

	ds := NewDeploymentService(store.New(), deployment.AllPlaybooks, AllManifests)

	i, err := c.is.Show(c.pID, c.ID)
	if err != nil {
		msg := fmt.Sprintf("Failed to deploy instance %s/%s: Instance not found", c.pID, c.ID)
		glog.Error(msg)
		return msg, err
	}

	go func() {
		glog.Infof("Asynchronously deploying %s/%s...", i.PlaybookID, i.ID)
		err := ds.DeployAndNotify(i)
		if err != nil {
			glog.Errorf("Slack command failed to deploy instance %s/%s:\n%s\n", i.PlaybookID, i.ID, err)
			return
		}
		glog.Infof("Slack command successfully deployed instance %s/%s", i.PlaybookID, i.ID)
		return
	}()

	return fmt.Sprintf("Started deployment of %s/%s", i.PlaybookID, i.ID), nil
}

// InvalidSetVar error presentation for invalid setvar syntax
type InvalidSetVar struct{}

func (e *InvalidSetVar) Error() string {
	return "Syntax error, example: setvar playbook1 instance10 var1=value"
}

// InvalidDeploy represents an error error for invalid deploy syntax
type InvalidDeploy struct{}

{'fraction_non_alphanumeric': 0.1260429082240763, 'fraction_numerical': 0.0029797377830750892, 'mean_word_length': 4.294952681388013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4892647', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1144, 'n_words': 327}