<commit_before>package main

import (
	"fmt"
	"github.com/byrnedo/capitan/logger"
	. "github.com/byrnedo/capitan/logger"
	"github.com/codeskyblue/go-sh"
	"github.com/mgutz/str"
	"io/ioutil"
	"os"
	"sort"
	"strings"
	"sync"
"math/rand"
)

const UniqueLabelName = "capitanRunCmd"

var colorList = []string{
	"white",
	"red",
	"green",
	"yellow",
	"blue",
	"magenta",
	"cyan",
}



var nextColorIndex = rand.Intn(len(colorList) - 1)

func getImageId(imageName string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "image", "--format", "{{.Id}}", imageName).Output()
	if err != nil {
		return ""
	}
	imageId := strings.Trim(string(out), " \n")
	return imageId
}

func getContainerIPAddress(name string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "container", "--format", "{{.NetworkSettings.IPAddress}}", name).Output()
	if err != nil {
		return ""
	}
	ip := strings.Trim(string(out), " \n")
	return ip

}

func getContainerUniqueLabel(name string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "container", "--format", "{{.Config.Labels."+UniqueLabelName+"}}", name).Output()
	if err != nil {
		return ""
	}
	label := strings.Trim(string(out), " \n")
	return label

}

func getContainerImageId(name string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "container", "--format", "{{.Image}}", name).Output()
	if err != nil {
		return ""
	}
	imageId := strings.Trim(string(out), " \n")
	return imageId

}

func exists(name string) bool {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--format", "{{.State.Running}}", name).Output()
	if err != nil {
		return false
	}
	if strings.Trim(string(out), " \n") == "<no value>" {
		return false
	}
	return true

}

func isRunning(name string) bool {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--format", "{{.State.Running}}", name).Output()
	if err != nil {
		return false
	}
	if strings.Trim(string(out), " \n") == "true" {
		return true
	}
	return false
}

func runCmd(args ...interface{}) ([]byte, error) {
	ses := sh.NewSession()

	if logger.GetLevel() == DebugLevel {
		ses.ShowCMD = true
	}

	out, err := ses.Command("docker", args...).Output()
	Debug.Println(string(out))
	if err != nil {
		return out, err
	}
	return out, nil
}

func DockerBuild(settings *ProjectSettings, dryRun bool) error {
	sort.Sort(settings.ContainerSettingsList)
	for _, set := range settings.ContainerSettingsList {
		if len(set.Build) == 0 {
			continue
		}
		Info.Println("Building " + set.Name)
		if !dryRun {
			if err := runHook("before.build", &set); err != nil {
				return err
			}
			if _, err := runCmd("build", "--tag", set.Name, set.Build); err != nil {
				return err
			}
			if err := runHook("after.build", &set); err != nil {
				return err
			}
		}

	}
	return nil
}

func DockerUp(settings *ProjectSettings, dryRun bool) error {
	sort.Sort(settings.ContainerSettingsList)

	for _, set := range settings.ContainerSettingsList {

		if !exists(set.Name) {
			if err := runContainer(&set, dryRun); err != nil {
				return err
			}
			continue
		}

		if set.Image != "" {
			conImage := getContainerImageId(set.Name)
			localImage := getImageId(set.Image)
			if conImage != "" && localImage != "" && conImage != localImage {
				// remove and restart
				Info.Println("Removing (different image available):", set.Name)
				if !dryRun {
					if _, err := runCmd("rm", "-f", set.Name); err != nil {
						return err
					}
				}

				if err := runContainer(&set, dryRun); err != nil {
					return err
				}
				continue
			}
			uniqueLabel := fmt.Sprintf("%s", getRunArguments(&set))
			if getContainerUniqueLabel(set.Name) != uniqueLabel {
				// remove and restart
				Info.Println("Removing (run arguments changed):", set.Name)
				if !dryRun {
					if _, err := runCmd("rm", "-f", set.Name); err != nil {
						return err
					}
				}

				if err := runContainer(&set, dryRun); err != nil {
					return err
				}
				continue
			}
		}

		if isRunning(set.Name) {
			Info.Println("Already running:", set.Name)
		} else {
			Info.Println("Starting " + set.Name)
			if err := runHook("before.start", &set); err != nil {
				return err
			}
			if !dryRun {
				if _, err := runCmd("start", set.Name); err != nil {
					return err
				}
			}
			if err := runHook("after.start", &set); err != nil {
				return err
			}
		}
		continue

	}
	return nil
}

