1package common
 2
 3import (
 4	"fmt"
 5	"net/url"
 6
 7	"github.com/charmbracelet/soft-serve/server/utils"
 8	"github.com/muesli/reflow/truncate"
 9)
10
11// TruncateString is a convenient wrapper around truncate.TruncateString.
12func TruncateString(s string, max int) string {
13	if max < 0 {
14		max = 0
15	}
16	return truncate.StringWithTail(s, uint(max), "…")
17}
18
19// RepoURL returns the URL of the repository.
20func RepoURL(publicURL, name string) string {
21	name = utils.SanitizeRepo(name) + ".git"
22	url, err := url.Parse(publicURL)
23	if err == nil {
24		switch url.Scheme {
25		case "ssh":
26			port := url.Port()
27			if port == "" || port == "22" {
28				return fmt.Sprintf("git@%s:%s", url.Hostname(), name)
29			} else {
30				return fmt.Sprintf("ssh://%s:%s/%s", url.Hostname(), url.Port(), name)
31			}
32		}
33	}
34
35	return fmt.Sprintf("%s/%s", publicURL, name)
36}
37
38// CloneCmd returns the URL of the repository.
39func CloneCmd(publicURL, name string) string {
40	return fmt.Sprintf("git clone %s", RepoURL(publicURL, name))
41}