<commit_before>package cli

import (
	"github.com/drewsonne/go-gocd/gocd"
	"github.com/urfave/cli"
	"gopkg.in/AlecAivazis/survey.v1"
	"gopkg.in/yaml.v2"
	"io/ioutil"
)

// List of command name and descriptions
const (
	ConfigureCommandName  = "configure"
	ConfigureCommandUsage = "Generate configuration file ~/.gocd.conf"
)

func configureAction(c *cli.Context) (err error) {
	var cfg *gocd.Configuration
	var profile string

	if profile = c.Parent().String("profile"); profile == "" {
		profile = "default"
	}

	cfgs, err := gocd.LoadConfigFromFile()

	if cfg, err = generateConfig(); err != nil {
		return handleErrOutput("Configure:generate", err)
	} else {
		cfgs[profile] = cfg
	}

	b, err := yaml.Marshal(cfgs)
	if err != nil {
		return handleErrOutput("Configure:yaml", err)
	}

	if err = ioutil.WriteFile(gocd.ConfigFilePath(), b, 0644); err != nil {
		return handleErrOutput("Configure:write", err)
	}

	return nil
}

// Build a default template
func generateConfig() (cfg *gocd.Configuration, err error) {
	cfg = &gocd.Configuration{}
	qs := []*survey.Question{
		{
			Name:     "server",
			Prompt:   &survey.Input{Message: "GoCD Server (should contain '/go/' suffix)"},
			Validate: survey.Required,
		},<commit_msg>Added better error handling when configuring<commit_after>package cli

import (
	"github.com/drewsonne/go-gocd/gocd"
	"github.com/urfave/cli"
	"gopkg.in/AlecAivazis/survey.v1"
	"gopkg.in/yaml.v2"
	"io/ioutil"
)

// List of command name and descriptions
const (
	ConfigureCommandName  = "configure"
	ConfigureCommandUsage = "Generate configuration file ~/.gocd.conf"
)

func configureAction(c *cli.Context) (err error) {
	var cfg *gocd.Configuration
	var profile string

	if profile = c.Parent().String("profile"); profile == "" {
		profile = "default"
	}

	cfgs, err := gocd.LoadConfigFromFile()
	if err != nil {
		return handleErrOutput("Configure:generate", err)
	}

	if cfg, err = generateConfig(); err != nil {
		return handleErrOutput("Configure:generate", err)
	} else {
		cfgs[profile] = cfg
	}

	b, err := yaml.Marshal(cfgs)
	if err != nil {
		return handleErrOutput("Configure:yaml", err)
	}

	if err = ioutil.WriteFile(gocd.ConfigFilePath(), b, 0644); err != nil {
		return handleErrOutput("Configure:write", err)
	}

	return nil
}

// Build a default template
func generateConfig() (cfg *gocd.Configuration, err error) {
	cfg = &gocd.Configuration{}
	qs := []*survey.Question{
		{
			Name:     "server",
			Prompt:   &survey.Input{Message: "GoCD Server (should contain '/go/' suffix)"},
			Validate: survey.Required,
		},
{'fraction_non_alphanumeric': 0.14681764935572042, 'fraction_numerical': 0.004685669660288949, 'mean_word_length': 4.175757575757576, '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': '1497211', 'n_tokens_mistral': 970, 'n_tokens_neox': 918, 'n_words': 213}