<commit_before>package main

import (
	"fmt"
	"os"
	"path"
	"reflect"
	"regexp"
	"strings"

	"github.com/howeyc/gopass"

	"github.com/ovh/cds/cli"
	"github.com/ovh/cds/sdk"
	"github.com/ovh/cds/sdk/cdsclient"
)

var loginCmd = cli.Command{
	Name:  "login",
	Short: "Login to CDS",
	Flags: []cli.Flag{
		{
			Name:      "api-url",
			ShortHand: "H",
			Usage:     "CDS API URL",
			IsValid: func(s string) bool {
				match, _ := regexp.MatchString(`http[s]?:\/\/(.*)`, s)
				return match
			},
			Kind: reflect.String,
		}, {
			Name:      "username",
			ShortHand: "u",
			Usage:     "CDS Username",
			Kind:      reflect.String,
		}, {
			Name:      "password",
			ShortHand: "p",
			Usage:     "CDS Password",
			Kind:      reflect.String,
		}, {
			Name:  "env",
			Usage: "Display the commands to set up the environment for the cds client",
			Kind:  reflect.Bool,
		},
	},
}

func loginRun(v cli.Values) error {
	url := v.GetString("api-url")
	username := v.GetString("username")
	password := v.GetString("password")
	env := v.GetBool("env")

	if env &&
		(url == "" || username == "" || password == "") {
		return fmt.Errorf("Please set flags to use --env option")
	}

	if !env {
		fmt.Println("CDS API URL:", url)
	}

	//Take the user from flags or ask for on command line
	if username == "" {
		fmt.Printf("Username: ")
		username = cli.ReadLine()
	} else if !env {
		fmt.Println("Username:", username)
	}

	//Take the password from flags or ask for on command line
	if password == "" {
		//Ask for the password
		fmt.Printf("Password: ")
		b, err := gopass.GetPasswd()
		password = string(b)
		if err != nil {
			cli.ExitOnError(err)
		}
	} else if !env {
		fmt.Println("Password: ********")
	}

	return doLogin(url, username, password, env)
}

