-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,36 @@ | ||
# CMDR | ||
CMDR is a simple command version manager, easy to manage and switch multiple commands. | ||
CMDR is a simple command version management tool that helps you quickly switch from multiple command versions. | ||
|
||
## Installation | ||
1. Download the pre-compiled binaries from [releases](https://github.com/MrLYC/cmdr/releases). | ||
2. Decompress the tar.gz file. | ||
3. In order to setup the cmdr environment, run: `cmdr setup`. | ||
Download the latest release from [GitHub](https://github.com/MrLYC/cmdr/releases) and make sure it is executable. | ||
Run the following command to install it in your system: | ||
```shell | ||
% /path/to/cmdr setup | ||
``` | ||
|
||
Check the CMDR version information by running the following command: | ||
```shell | ||
% cmdr version -a | ||
``` | ||
|
||
## Get Started | ||
To install a new command, run the following command: | ||
```shell | ||
% 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> | ||
``` | ||
|
||
Use a specified command version: | ||
```shell | ||
% cmdr command use -n <command-name> -v <version> | ||
``` | ||
|
||
## Upgrade | ||
To upgrade the CMDR, run: | ||
```shell | ||
% cmdr upgrade | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package core | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/mrlyc/cmdr/define" | ||
"github.com/mrlyc/cmdr/utils" | ||
) | ||
|
||
type ShellProfiler struct { | ||
BaseStep | ||
shell string | ||
script string | ||
} | ||
|
||
func (s *ShellProfiler) String() string { | ||
return "shell-profiler" | ||
} | ||
|
||
func (s *ShellProfiler) isContainsProfile(path string) bool { | ||
fs := define.FS | ||
|
||
file, err := fs.Open(path) | ||
if err != nil { | ||
return false | ||
} | ||
defer utils.CallClose(file) | ||
|
||
content, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return bytes.Contains(content, []byte(s.script)) | ||
} | ||
|
||
func (s *ShellProfiler) Run(ctx context.Context) (context.Context, error) { | ||
fs := define.FS | ||
logger := define.Logger | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
return ctx, errors.Wrapf(err, "failed to get user home dir") | ||
} | ||
|
||
script := `eval "$(cmdr init)` | ||
var profile string | ||
switch s.shell { | ||
case "bash": | ||
profile = filepath.Join(homeDir, ".bashrc") | ||
case "zsh": | ||
profile = filepath.Join(homeDir, ".zshrc") | ||
default: | ||
logger.Warn("shell is not supported, please execute this script to init cmdr environment", map[string]interface{}{ | ||
"shell": s.shell, | ||
"script": script, | ||
}) | ||
} | ||
|
||
if s.isContainsProfile(profile) { | ||
return ctx, nil | ||
} | ||
|
||
file, err := fs.OpenFile(profile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return ctx, errors.Wrapf(err, "failed to open profile file") | ||
} | ||
defer utils.CallClose(file) | ||
|
||
_, err = fmt.Fprintf(file, "\n%s\n", script) | ||
if err != nil { | ||
return ctx, errors.Wrapf(err, "failed to write to profile file") | ||
} | ||
|
||
return ctx, nil | ||
} | ||
|
||
func NewShellProfiler(shell string) *ShellProfiler { | ||
return &ShellProfiler{ | ||
shell: filepath.Base(shell), | ||
script: `eval "$(cmdr init)`, | ||
} | ||
} |