Skip to content

Commit

Permalink
fix: support git@github.com:kubeshop/testkube.git syntax support for …
Browse files Browse the repository at this point in the history
…cloning repositories (#5786)

* fix: add git@github.com:kubeshop/testkube.git syntax support for cloning URI
* feat: ensure new line on EOF in SSH key
  • Loading branch information
rangoo94 authored Aug 29, 2024
1 parent 0a7b9f6 commit 14486e2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cmd/testworkflow-toolkit/commands/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/kballard/go-shellquote"
Expand All @@ -15,6 +16,10 @@ import (
"github.com/kubeshop/testkube/pkg/ui"
)

var (
protocolRe = regexp.MustCompile(`^[^:]+://`)
)

func NewCloneCmd() *cobra.Command {
var (
rawPaths []string
Expand All @@ -31,6 +36,10 @@ func NewCloneCmd() *cobra.Command {
Args: cobra.ExactArgs(2),

Run: func(cmd *cobra.Command, args []string) {
// Append SSH protocol if there is missing one and it looks like that (git@github.com:kubeshop/testkube.git)
if !protocolRe.MatchString(args[0]) && strings.ContainsRune(args[0], ':') && !strings.ContainsRune(args[0], '\\') {
args[0] = "ssh://" + strings.Replace(args[0], ":", "/", 1)
}
uri, err := url.Parse(args[0])
ui.ExitOnError("repository uri", err)
destinationPath, err := filepath.Abs(args[1])
Expand Down Expand Up @@ -69,8 +78,9 @@ func NewCloneCmd() *cobra.Command {
}
}

// Use the SSH key
if sshKey != "" {
// Use the SSH key (ensure there is new line at EOF)
sshKey = strings.TrimRight(sshKey, "\n") + "\n"
if sshKey != "\n" {
sshKeyPath := filepath.Join(constants.DefaultTmpDirPath, "id_rsa")
err := os.WriteFile(sshKeyPath, []byte(sshKey), 0400)
ui.ExitOnError("saving SSH key temporarily", err)
Expand Down

0 comments on commit 14486e2

Please sign in to comment.