func doLogin(url, username, password string, env bool) error {
	conf := cdsclient.Config{
		Host:    url,
		Verbose: os.Getenv("CDS_VERBOSE") == "true",
	}

	client = cdsclient.New(conf)
	ok, token, err := client.UserLogin(username, password)
	if err != nil {
		if conf.Verbose {
			fmt.Fprintf(os.Stderr, "error:%s\n", err)
		}
		if strings.HasSuffix(url, "/") {
			fmt.Fprintf(os.Stderr, "Invalid URL. Remove trailing '/'\n")
		}
		return fmt.Errorf("Please check CDS API URL")
	}
	if !ok {
		return fmt.Errorf("login failed")
	}

	if env && sdk.GOOS == "windows" {
		fmt.Println("env option is not supported on windows yet")
		os.Exit(1)
	}

	if env {
		fmt.Printf("export CDS_API_URL=%s\n", url)
		fmt.Printf("export CDS_USER=%s\n", username)
		fmt.Printf("export CDS_TOKEN=%s\n", token)
		fmt.Println("# Run this command to configure your shell:")
		fmt.Println(`# eval $(cds login -H API_URL -u USERNAME -p PASSWORD --env)`)
		return nil
	}

	if configFile == "" {
		homedir := userHomeDir()
		configFile = path.Join(homedir, ".cdsrc")
		fmt.Printf("You didn't specify config file location; %s will be used.\n", configFile)
	}

	var errfi error
	var fi *os.File

	//Check if file exists
	if _, err := os.Stat(configFile); err == nil {
		if !cli.AskForConfirmation(fmt.Sprintf("File %s exists, do you want to overwrite?", configFile)) {
			return fmt.Errorf("aborted")
		}
		fi, errfi = os.OpenFile(configFile, os.O_RDWR, os.FileMode(0600))
	} else {
		fi, errfi = os.Create(configFile)
	}

	if errfi != nil {
		return errfi
	}

	tomlConf := config{
		Host: url,
		InsecureSkipVerifyTLS: insecureSkipVerifyTLS,
		User:  username,
		Token: token,
	}

	defer fi.Close()

	if err := storeSecret(fi, &tomlConf); err != nil {
		return err
	}

	fmt.Println("Login successful")
	return nil
}<commit_msg>fix(cdsctl): if config file exists, delete it before create (#3409)<commit_after>package main

import (
	"fmt"
	"os"
	"path"
	"reflect"
	"regexp"
	"strings"

	"github.com/howeyc/gopass"

	"github.com/ovh/cds/cli"
	"github.com/ovh/cds/sdk"
	"github.com/ovh/cds/sdk/cdsclient"
)

var loginCmd = cli.Command{
	Name:  "login",
	Short: "Login to CDS",
	Flags: []cli.Flag{
		{
			Name:      "api-url",
			ShortHand: "H",
			Usage:     "CDS API URL",
			IsValid: func(s string) bool {
				match, _ := regexp.MatchString(`http[s]?:\/\/(.*)`, s)
				return match
			},
			Kind: reflect.String,
		}, {
			Name:      "username",
			ShortHand: "u",
			Usage:     "CDS Username",
			Kind:      reflect.String,
		}, {
			Name:      "password",
			ShortHand: "p",
			Usage:     "CDS Password",
			Kind:      reflect.String,
		}, {
			Name:  "env",
			Usage: "Display the commands to set up the environment for the cds client",
			Kind:  reflect.Bool,
		},
	},
}

func loginRun(v cli.Values) error {
	url := v.GetString("api-url")
	username := v.GetString("username")
	password := v.GetString("password")
	env := v.GetBool("env")

	if env &&
		(url == "" || username == "" || password == "") {
		return fmt.Errorf("Please set flags to use --env option")
	}

	if !env {
		fmt.Println("CDS API URL:", url)
	}

	//Take the user from flags or ask for on command line
	if username == "" {
		fmt.Printf("Username: ")
		username = cli.ReadLine()
	} else if !env {
		fmt.Println("Username:", username)
	}

	//Take the password from flags or ask for on command line
	if password == "" {
		//Ask for the password
		fmt.Printf("Password: ")
		b, err := gopass.GetPasswd()
		password = string(b)
		if err != nil {
			cli.ExitOnError(err)
		}
	} else if !env {
		fmt.Println("Password: ********")
	}

	return doLogin(url, username, password, env)
}

func doLogin(url, username, password string, env bool) error {
	conf := cdsclient.Config{
		Host:    url,
		Verbose: os.Getenv("CDS_VERBOSE") == "true",
	}

	client = cdsclient.New(conf)
	ok, token, err := client.UserLogin(username, password)
	if err != nil {
		if conf.Verbose {
			fmt.Fprintf(os.Stderr, "error:%s\n", err)
		}
		if strings.HasSuffix(url, "/") {
			fmt.Fprintf(os.Stderr, "Invalid URL. Remove trailing '/'\n")
		}
		return fmt.Errorf("Please check CDS API URL")
	}
	if !ok {
		return fmt.Errorf("login failed")
	}

	if env && sdk.GOOS == "windows" {
		fmt.Println("env option is not supported on windows yet")
		os.Exit(1)
	}

	if env {
		fmt.Printf("export CDS_API_URL=%s\n", url)
		fmt.Printf("export CDS_USER=%s\n", username)
		fmt.Printf("export CDS_TOKEN=%s\n", token)
		fmt.Println("# Run this command to configure your shell:")
		fmt.Println(`# eval $(cds login -H API_URL -u USERNAME -p PASSWORD --env)`)
		return nil
	}

	if configFile == "" {
		homedir := userHomeDir()
		configFile = path.Join(homedir, ".cdsrc")
		fmt.Printf("You didn't specify config file location; %s will be used.\n", configFile)
	}

	var errfi error
	var fi *os.File

	//Check if file exists
	if _, err := os.Stat(configFile); err == nil {
		if !cli.AskForConfirmation(fmt.Sprintf("File %s exists, do you want to overwrite?", configFile)) {
			return fmt.Errorf("aborted")
		}
		if errre := os.Remove(configFile); errre != nil {
			return fmt.Errorf("Error while removing old file %s: %s", configFile, errre)
		}
	}

	fi, errfi = os.Create(configFile)
	if errfi != nil {
		return fmt.Errorf("Error while creating file %s: %s", configFile, errfi)
	}

	tomlConf := config{
		Host: url,
		InsecureSkipVerifyTLS: insecureSkipVerifyTLS,
		User:  username,
		Token: token,
	}

	defer fi.Close()

	if err := storeSecret(fi, &tomlConf); err != nil {
		return err
	}

	if errm := fi.Chmod(os.FileMode(0600)); errm != nil {
		return fmt.Errorf("Error while chmod 600 file %s: %s", configFile, errm)
	}

	fmt.Println("Login successful")
	return nil
}
{'fraction_non_alphanumeric': 0.16381286234326548, 'fraction_numerical': 0.002292031818794661, 'mean_word_length': 3.260769672601953, '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': '2441575', 'n_tokens_mistral': 3003, 'n_tokens_neox': 2770, 'n_words': 726}