Initial commit

This commit is contained in:
Bernd Ahlers
2017-04-07 17:11:08 +02:00
commit 80ebf85e88
1054 changed files with 345087 additions and 0 deletions

48
utils/github.go Normal file
View File

@@ -0,0 +1,48 @@
package utils
import (
"github.com/pkg/errors"
"path/filepath"
"strings"
)
func ParseGitHubURL(url string) (GitHubURL, error) {
gitHubURL := GitHubURL{}
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
}
type GitHubURL struct {
Repository string
}
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
}

67
utils/github_test.go Normal file
View File

@@ -0,0 +1,67 @@
package utils
import "testing"
func TestParseGitHubURL(t *testing.T) {
var url GitHubURL
var expected string
var err error
// Custom URL
url, _ = ParseGitHubURL("github://Graylog2/graylog2-server.git")
expected = "git@github.com:Graylog2/graylog2-server.git"
if url.SSH() != expected {
t.Errorf("expected <%s> but got <%s>", expected, url.SSH())
}
url, _ = ParseGitHubURL("github://Graylog2/graylog2-server.git")
expected = "https://github.com/Graylog2/graylog2-server.git"
if url.HTTPS() != expected {
t.Errorf("expected <%s> but got <%s>", expected, url.HTTPS())
}
// Git URL
url, _ = ParseGitHubURL("git@github.com:Graylog2/graylog2-server.git")
expected = "git@github.com:Graylog2/graylog2-server.git"
if url.SSH() != expected {
t.Errorf("expected <%s> but got <%s>", expected, url.SSH())
}
url, _ = ParseGitHubURL("git@github.com:Graylog2/graylog2-server.git")
expected = "https://github.com/Graylog2/graylog2-server.git"
if url.HTTPS() != expected {
t.Errorf("expected <%s> but got <%s>", expected, url.HTTPS())
}
// HTTPS URL
url, _ = ParseGitHubURL("https://github.com/Graylog2/graylog2-server.git")
expected = "git@github.com:Graylog2/graylog2-server.git"
if url.SSH() != expected {
t.Errorf("expected <%s> but got <%s>", expected, url.SSH())
}
url, _ = ParseGitHubURL("https://github.com/Graylog2/graylog2-server.git")
expected = "https://github.com/Graylog2/graylog2-server.git"
if url.HTTPS() != expected {
t.Errorf("expected <%s> but got <%s>", expected, url.HTTPS())
}
// Directory
url, _ = ParseGitHubURL("https://github.com/Graylog2/graylog2-server.git")
expected = "graylog2-server"
if url.Directory() != expected {
t.Errorf("expected <%s> but got <%s>", expected, url.HTTPS())
}
// Missing .git suffix
_, err = ParseGitHubURL("https://github.com/Graylog2/graylog2-server")
if err == nil {
t.Error("expected URL without .git suffix to fail")
}
// Unknown URL format
_, err = ParseGitHubURL("https://example.com/Graylog2/graylog2-server")
if err == nil {
t.Error("expected unknown URL to fail")
}
}

112
utils/utils.go Normal file
View File

@@ -0,0 +1,112 @@
package utils
import (
"bytes"
"errors"
"github.com/Graylog2/graylog-project-cli/logger"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func GetCwd() string {
currentDir, err := os.Getwd()
if err != nil {
logger.Fatal("Unable to get current directory: %v", err)
}
return currentDir
}
func Chdir(path string) {
if err := os.Chdir(path); err != nil {
logger.Fatal("Unable to change into directory %v: %v", path, err)
}
}
func GetRelativePath(path string) string {
if !filepath.IsAbs(path) {
return path
}
cwd, err := os.Getwd()
if err != nil {
logger.Fatal("Unable to get current working directory")
}
relPath, err := filepath.Rel(cwd, path)
if err != nil {
logger.Fatal("Unable to get relative path for %v", path)
}
return relPath
}
func GetAbsolutePath(path string) string {
absolutePath, err := filepath.Abs(path)
if err != nil {
logger.Fatal("Unable to get absolute path for %s: %v", path, err)
}
return absolutePath
}
func NameFromRepository(repository string) string {
if strings.HasPrefix(repository, "https://") {
return strings.Replace(strings.Split(strings.TrimPrefix(repository, "https://"), "/")[2], ".git", "", 1)
} else if strings.HasPrefix(repository, "git@") {
return strings.Replace(strings.Split(repository, "/")[1], ".git", "", 1)
} else {
logger.Fatal("Unable to get name from repository: %s", repository)
}
return ""
}
func ConvertGithubGitToHTTPS(repository string) string {
return strings.Replace(repository, "git@github.com:", "https://github.com/", 1)
}
func FirstNonEmpty(values ...string) (string, error) {
for idx := range values {
trimmedValue := strings.TrimSpace(values[idx])
if trimmedValue != "" {
return values[idx], nil
}
}
return "", errors.New("all values are empty")
}
func FileExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
type DirectoryCallback func()
func InDirectory(path string, callback DirectoryCallback) {
defer Chdir(GetCwd())
Chdir(path)
callback()
}
func FilesIdentical(file1, file2 string) bool {
buf1, err := ioutil.ReadFile(file1)
if err != nil {
logger.Fatal("Unable to read file <%s>: %v", file1, err)
}
buf2, err := ioutil.ReadFile(file2)
if err != nil {
logger.Fatal("Unable to read file <%s>: %v", file2, err)
}
return bytes.Equal(buf1, buf2)
}

35
utils/utils_test.go Normal file
View File

@@ -0,0 +1,35 @@
package utils_test
import (
"github.com/Graylog2/graylog-project-cli/utils"
"testing"
)
const sshRepo = "git@github.com:Graylog2/graylog2-server.git"
const httpsRepo = "https://github.com/Graylog2/graylog2-server.git"
func TestNameFromRepository(t *testing.T) {
expected := "graylog2-server"
sshRepoName := utils.NameFromRepository(sshRepo)
if sshRepoName != expected {
t.Errorf("Repository %s does not resolve to name %s - result: `%s`", sshRepo, expected, sshRepoName)
}
httpsRepoName := utils.NameFromRepository(httpsRepo)
if httpsRepoName != expected {
t.Errorf("Repository %s does not resolve to name %s - result: `%s`", httpsRepo, expected, httpsRepoName)
}
}
func TestConvertGithubGitToHTTPS(t *testing.T) {
toHTTPS := utils.ConvertGithubGitToHTTPS(sshRepo)
if toHTTPS != httpsRepo {
t.Errorf("Repository %s was not converted to %s - result: %s", sshRepo, httpsRepo, toHTTPS)
}
toHTTPS = utils.ConvertGithubGitToHTTPS(httpsRepo)
if toHTTPS != httpsRepo {
t.Errorf("Repository %s was converted to %s - that should not happen", httpsRepo, toHTTPS)
}
}