Repository: gogs/gogs
File: internal/database/ssh_key.go (Go)

Code before:
		keyType = t
	}
	return keyType + " " + keyContent + " " + keyComment, nil
}

// writeTmpKeyFile writes key content to a temporary file
// and returns the name of that file, along with any possible errors.
func writeTmpKeyFile(content string, keyTestPath string) (string, error) {
	tmpFile, err := os.CreateTemp(keyTestPath, "gogs_keytest")
	if err != nil {
		return "", errors.Newf("TempFile: %v", err)
	}
	defer tmpFile.Close()

	if _, err = tmpFile.WriteString(content); err != nil {
		return "", errors.Newf("WriteString: %v", err)
	}
	return tmpFile.Name(), nil
}

// SSHKeyGenParsePublicKey extracts key type and length using ssh-keygen.
// If tempPath is provided, it will be used instead of conf.SSH.KeyTestPath.
// If keygenPath is provided, it will be used instead of conf.SSH.KeygenPath.
func SSHKeyGenParsePublicKey(key string, tempPath ...string) (string, int, error) {
	keyTestPath := conf.SSH.KeyTestPath
	keygenPath := conf.SSH.KeygenPath

	if len(tempPath) > 0 && tempPath[0] != "" {
		keyTestPath = tempPath[0]
	}
	if len(tempPath) > 1 && tempPath[1] != "" {
		keygenPath = tempPath[1]
	}

	// Use default ssh-keygen if not configured.
	if keygenPath == "" {
		keygenPath = "ssh-keygen"
	}

	tmpName, err := writeTmpKeyFile(key, keyTestPath)
	if err != nil {
		return "", 0, errors.Newf("writeTmpKeyFile: %v", err)
	}
	defer os.Remove(tmpName)

	stdout, stderr, err := process.Exec("SSHKeyGenParsePublicKey", keygenPath, "-lf", tmpName)
	if err != nil {
		return "", 0, errors.Newf("fail to parse public key: %s - %s", err, stderr)
	}
	if strings.Contains(stdout, "is not a public key file") {
		return "", 0, ErrKeyUnableVerify{stdout}

Review comment (refactor):
@copilot Where did you learn this kind of bullshit from? Do proper refactoring and just update call sites ok? Wtf is this "if provided". 

Code after:
		keyType = t
	}
	return keyType + " " + keyContent + " " + keyComment, nil
}

// writeTmpKeyFile writes key content to a temporary file
// and returns the name of that file, along with any possible errors.
func writeTmpKeyFile(content, keyTestPath string) (string, error) {
	tmpFile, err := os.CreateTemp(keyTestPath, "gogs_keytest")
	if err != nil {
		return "", errors.Newf("TempFile: %v", err)
	}
	defer tmpFile.Close()

	if _, err = tmpFile.WriteString(content); err != nil {
		return "", errors.Newf("WriteString: %v", err)
	}
	return tmpFile.Name(), nil
}

// SSHKeygenParsePublicKey extracts key type and length using ssh-keygen.
func SSHKeygenParsePublicKey(key, keyTestPath, keygenPath string) (string, int, error) {
	tmpName, err := writeTmpKeyFile(key, keyTestPath)
	if err != nil {
		return "", 0, errors.Newf("writeTmpKeyFile: %v", err)
	}
	defer os.Remove(tmpName)

	stdout, stderr, err := process.Exec("SSHKeygenParsePublicKey", keygenPath, "-lf", tmpName)
	if err != nil {
		return "", 0, errors.Newf("fail to parse public key: %s - %s", err, stderr)
	}
	if strings.Contains(stdout, "is not a public key file") {
		return "", 0, ErrKeyUnableVerify{stdout}
	}

	fields := strings.Split(stdout, " ")
	if len(fields) < 4 {
		return "", 0, errors.Newf("invalid public key line: %s", stdout)
	}

	keyType := strings.Trim(fields[len(fields)-1], "()\r\n")
	return strings.ToLower(keyType), com.StrTo(fields[0]).MustInt(), nil
}

// SSHNativeParsePublicKey extracts the key type and length using the golang SSH library.
func SSHNativeParsePublicKey(keyLine string) (string, int, error) {
	fields := strings.Fields(keyLine)
	if len(fields) < 2 {
		return "", 0, errors.Newf("not enough fields in public key line: %s", keyLine)
	}