Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change working directory location #9

Merged
merged 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ type getCommand struct {
}

func (g *getCommand) Execute(args []string) error {
bin, err := ghgBin()
gHome, err := ghgHome()
if err != nil {
return err
}
ghcli := getOctCli(getToken())
for _, target := range args {
gh := &ghg{
binDir: bin,
ghgHome: gHome,
target: target,
client: ghcli,
upgrade: g.Upgrade,
Expand Down
54 changes: 16 additions & 38 deletions ghg.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ func getOctCli(token string) *octokit.Client {
}

type ghg struct {
binDir string
ghgHome string
target string
client *octokit.Client
upgrade bool
}

func (gh *ghg) getBinDir() string {
if gh.binDir != "" {
return gh.binDir
func (gh *ghg) getGhgHome() string {
if gh.ghgHome != "" {
return gh.ghgHome
}
return "."
}

func (gh *ghg) getBinDir() string {
return filepath.Join(gh.getGhgHome(), "bin")
}

var releaseByTagURL = octokit.Hyperlink("repos/{owner}/{repo}/releases/tags/{tag}")
var (
archiveReg = regexp.MustCompile(`\.(?:zip|tgz|tar\.gz)$`)
Expand Down Expand Up @@ -81,6 +85,10 @@ func (gh *ghg) get() error {
if len(urls) < 1 {
return fmt.Errorf("no assets available")
}
err = os.MkdirAll(filepath.Join(gh.getGhgHome(), "tmp"), 0755)
if err != nil {
return errors.Wrap(err, "failed to create tmpdir")
}
log.Printf("install %s/%s version: %s", owner, repo, tag)
for _, url := range urls {
err := gh.install(url)
Expand All @@ -93,7 +101,7 @@ func (gh *ghg) get() error {

func (gh *ghg) install(url string) error {
log.Printf("download %s\n", url)
archivePath, err := download(url)
archivePath, err := gh.download(url)
if err != nil {
return errors.Wrap(err, "failed to download")
}
Expand Down Expand Up @@ -140,14 +148,10 @@ func (gh *ghg) place(src, dest string) error {
log.Printf("%s exists. overwrite it", dest)
}
log.Printf("install %s\n", filepath.Base(dest))
err := os.Rename(src, dest)
if err != nil {
return copyExecutable(src, dest)
}
return nil
return os.Rename(src, dest)
}

func download(url string) (fpath string, err error) {
func (gh *ghg) download(url string) (fpath string, err error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
err = errors.Wrap(err, "failed to create request")
Expand All @@ -165,7 +169,7 @@ func download(url string) (fpath string, err error) {
return
}
archiveBase := path.Base(url)
tempdir, err := ioutil.TempDir("", "ghg-")
tempdir, err := ioutil.TempDir(filepath.Join(gh.getGhgHome(), "tmp"), "")
if err != nil {
err = errors.Wrap(err, "failed to create tempdir")
return
Expand Down Expand Up @@ -260,32 +264,6 @@ func exists(filename string) bool {
return err == nil
}

func copyExecutable(srcName string, destName string) error {
src, err := os.Open(srcName)
if err != nil {
return err
}
defer src.Close()

dest, err := os.Create(destName)
if err != nil {
return err
}
defer dest.Close()

_, err = io.Copy(dest, src)
if err != nil {
return err
}

fileInfo, err := os.Stat(srcName)
if err != nil {
return err
}

return os.Chmod(destName, fileInfo.Mode())
}

func lcs(a, b string) string {
arunes := []rune(a)
brunes := []rune(b)
Expand Down