<commit_before>package cmd

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"github.com/exitshell/konnect/engine"
	"github.com/exitshell/konnect/proxy"
	"github.com/exitshell/konnect/task"
	"github.com/fatih/color"
	"github.com/spf13/cobra"
)

// handleErr is a function that logs Fatal
// if the given error `err` is populated.
func handleErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

// This function returns a slice of
// possible default config filenames.
func getDefaultConfigs() []string {
	return []string{
		"./konnect.yml",
		"../konnect.yml",
	}
}

// Remove duplicate elements from a string slice.
// https://goo.gl/ttDAg2
func removeDuplicates(elements []string) []string {
	encountered := map[string]bool{}
	result := []string{}
	for _, host := range elements {
		if encountered[host] == false {
			encountered[host] = true
			result = append(result, host)
		}
	}
	return result
}

// Get the config filename from cmd flags.
// Fallback to default filenames.
// Validate that the file exists.
func resolveFilename(cmd *cobra.Command) (string, error) {
	// Get config filename from flags.
	filename, _ := cmd.Flags().GetString("filename")
	filenames := []string{filename}

	// If filename is not specified, then get
	// a list of possible config filenames.
	if filename == "" {
		filenames = getDefaultConfigs()
	}

	for _, fName := range filenames {
		// Check if the filename exists.
		if _, err := os.Stat(fName); err == nil {
			// Filename was found. Immediately return.
			return fName, nil
		}
	}

	// At this point, none of the possible filenames
	// were found. Return an error.
	return "", errConfigNotFound
}

func makeDefaultConfig(filename string) error {
	// Make default proxylist.
	proxyList := map[string]*proxy.SSHProxy{
		"app": &proxy.SSHProxy{
			User: "root",
			Host: "127.0.0.1",
			Port: 22,
			Key:  "/home/app/key",
		},
		"database": &proxy.SSHProxy{
			User: "admin",
			Host: "192.168.99.100",
			Port: 89,
			Key:  "~/.ssh/id_rsa",
		},
	}
	// Make default tasklist.
	taskList := map[string]*task.SSHTask{
		"ping": &task.SSHTask{
			Command: "echo ping",
		},
		"tailsys": &task.SSHTask{
			Command: "tail -f -n 100 /var/log/syslog",
		},
	}

	// Init engine, and assign structs.
	konnect := engine.New()
	konnect.Hosts = proxyList
	konnect.Tasks = taskList

	// Marshal the konnect hosts.
	hostsByteSlice, err := konnect.MarshalHosts()
	if err != nil {
		return err
	}

	// Marshal the konnect tasks.
	tasksByteSlice, err := konnect.MarshalTasks()
	if err != nil {
		return err
	}

	// Build the byte slice to export.
	data := []byte{}
	data = append(data, []byte(`# This is an autogenerated configuration file for konnect.`)...)
	data = append(data, 10, 10)

	// Append hosts.
	data = append(data, []byte(`# Define your hosts here.`)...)
	data = append(data, 10)
	data = append(data, hostsByteSlice...)
	data = append(data, 10, 10)

	// Append tasks.<commit_msg>Modify config file generation comments.<commit_after>package cmd

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"github.com/exitshell/konnect/engine"
	"github.com/exitshell/konnect/proxy"
	"github.com/exitshell/konnect/task"
	"github.com/fatih/color"
	"github.com/spf13/cobra"
)

// handleErr is a function that logs Fatal
// if the given error `err` is populated.
func handleErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

// This function returns a slice of
// possible default config filenames.
func getDefaultConfigs() []string {
	return []string{
		"./konnect.yml",
		"../konnect.yml",
	}
}

// Remove duplicate elements from a string slice.
// https://goo.gl/ttDAg2
func removeDuplicates(elements []string) []string {
	encountered := map[string]bool{}
	result := []string{}
	for _, host := range elements {
		if encountered[host] == false {
			encountered[host] = true
			result = append(result, host)
		}
	}
	return result
}

// Get the config filename from cmd flags.
// Fallback to default filenames.
// Validate that the file exists.
func resolveFilename(cmd *cobra.Command) (string, error) {
	// Get config filename from flags.
	filename, _ := cmd.Flags().GetString("filename")
	filenames := []string{filename}

	// If filename is not specified, then get
	// a list of possible config filenames.
	if filename == "" {
		filenames = getDefaultConfigs()
	}

	for _, fName := range filenames {
		// Check if the filename exists.
		if _, err := os.Stat(fName); err == nil {
			// Filename was found. Immediately return.
			return fName, nil
		}
	}

	// At this point, none of the possible filenames
	// were found. Return an error.
	return "", errConfigNotFound
}

func makeDefaultConfig(filename string) error {
	// Make default proxylist.
	proxyList := map[string]*proxy.SSHProxy{
		"app": &proxy.SSHProxy{
			User: "root",
			Host: "127.0.0.1",
			Port: 22,
			Key:  "/home/app/key",
		},
		"database": &proxy.SSHProxy{
			User: "admin",
			Host: "192.168.99.100",
			Port: 89,
			Key:  "~/.ssh/id_rsa",
		},
	}
	// Make default tasklist.
	taskList := map[string]*task.SSHTask{
		"ping": &task.SSHTask{
			Command: "echo ping",
		},
		"tailsys": &task.SSHTask{
			Command: "tail -f -n 100 /var/log/syslog",
		},
	}

	// Init engine, and assign structs.
	konnect := engine.New()
	konnect.Hosts = proxyList
	konnect.Tasks = taskList

	// Marshal the konnect hosts.
	hostsByteSlice, err := konnect.MarshalHosts()
	if err != nil {
		return err
	}

	// Marshal the konnect tasks.
	tasksByteSlice, err := konnect.MarshalTasks()
	if err != nil {
		return err
	}

	// Build the byte slice to export.
	data := []byte{}
	data = append(data, []byte(`# Configuration file for konnect.`)...)
	data = append(data, 10, 10)

	// Append hosts.
	data = append(data, []byte(`# Define your hosts here.`)...)
	data = append(data, 10)
	data = append(data, hostsByteSlice...)
	data = append(data, 10, 10)

	// Append tasks.
{'fraction_non_alphanumeric': 0.1507460126907906, 'fraction_numerical': 0.012690790601955068, 'mean_word_length': 3.269399707174231, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7339055', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2076, 'n_words': 579}