func runContainer(set *ContainerSettings, dryRun bool) error {

	Info.Println("Running " + set.Name)
	if dryRun {
		return nil
	}<commit_msg>Fixed hook running when dry run on start command<commit_after>package main

import (
	"fmt"
	"github.com/byrnedo/capitan/logger"
	. "github.com/byrnedo/capitan/logger"
	"github.com/codeskyblue/go-sh"
	"github.com/mgutz/str"
	"io/ioutil"
	"os"
	"sort"
	"strings"
	"sync"
"math/rand"
)

const UniqueLabelName = "capitanRunCmd"

var colorList = []string{
	"white",
	"red",
	"green",
	"yellow",
	"blue",
	"magenta",
	"cyan",
}



var nextColorIndex = rand.Intn(len(colorList) - 1)

func getImageId(imageName string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "image", "--format", "{{.Id}}", imageName).Output()
	if err != nil {
		return ""
	}
	imageId := strings.Trim(string(out), " \n")
	return imageId
}

func getContainerIPAddress(name string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "container", "--format", "{{.NetworkSettings.IPAddress}}", name).Output()
	if err != nil {
		return ""
	}
	ip := strings.Trim(string(out), " \n")
	return ip

}

func getContainerUniqueLabel(name string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "container", "--format", "{{.Config.Labels."+UniqueLabelName+"}}", name).Output()
	if err != nil {
		return ""
	}
	label := strings.Trim(string(out), " \n")
	return label

}

func getContainerImageId(name string) string {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--type", "container", "--format", "{{.Image}}", name).Output()
	if err != nil {
		return ""
	}
	imageId := strings.Trim(string(out), " \n")
	return imageId

}

func exists(name string) bool {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--format", "{{.State.Running}}", name).Output()
	if err != nil {
		return false
	}
	if strings.Trim(string(out), " \n") == "<no value>" {
		return false
	}
	return true

}

func isRunning(name string) bool {
	ses := sh.NewSession()
	ses.Stderr = ioutil.Discard
	out, err := ses.Command("docker", "inspect", "--format", "{{.State.Running}}", name).Output()
	if err != nil {
		return false
	}
	if strings.Trim(string(out), " \n") == "true" {
		return true
	}
	return false
}

func runCmd(args ...interface{}) ([]byte, error) {
	ses := sh.NewSession()

	if logger.GetLevel() == DebugLevel {
		ses.ShowCMD = true
	}

	out, err := ses.Command("docker", args...).Output()
	Debug.Println(string(out))
	if err != nil {
		return out, err
	}
	return out, nil
}

func DockerBuild(settings *ProjectSettings, dryRun bool) error {
	sort.Sort(settings.ContainerSettingsList)
	for _, set := range settings.ContainerSettingsList {
		if len(set.Build) == 0 {
			continue
		}
		Info.Println("Building " + set.Name)
		if !dryRun {
			if err := runHook("before.build", &set); err != nil {
				return err
			}
			if _, err := runCmd("build", "--tag", set.Name, set.Build); err != nil {
				return err
			}
			if err := runHook("after.build", &set); err != nil {
				return err
			}
		}

	}
	return nil
}

func DockerUp(settings *ProjectSettings, dryRun bool) error {
	sort.Sort(settings.ContainerSettingsList)

	for _, set := range settings.ContainerSettingsList {

		if !exists(set.Name) {
			if err := runContainer(&set, dryRun); err != nil {
				return err
			}
			continue
		}

		if set.Image != "" {
			conImage := getContainerImageId(set.Name)
			localImage := getImageId(set.Image)
			if conImage != "" && localImage != "" && conImage != localImage {
				// remove and restart
				Info.Println("Removing (different image available):", set.Name)
				if !dryRun {
					if _, err := runCmd("rm", "-f", set.Name); err != nil {
						return err
					}
				}

				if err := runContainer(&set, dryRun); err != nil {
					return err
				}
				continue
			}
			uniqueLabel := fmt.Sprintf("%s", getRunArguments(&set))
			if getContainerUniqueLabel(set.Name) != uniqueLabel {
				// remove and restart
				Info.Println("Removing (run arguments changed):", set.Name)
				if !dryRun {
					if _, err := runCmd("rm", "-f", set.Name); err != nil {
						return err
					}
				}

				if err := runContainer(&set, dryRun); err != nil {
					return err
				}
				continue
			}
		}

		if isRunning(set.Name) {
			Info.Println("Already running:", set.Name)
		} else {
			Info.Println("Starting " + set.Name)
			if dryRun {
				continue
			}
			if err := runHook("before.start", &set); err != nil {
				return err
			}
			if _, err := runCmd("start", set.Name); err != nil {
				return err
			}
			if err := runHook("after.start", &set); err != nil {
				return err
			}
		}
		continue

	}
	return nil
}

func runContainer(set *ContainerSettings, dryRun bool) error {

	Info.Println("Running " + set.Name)
	if dryRun {
		return nil
	}
{'fraction_non_alphanumeric': 0.17732618114297505, 'fraction_numerical': 0.0004126263668248401, 'mean_word_length': 3.3108048021342817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6287690', 'n_tokens_mistral': 3842, 'n_tokens_neox': 3407, 'n_words': 827}