mirror of
https://github.com/Graylog2/graylog-project-cli.git
synced 2026-03-13 08:02:57 +08:00
Use the transport of the given graylog-project URL to decide how to clone all other repositories. Also remove obsolete --use-ssh and --use-https flags from bootstrap command. Fixes #4
74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/pkg/errors"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func ParseGitHubURL(url string) (GitHubURL, error) {
|
|
gitHubURL := GitHubURL{URL: url}
|
|
|
|
if !strings.HasSuffix(url, ".git") {
|
|
return gitHubURL, errors.Errorf("GitHub URL is missing .git suffix: %s", url)
|
|
}
|
|
|
|
switch {
|
|
case strings.HasPrefix(url, "github://"):
|
|
gitHubURL.Repository = strings.Split(url, "//")[1]
|
|
case strings.HasPrefix(url, "git@github"):
|
|
gitHubURL.Repository = strings.Split(url, ":")[1]
|
|
case strings.HasPrefix(url, "https://github"):
|
|
gitHubURL.Repository = strings.Split(url, "github.com/")[1]
|
|
default:
|
|
return GitHubURL{}, errors.Errorf("unknown GitHub URL: %s", url)
|
|
}
|
|
|
|
return gitHubURL, nil
|
|
}
|
|
|
|
func ReplaceGitHubURL(url string, repoName string) (string, error) {
|
|
name := strings.TrimSuffix(repoName, ".git")
|
|
|
|
switch {
|
|
case strings.HasPrefix(url, "github://"):
|
|
return fmt.Sprintf("github://%s.git", name), nil
|
|
case strings.HasPrefix(url, "git@github"):
|
|
return fmt.Sprintf("git@github.com:%s.git", name), nil
|
|
case strings.HasPrefix(url, "https://github"):
|
|
return fmt.Sprintf("https://github.com/%s.git", name), nil
|
|
default:
|
|
return "", errors.Errorf("unknown GitHub URL: %s", url)
|
|
}
|
|
}
|
|
|
|
type GitHubURL struct {
|
|
URL string
|
|
Repository string
|
|
}
|
|
|
|
func (url GitHubURL) IsHTTPS() bool {
|
|
return strings.HasPrefix(url.URL, "https://")
|
|
}
|
|
|
|
func (url GitHubURL) IsSSH() bool {
|
|
return strings.HasPrefix(url.URL, "git@")
|
|
}
|
|
|
|
func (url GitHubURL) SSH() string {
|
|
return "git@github.com:" + url.Repository
|
|
}
|
|
|
|
func (url GitHubURL) HTTPS() string {
|
|
return "https://github.com/" + url.Repository
|
|
}
|
|
|
|
func (url GitHubURL) Directory() string {
|
|
return strings.TrimSuffix(filepath.Base(url.Repository), filepath.Ext(url.Repository))
|
|
}
|
|
|
|
func (url GitHubURL) String() string {
|
|
return "github://" + url.Repository
|
|
}
|