<commit_before>package cmd

import (
	"fmt"
	"io"
	"net"
	"os"
	"os/exec"
	"path"
	"strings"
	"sync"

	"github.com/aakso/ssh-inscribe/pkg/client"
	"github.com/aakso/ssh-inscribe/pkg/logging"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"golang.org/x/crypto/ssh/agent"
)

var SshCmd = &cobra.Command{
	Use:                "ssh",
	Short:              "Invoke ssh command with signed certificate on ssh-agent",
	DisableFlagParsing: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		if os.Getenv("SSH_AUTH_SOCK") == "" {
			if err := startAdhocAgent(); err != nil {
				return err
			}
			os.Setenv("SSH_AUTH_SOCK", agentSock)
		}
		ClientConfig.GenerateKeypair = true
		c := &client.Client{Config: ClientConfig}
		if err := c.Login(); err != nil {
			return err
		}
		c.Close()
		runSSH(RootCmd.Flags().Args()[2:])
		if agentListener != nil {
			agentListener.Close()
			wg.Wait()
		}
		return nil
	},
}
var agentListener net.Listener
var agentSock = path.Join(os.Getenv("HOME"), ".ssh_inscribe", "adhocagent.sock")
var wg = new(sync.WaitGroup)
var Log = logging.GetLogger("ssh").WithField("pkg", "cmd/ssh")

func startAdhocAgent() error {
	if _, err := os.Stat(agentSock); err == nil {
		if err := os.Remove(agentSock); err != nil {
			return errors.Wrapf(err, "cannot remove existing agent socket: %s", agentSock)
		}
	}
	ln, err := net.Listen("unix", agentSock)
	agentListener = ln
	if err != nil {
		return errors.Wrapf(err, "cannot listen on socket: %s", agentSock)
	}
	go func() {
		keyRing := agent.NewKeyring()
		defer wg.Done()
		for {
			conn, err := agentListener.Accept()
			if err != nil {
				if operr, _ := err.(*net.OpError); operr != nil {
					if operr.Err.Error() == "use of closed network connection" {
						return<commit_msg>ensure program dir exists for the adhoc socket<commit_after>package cmd

import (
	"fmt"
	"io"
	"net"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strings"
	"sync"

	"github.com/aakso/ssh-inscribe/pkg/client"
	"github.com/aakso/ssh-inscribe/pkg/logging"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"golang.org/x/crypto/ssh/agent"
)

var SshCmd = &cobra.Command{
	Use:                "ssh",
	Short:              "Invoke ssh command with signed certificate on ssh-agent",
	DisableFlagParsing: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		if os.Getenv("SSH_AUTH_SOCK") == "" {
			// Ensure agentSock dir exists
			if info, _ := os.Stat(filepath.Dir(agentSock)); info == nil {
				if err := os.MkdirAll(filepath.Dir(agentSock), 0750); err != nil {
					return errors.Wrap(err, "cannot create directory")
				}
			}
			if err := startAdhocAgent(); err != nil {
				return err
			}
			os.Setenv("SSH_AUTH_SOCK", agentSock)
			defer func() {
				agentListener.Close()
				wg.Wait()
			}()
		}
		ClientConfig.GenerateKeypair = true
		c := &client.Client{Config: ClientConfig}
		if err := c.Login(); err != nil {
			return err
		}
		c.Close()
		runSSH(RootCmd.Flags().Args()[2:])
		return nil
	},
}
var agentListener net.Listener
var agentSock = path.Join(os.Getenv("HOME"), ".ssh_inscribe", "adhocagent.sock")
var wg = new(sync.WaitGroup)
var Log = logging.GetLogger("ssh").WithField("pkg", "cmd/ssh")

func startAdhocAgent() error {
	if _, err := os.Stat(agentSock); err == nil {
		if err := os.Remove(agentSock); err != nil {
			return errors.Wrapf(err, "cannot remove existing agent socket: %s", agentSock)
		}
	}
	ln, err := net.Listen("unix", agentSock)
	agentListener = ln
	if err != nil {
		return errors.Wrapf(err, "cannot listen on socket: %s", agentSock)
	}
	go func() {
		keyRing := agent.NewKeyring()
		defer wg.Done()
		for {
			conn, err := agentListener.Accept()
			if err != nil {
				if operr, _ := err.(*net.OpError); operr != nil {
					if operr.Err.Error() == "use of closed network connection" {
						Log.Debug("Shutting down")
						return
{'fraction_non_alphanumeric': 0.16081186572989853, 'fraction_numerical': 0.0026021337496747333, 'mean_word_length': 3.3931428571428572, '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': '2840174', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1406, 'n_words': 333}