Skip to content

Commit

Permalink
Fix bug introduced in previous commit (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsaha authored Oct 23, 2022
1 parent 1b5f94d commit a913105
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
8 changes: 5 additions & 3 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,18 @@ func setupBackupDir(backupDir, service, githostURL *string) string {
var gitHost, backupPath string
var err error

if githostURL != nil {
if len(*githostURL) != 0 {
u, err := url.Parse(*githostURL)
if err != nil {
panic(err)
}
gitHost = u.Host
} else {
gitHost = knownServices[*service]
log.Println("knownservices", gitHost)
}

if backupDir == nil {
if len(*backupDir) == 0 {
homeDir, err := homedir.Dir()
if err == nil {
backupPath = path.Join(homeDir, ".gitbackup", gitHost)
Expand All @@ -95,9 +96,10 @@ func setupBackupDir(backupDir, service, githostURL *string) string {
} else {
backupPath = path.Join(*backupDir, gitHost)
}

err = createBackupRootDirIfRequired(backupPath)
if err != nil {
log.Fatal(err)
log.Fatalf("Error creating backup directory: %s %v", backupPath, err)
}
return backupPath
}
Expand Down
38 changes: 19 additions & 19 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,64 +158,64 @@ func TestSetupBackupDir(t *testing.T) {
serviceGitlabCustomUrl := "https://company.gitlab.com"

var testConfigs = []struct {
backupRootDir *string
backupRootDir string
gitService string
gitServiceUrl *string
gitServiceUrl string
wantBackupPath string
}{
{
nil,
"",
"github",
nil,
"",
"/home/fakeuser/.gitbackup/github.com",
},
{
&backupRoot,
backupRoot,
"github",
nil,
"",
"/my/backup/root/github.com",
},
{
&backupRoot,
backupRoot,
"github",
&serviceGithubCustomUrl,
serviceGithubCustomUrl,
"/my/backup/root/company.github.com",
},
{
nil,
"",
"gitlab",
nil,
"",
"/home/fakeuser/.gitbackup/gitlab.com",
},

{
&backupRoot,
backupRoot,
"gitlab",
nil,
"",
"/my/backup/root/gitlab.com",
},
{
&backupRoot,
backupRoot,
"gitlab",
&serviceGitlabCustomUrl,
serviceGitlabCustomUrl,
"/my/backup/root/company.gitlab.com",
},
{
&backupRoot,
backupRoot,
"bitbucket",
nil,
"",
"/my/backup/root/bitbucket.org",
},
{
nil,
"",
"bitbucket",
nil,
"",
"/home/fakeuser/.gitbackup/bitbucket.org",
},
}

for _, tc := range testConfigs {
backupdir := setupBackupDir(tc.backupRootDir, &tc.gitService, tc.gitServiceUrl)
backupdir := setupBackupDir(&tc.backupRootDir, &tc.gitService, &tc.gitServiceUrl)
if backupdir != tc.wantBackupPath {
t.Errorf("Expected %s, Got %s", tc.wantBackupPath, backupdir)
}
Expand Down

0 comments on commit a913105

Please sign in to comment.