Skip to content

Commit

Permalink
MakePackagePath
Browse files Browse the repository at this point in the history
  • Loading branch information
3846masa committed Feb 13, 2017
1 parent bc12aa0 commit fe06f35
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
6 changes: 1 addition & 5 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"

Expand All @@ -27,9 +26,6 @@ var editCommand = &cobra.Command{
}

pkgName := args[0]
if runtime.GOOS == "windows" {
pkgName = pkgName + ".bat"
}
pm := packages.NewPackageManager(viper.GetString("install_path"))
_, err := pm.Load(pkgName)
if err != nil {
Expand All @@ -55,7 +51,7 @@ var editCommand = &cobra.Command{

editorArgs := []string{
editorPath,
filepath.Join(pm.InstallPath, pkgName),
pm.MakePackagePath(pkgName),
}

if runtime.GOOS == "windows" {
Expand Down
3 changes: 1 addition & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/bfirsh/whalebrew/client"
"github.com/bfirsh/whalebrew/packages"
Expand Down Expand Up @@ -71,7 +70,7 @@ var installCommand = &cobra.Command{
if err != nil {
return err
}
fmt.Printf("🐳 Installed %s to %s\n", imageName, filepath.Join(pm.InstallPath, pkg.Name))
fmt.Printf("🐳 Installed %s to %s\n", imageName, pm.MakePackagePath(pkg.Name))
return nil
},
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"path/filepath"

"github.com/Songmu/prompter"
"github.com/bfirsh/whalebrew/packages"
Expand All @@ -28,7 +27,7 @@ var uninstallCommand = &cobra.Command{
pm := packages.NewPackageManager(viper.GetString("install_path"))
packageName := args[0]

path := filepath.Join(pm.InstallPath, packageName)
path := pm.MakePackagePath(packageName)

if !prompter.YN(fmt.Sprintf("This will permanently delete '%s'. Are you sure?", path), false) {
return nil
Expand Down
37 changes: 28 additions & 9 deletions packages/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

Expand All @@ -31,16 +32,16 @@ func (pm *PackageManager) Install(pkg *Package) error {
return err
}

packagePath := filepath.Join(pm.InstallPath, pkg.Name)
packagePath := pm.MakePackagePath(pkg.Name)

if _, err := os.Stat(packagePath); err == nil {
return fmt.Errorf("'%s' already exists", packagePath)
}

if runtime.GOOS == "windows" {
d = append([]byte(":: |\n @( whalebrew run %~f0 %* || exit /b %ERRORLEVEL% ) && exit /b 0\n"), d...)
d = bytes.Replace(d, []byte("\r\n"), []byte("\n"), -1)
d = bytes.Replace(d, []byte("\n"), []byte("\r\n"), -1)
packagePath = packagePath + ".bat"
} else {
d = append([]byte("#!/usr/bin/env whalebrew\n"), d...)
}
Expand Down Expand Up @@ -72,27 +73,30 @@ func (pm *PackageManager) List() (map[string]*Package, error) {
return packages, err
}
if isPackage {
pkg, err := pm.Load(file.Name())
pkgName := file.Name()
if runtime.GOOS == "windows" {
re := regexp.MustCompile("\\.bat$")
pkgName = re.ReplaceAllLiteralString(pkgName, "")
}

pkg, err := pm.Load(pkgName)
if err != nil {
return packages, err
}
packages[file.Name()] = pkg
packages[pkgName] = pkg
}
}
return packages, nil
}

// Load returns an installed package given its package name
func (pm *PackageManager) Load(name string) (*Package, error) {
return LoadPackageFromPath(filepath.Join(pm.InstallPath, name))
return LoadPackageFromPath(pm.MakePackagePath(name))
}

// Uninstall uninstalls a package
func (pm *PackageManager) Uninstall(packageName string) error {
p := filepath.Join(pm.InstallPath, packageName)
if runtime.GOOS == "windows" {
p = p + ".bat"
}
p := pm.MakePackagePath(packageName)
isPackage, err := IsPackage(p)
if err != nil {
return err
Expand Down Expand Up @@ -171,3 +175,18 @@ func IsPackage(path string) (bool, error) {

return false, nil
}

// MakePackagePath returns package path
func MakePackagePath(dir, name string) string {
// if on Windows, file is batch file.
pkgPath := filepath.Join(dir, name)
if runtime.GOOS == "windows" {
pkgPath = pkgPath + ".bat"
}
return pkgPath
}

// MakePackagePath returns package path
func (pm *PackageManager) MakePackagePath(name string) string {
return MakePackagePath(pm.InstallPath, name)
}

0 comments on commit fe06f35

Please sign in to comment.