Skip to content

Commit

Permalink
Merge pull request #22 from MrLYC/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MrLYC authored May 7, 2023
2 parents 6edee69 + f1463d6 commit 0663834
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Install
run: curl -o- https://raw.githubusercontent.com/MrLYC/cmdr/master/install.sh | bash
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ curl -o- https://raw.githubusercontent.com/MrLYC/cmdr/master/install.sh | ${SHEL
2. Make sure the download asset is executable;
3. Run the following command to install the binary:
```shell
% /path/to/cmdr init
/path/to/cmdr init
```
4. Restart your shell and run the following command to verify the installation:
```shell
% cmdr version
cmdr version
```

## Get Started
To install a new command, run the following command:
```shell
% cmdr command install -n <command-name> -v <version> -l <path-or-url>
cmdr command install -n <command-name> -v <version> -l <path-or-url>
```

Then you can list the installed commands by running the following command:
```shell
% cmdr command list -n <command-name>
cmdr command list -n <command-name>
```

Use a specified command version:
```shell
% cmdr command use -n <command-name> -v <version>
cmdr command use -n <command-name> -v <version>
```

## Upgrade
To upgrade the CMDR, just run:
```shell
% cmdr upgrade
cmdr upgrade
```
25 changes: 6 additions & 19 deletions core/manager/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

. "github.com/ahmetb/go-linq/v3"
ver "github.com/hashicorp/go-version"
"github.com/homedepot/flop"
"github.com/pkg/errors"

"github.com/mrlyc/cmdr/core"
Expand Down Expand Up @@ -213,11 +212,6 @@ func (m *BinaryManager) Define(name string, version string, location string) (co
return nil, errors.WithMessagef(err, "link %s to %s failed", location, dstLocation)
}

err = os.Chmod(dstLocation, 0755)
if err != nil {
return nil, errors.Wrapf(err, "chmod %s failed", dstLocation)
}

return NewBinary(m.binDir, m.shimsDir, name, version, shimsName), nil
}

Expand Down Expand Up @@ -290,15 +284,10 @@ func NewBinaryManagerWithCopy(
dirMode os.FileMode,
) *BinaryManager {
return NewBinaryManager(binDir, shimsDir, dirMode, func(src, dst string) error {
err := flop.Copy(src, dst, flop.Options{
MkdirAll: true,
Recursive: true,
})
if err != nil {
return errors.WithMessagef(err, "copy %s to %s failed", src, dst)
}
dstDir, dstName := filepath.Split(dst)
helper := utils.NewPathHelper(dstDir)

return nil
return helper.CopyFile(dstName, src, 0755)
})
}

Expand All @@ -307,12 +296,10 @@ func NewBinaryManagerWithLink(
dirMode os.FileMode,
) *BinaryManager {
return NewBinaryManager(binDir, shimsDir, dirMode, func(src, dst string) error {
location, err := filepath.Abs(src)
if err != nil {
return err
}
dstDir, dstName := filepath.Split(dst)
helper := utils.NewPathHelper(dstDir)

return os.Symlink(location, dst)
return helper.SymbolLink(dstName, src, 0755)
})
}

Expand Down
12 changes: 6 additions & 6 deletions core/manager/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ var _ = Describe("Binary", func() {
}

BeforeEach(func() {
binDir, err = os.MkdirTemp("", "")
binDir, err = os.MkdirTemp("", "bin")
Expect(err).To(BeNil())

shimsDir, err = os.MkdirTemp("", "")
shimsDir, err = os.MkdirTemp("", "shims")
Expect(err).To(BeNil())

mgr = manager.NewBinaryManagerWithCopy(binDir, shimsDir, 0755)
mgr = manager.NewBinaryManagerWithLink(binDir, shimsDir, 0755)
})

JustBeforeEach(func() {
Expand All @@ -208,7 +208,7 @@ var _ = Describe("Binary", func() {
Expect(os.RemoveAll(binDir)).To(Succeed())
Expect(os.RemoveAll(shimsDir)).To(Succeed())

mgr = manager.NewBinaryManagerWithCopy(binDir, shimsDir, 0755)
mgr = manager.NewBinaryManagerWithLink(binDir, shimsDir, 0755)
Expect(mgr.Init()).To(Succeed())
Expect(binDir).To(BeADirectory())
Expect(shimsDir).To(BeADirectory())
Expand Down Expand Up @@ -244,9 +244,9 @@ var _ = Describe("Binary", func() {
shimsPath := getShimsPath(name)
Expect(shimsPath).To(BeARegularFile())

info, err := os.Stat(shimsPath)
info, err := os.Lstat(shimsPath)
Expect(err).To(BeNil())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
Expect(info.Mode() & os.FileMode(0755)).To(Equal(os.FileMode(0755)))
}

It("should define a command", func() {
Expand Down
57 changes: 33 additions & 24 deletions core/utils/pathlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ import (
"github.com/pkg/errors"
)

func ensureNotExists(path string) error {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return nil
}

if err != nil {
return errors.Wrapf(err, "stat dir %s failed", path)
}

if info.IsDir() {
err = os.RemoveAll(path)
} else {
err = os.Remove(path)
}

if err != nil {
return errors.Wrapf(err, "remove dir %s failed", path)
}

return nil
}

type PathHelper struct {
path string
}
Expand Down Expand Up @@ -36,26 +59,7 @@ func (p *PathHelper) Parent() *PathHelper {
func (p *PathHelper) EnsureNotExists(name string) error {
path := filepath.Join(p.path, name)

info, err := os.Stat(path)
if os.IsNotExist(err) {
return nil
}

if err != nil {
return errors.Wrapf(err, "stat dir %s failed", path)
}

if info.IsDir() {
err = os.RemoveAll(path)
} else {
err = os.Remove(path)
}

if err != nil {
return errors.Wrapf(err, "remove dir %s failed", path)
}

return nil
return ensureNotExists(path)
}

func (p *PathHelper) Exists(name string) error {
Expand Down Expand Up @@ -84,13 +88,18 @@ func (p *PathHelper) SymbolLink(name, target string, mode os.FileMode) error {
return err
}

path := filepath.Join(p.path, name)
err = os.Symlink(target, path)
absTarget, err := filepath.Abs(target)
if err != nil {
return errors.Wrapf(err, "get abs target path %s failed", target)
}

linkFile := p.Child(name).Path()
err = os.Symlink(absTarget, linkFile)
if err != nil {
return errors.Wrapf(err, "create symbol link failed")
}

return p.Chmod(name, mode)
return os.Chmod(linkFile, os.ModeSymlink|mode)
}

func (p *PathHelper) CopyFile(name, target string, mode os.FileMode) error {
Expand All @@ -108,7 +117,7 @@ func (p *PathHelper) CopyFile(name, target string, mode os.FileMode) error {
return errors.WithMessagef(err, "copy file failed")
}

return p.Chmod(name, mode)
return os.Chmod(path, mode)
}

func (p *PathHelper) RealPath(name string) (string, error) {
Expand Down
2 changes: 2 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ chmod +x "${target}"
rm -f "${target}"
set +x

"${target}" command list -n cmdr

echo "restart your terminal to activate the cmdr command"

0 comments on commit 0663834

Please sign in to comment.