<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/michaelsauter/crane/print"
	"gopkg.in/v1/yaml"
	"io/ioutil"
	"os"
	"path/filepath"
)

type Manifest struct {
	Containers Containers          `json:"containers" yaml:"containers"`
	Groups     map[string][]string `json:"groups" yaml:"groups"`
}

func determineTargetedContainers(manifest Manifest, specifiedGroup string) []string {
	// If group is not given, all containers
	if len(specifiedGroup) == 0 {
		var containers []string
		for i := 0; i < len(manifest.Containers); i++ {
			containers = append(containers, manifest.Containers[i].Name)
		}
		return containers
	}
	// Select specified group from listed groups
	for name, containers := range manifest.Groups {
		if name == specifiedGroup {
			return containers
		}
	}
	// Otherwise, the group is just the specified container
	return append([]string{}, specifiedGroup)
}

func getManifest(options Options) Manifest {
	if len(options.config) > 0 {
		return unmarshalJSON([]byte(options.config))
	} else {
		for _, f := range manifestFiles() {
			if _, err := os.Stat(f); err == nil {
				return readCraneData(f)
			}
		}
	}
	panic(StatusError{fmt.Errorf("No configuration found %v", manifestFiles()), 78})
}

func getContainers(options Options) Containers {
	manifest := getManifest(options)
	targetedContainers := determineTargetedContainers(manifest, options.group)
	return manifest.Containers.filter(targetedContainers)
}

func readCraneData(filename string) Manifest {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}

	if filename == "Cranefile" {
		print.Notice("Using a Cranefile is deprecated. Please use crane.json/crane.yaml instead.\n")
	}

	ext := filepath.Ext(filename)
	if ext == ".json" {
		return unmarshalJSON(data)
	} else if ext == ".yml" || ext == ".yaml" {
		return unmarshalYAML(data)
	} else if ext == "" {
		return unmarshalJSON(data)
	} else {
		panic("Unrecognized file extension")
	}
}

// Thanks to https://github.com/markpeek/packer/commit/5bf33a0e91b2318a40c42e9bf855dcc8dd4cdec5
func displaySyntaxError(data []byte, syntaxError error) (err error) {
	syntax, ok := syntaxError.(*json.SyntaxError)<commit_msg>IO_ERR for issues when opening config file<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/michaelsauter/crane/print"
	"gopkg.in/v1/yaml"
	"io/ioutil"
	"os"
	"path/filepath"
)

type Manifest struct {
	Containers Containers          `json:"containers" yaml:"containers"`
	Groups     map[string][]string `json:"groups" yaml:"groups"`
}

func determineTargetedContainers(manifest Manifest, specifiedGroup string) []string {
	// If group is not given, all containers
	if len(specifiedGroup) == 0 {
		var containers []string
		for i := 0; i < len(manifest.Containers); i++ {
			containers = append(containers, manifest.Containers[i].Name)
		}
		return containers
	}
	// Select specified group from listed groups
	for name, containers := range manifest.Groups {
		if name == specifiedGroup {
			return containers
		}
	}
	// Otherwise, the group is just the specified container
	return append([]string{}, specifiedGroup)
}

func getManifest(options Options) Manifest {
	if len(options.config) > 0 {
		return unmarshalJSON([]byte(options.config))
	} else {
		for _, f := range manifestFiles() {
			if _, err := os.Stat(f); err == nil {
				return readCraneData(f)
			}
		}
	}
	panic(StatusError{fmt.Errorf("No configuration found %v", manifestFiles()), 78})
}

func getContainers(options Options) Containers {
	manifest := getManifest(options)
	targetedContainers := determineTargetedContainers(manifest, options.group)
	return manifest.Containers.filter(targetedContainers)
}

func readCraneData(filename string) Manifest {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(StatusError{err, 74})
	}

	if filename == "Cranefile" {
		print.Notice("Using a Cranefile is deprecated. Please use crane.json/crane.yaml instead.\n")
	}

	ext := filepath.Ext(filename)
	if ext == ".json" {
		return unmarshalJSON(data)
	} else if ext == ".yml" || ext == ".yaml" {
		return unmarshalYAML(data)
	} else if ext == "" {
		return unmarshalJSON(data)
	} else {
		panic("Unrecognized file extension")
	}
}

// Thanks to https://github.com/markpeek/packer/commit/5bf33a0e91b2318a40c42e9bf855dcc8dd4cdec5
func displaySyntaxError(data []byte, syntaxError error) (err error) {
	syntax, ok := syntaxError.(*json.SyntaxError)
{'fraction_non_alphanumeric': 0.12983798379837982, 'fraction_numerical': 0.012601260126012601, 'mean_word_length': 4.487654320987654, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '3970169', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1462, 'n_words': 360}