<commit_before>package base

import (
	"fmt"
	"regexp"

	"github.com/scrapli/scrapligo/logging"

	"github.com/scrapli/scrapligo/channel"
	"github.com/scrapli/scrapligo/transport"
)

// PrivilegeLevel struct defining a single privilege level -- used only for "network" level drivers.
type PrivilegeLevel struct {
	Pattern            string
	PatternRe          *regexp.Regexp
	PatternNotContains []string
	Name               string
	PreviousPriv       string
	Deescalate         string
	Escalate           string
	EscalateAuth       bool
	EscalatePrompt     string
}

// Driver primary/base driver struct.
type Driver struct {
	Host string

	AuthUsername             string
	AuthPassword             string
	AuthSecondary            string
	AuthPrivateKey           string
	AuthPrivateKeyPassphrase string
	AuthStrictKey            bool
	AuthBypass               bool

	SSHConfigFile     string
	SSHKnownHostsFile string

	TransportType string
	Transport     *transport.Transport

	Channel *channel.Channel

	FailedWhenContains []string

	PrivilegeLevels    map[string]*PrivilegeLevel
	DefaultDesiredPriv string

	NetconfEcho *bool
}

// Open opens the connection.
func (d *Driver) Open() error {
	logging.LogDebug(
		d.FormatLogMessage(
			"info",
			fmt.Sprintf(
				"opening connection to '%s' on port '%d'",
				d.Host,
				d.Transport.BaseTransportArgs.Port,
			),
		),
	)

	err := d.Transport.Open()
	if err != nil {
		return err
	}

	var authErr error
	if d.TransportType == transport.SystemTransportName && !d.AuthBypass {
		_, authErr = d.Channel.AuthenticateSSH(d.AuthPassword, d.AuthPrivateKeyPassphrase)
	} else if d.TransportType == transport.TelnetTransportName && !d.AuthBypass {
		_, authErr = d.Channel.AuthenticateTelnet(d.AuthUsername, d.AuthPassword)
	}

	if authErr != nil {
		logging.LogError(
			d.FormatLogMessage("error", "authentication failed, connection not opened"),
		)

		return err
	}

	logging.LogDebug(d.FormatLogMessage("info", "connection to device opened successfully"))

	return nil
}

// Close closes the connection.
func (d *Driver) Close() error {
	logging.LogDebug(
		d.FormatLogMessage(
			"info",
			fmt.Sprintf(
				"closing connection to '%s' on port '%d'",
				d.Host,
				d.Transport.BaseTransportArgs.Port,<commit_msg>fix returning wrong error in driver open<commit_after>package base

import (
	"fmt"
	"regexp"

	"github.com/scrapli/scrapligo/logging"

	"github.com/scrapli/scrapligo/channel"
	"github.com/scrapli/scrapligo/transport"
)

// PrivilegeLevel struct defining a single privilege level -- used only for "network" level drivers.
type PrivilegeLevel struct {
	Pattern            string
	PatternRe          *regexp.Regexp
	PatternNotContains []string
	Name               string
	PreviousPriv       string
	Deescalate         string
	Escalate           string
	EscalateAuth       bool
	EscalatePrompt     string
}

// Driver primary/base driver struct.
type Driver struct {
	Host string

	AuthUsername             string
	AuthPassword             string
	AuthSecondary            string
	AuthPrivateKey           string
	AuthPrivateKeyPassphrase string
	AuthStrictKey            bool
	AuthBypass               bool

	SSHConfigFile     string
	SSHKnownHostsFile string

	TransportType string
	Transport     *transport.Transport

	Channel *channel.Channel

	FailedWhenContains []string

	PrivilegeLevels    map[string]*PrivilegeLevel
	DefaultDesiredPriv string

	NetconfEcho *bool
}

// Open opens the connection.
func (d *Driver) Open() error {
	logging.LogDebug(
		d.FormatLogMessage(
			"info",
			fmt.Sprintf(
				"opening connection to '%s' on port '%d'",
				d.Host,
				d.Transport.BaseTransportArgs.Port,
			),
		),
	)

	err := d.Transport.Open()
	if err != nil {
		return err
	}

	var authErr error
	if d.TransportType == transport.SystemTransportName && !d.AuthBypass {
		_, authErr = d.Channel.AuthenticateSSH(d.AuthPassword, d.AuthPrivateKeyPassphrase)
	} else if d.TransportType == transport.TelnetTransportName && !d.AuthBypass {
		_, authErr = d.Channel.AuthenticateTelnet(d.AuthUsername, d.AuthPassword)
	}

	if authErr != nil {
		logging.LogError(
			d.FormatLogMessage("error", "authentication failed, connection not opened"),
		)

		return authErr
	}

	logging.LogDebug(d.FormatLogMessage("info", "connection to device opened successfully"))

	return nil
}

// Close closes the connection.
func (d *Driver) Close() error {
	logging.LogDebug(
		d.FormatLogMessage(
			"info",
			fmt.Sprintf(
				"closing connection to '%s' on port '%d'",
				d.Host,
				d.Transport.BaseTransportArgs.Port,
{'fraction_non_alphanumeric': 0.08945406709055032, 'fraction_numerical': 0.0, 'mean_word_length': 3.1510464058234757, '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': '5274401', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1407, 'n_words': 349}