Refactor: apply new internal structurs #206

Merged
lunny merged 22 commits from 6543/tea:refactor_new-internal-structure into master 2020-09-30 05:11:35 +00:00
2 changed files with 22 additions and 18 deletions
Showing only changes of commit 2224d8f35d - Show all commits

View File

@ -10,10 +10,10 @@ import (
"io/ioutil"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
"code.gitea.io/tea/modules/utils"
git_transport "github.com/go-git/go-git/v5/plumbing/transport"
gogit_http "github.com/go-git/go-git/v5/plumbing/transport/http"
gogit_ssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
@ -67,9 +67,9 @@ func GetAuthForURL(remoteURL *url.URL, httpUser, keyFile string) (auth git_trans
func readSSHPrivKey(keyFile string) (sig ssh.Signer, err error) {
if keyFile != "" {
keyFile, err = absPathWithExpansion(keyFile)
keyFile, err = utils.AbsPathWithExpansion(keyFile)
} else {
keyFile, err = absPathWithExpansion("~/.ssh/id_rsa")
keyFile, err = utils.AbsPathWithExpansion("~/.ssh/id_rsa")
}
if err != nil {
return nil, err
@ -104,17 +104,3 @@ func promptPass(domain string) (string, error) {
pass, err := terminal.ReadPassword(0)
return string(pass), err
}
func absPathWithExpansion(p string) (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
if p == "~" {
return u.HomeDir, nil
} else if strings.HasPrefix(p, "~/") {
return filepath.Join(u.HomeDir, p[2:]), nil
} else {
return filepath.Abs(p)
}
}

View File

@ -7,6 +7,9 @@ package utils
import (
"errors"
"os"
"os/user"
"path/filepath"
"strings"
)
// PathExists returns whether the given file or directory exists or not
@ -35,3 +38,18 @@ func FileExist(fileName string) (bool, error) {
}
return true, nil
}
// AbsPathWithExpansion expand path beginning with "~/" to absolute path
func AbsPathWithExpansion(p string) (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
if p == "~" {
return u.HomeDir, nil
} else if strings.HasPrefix(p, "~/") {
return filepath.Join(u.HomeDir, p[2:]), nil
} else {
return filepath.Abs(p)
}
}