Skip to content

Commit

Permalink
Add -d flag for show/upgrade command
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Oct 26, 2020
1 parent e005658 commit ff68510
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
.vscode/
.DS_Store

pls
pls*
24 changes: 6 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,12 @@ Linux 是每位开发者必备的技能,如何高效地掌握 Linux 命令就

### 🔰 安装

使用 `go get` 安装
```shell
$ go get -u github.com/chenjiandongx/pls
```

使用编译好的二进制版本
```shell
# https://github.com/chenjiandongx/pls/releases
* 使用 `go get` 安装
```shell
$ go get -u github.com/chenjiandongx/pls
```

# linux
$ wget https://github.com/chenjiandongx/pls/releases/download/v0.1.2/pls_linux_amd64

# macos
$ wget https://github.com/chenjiandongx/pls/releases/download/v0.1.2/pls_darwin_amd64

# windows
$ wget https://github.com/chenjiandongx/pls/releases/download/v0.1.2/pls_windows_amd64.exe
```
* 使用编译好的二进制版本: [releases](https://github.com/chenjiandongx/pls/releases)

### 📏 使用

Expand All @@ -57,7 +45,7 @@ Use "pls [command] --help" for more information about a command.

### 🔖 示例

> Note: 建议第一次使用的时候先初始化所有命令
> Note: 建议第一次使用的时候先初始化所有命令,可以使用 -d 指定命令文档文件夹下载位置
```shell
$ pls upgrade
```
Expand Down
7 changes: 7 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

GOOS=linux go build -o pls_linux_amd64 .
GOOS=darwin go build -o pls_darwin_amd64 .
GOOS=windows go build -o pls_windows_amd64.exe .

ls | grep pls
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var commands = []string{
"awk",
"axel",
"badblocks",
"base64",
"basename",
"batch",
"bc",
Expand Down Expand Up @@ -358,6 +359,7 @@ var commands = []string{
"nmcli",
"nohup",
"nologin",
"nproc",
"nslookup",
"ntpdate",
"ntsysv",
Expand Down
42 changes: 27 additions & 15 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,31 @@ func NewShowCommand() *cobra.Command {
return
}
force, _ := cmd.Flags().GetBool("force")
showCmd(args[0], force)
dir, _ := cmd.Flags().GetString("directory")
showCmd(args[0], dir, force)
},
}
cmd.Flags().BoolP("force", "f", false, "force to refresh command usage from remote.")
cmd.Flags().StringP("directory", "d", "", "specify the command files directory (absolute path).")
return cmd
}

func showCmd(cmd string, force bool) {
func showCmd(cmd, dir string, force bool) {
cmd = strings.ToLower(cmd)
d := commandPath
if dir != "" {
d = dir
}

if force {
retryDownloadCmd(cmd)
retryDownloadCmd(cmd, d)
}

p := path.Join(getHomedir(), commandDir, fmt.Sprintf("%s.md", cmd))
p := path.Join(d, fmt.Sprintf("%s.md", cmd))
if !isFileExist(p) {
status, err := retryDownloadCmd(cmd)
status, err := retryDownloadCmd(cmd, d)
if err != nil {
fmt.Printf("[sorry]: fetch command <%s> error\n", cmd)
fmt.Printf("[sorry]: failed to retrieve command <%s>\n", cmd)
return
}
if status == http.StatusNotFound {
Expand All @@ -60,7 +67,7 @@ func showCmd(cmd string, force bool) {
}
source, err := ioutil.ReadFile(p)
if err != nil {
fmt.Printf("[sorry]: open file <%s> error\n", p)
fmt.Printf("[sorry]: failed to open file <%s>\n", p)
return
}
markdown.BlueBgItalic = color.New(color.FgBlue).SprintFunc()
Expand All @@ -73,9 +80,9 @@ func getHomedir() string {
return home
}

func makeCmdDir() error {
if _, err := os.Stat(commandPath); err != nil && !os.IsExist(err) {
return os.Mkdir(commandPath, 0755)
func makeCmdDir(dir string) error {
if _, err := os.Stat(dir); err != nil && !os.IsExist(err) {
return os.Mkdir(dir, 0755)
}
return nil
}
Expand All @@ -85,20 +92,25 @@ func isFileExist(path string) bool {
return !os.IsNotExist(err)
}

func retryDownloadCmd(cmd string) (int, error) {
func retryDownloadCmd(cmd, dir string) (int, error) {
var err error
var status int
for j := 0; j < maxRetry; j++ {
if err, status = downloadCmd(cmd); err != nil {
if err, status = downloadCmd(cmd, dir); err != nil {
continue
}
break
}
return status, err
}

func downloadCmd(cmd string) (error, int) {
if err := makeCmdDir(); err != nil {
func downloadCmd(cmd, dir string) (error, int) {
d := commandPath
if dir != "" {
d = dir
}

if err := makeCmdDir(d); err != nil {
return err, 0
}

Expand Down Expand Up @@ -127,6 +139,6 @@ func downloadCmd(cmd string) (error, int) {
content = append(content, []byte("\n")...)
}

fp := path.Join(commandPath, fmt.Sprintf("%s.md", cmd))
fp := path.Join(d, fmt.Sprintf("%s.md", cmd))
return ioutil.WriteFile(fp, content, 0666), 0
}
8 changes: 5 additions & 3 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ func NewUpgradeCommand() *cobra.Command {
Use: "upgrade",
Short: "Upgrade all commands from remote.",
Run: func(cmd *cobra.Command, args []string) {
upgradeCmd()
dir, _ := cmd.Flags().GetString("directory")
upgradeCmd(dir)
},
}
cmd.Flags().StringP("directory", "d", "", "specify the command files directory (absolute path).")
return cmd
}

func upgradeCmd() {
func upgradeCmd(dir string) {
var num int64
l := len(commands)
ch := make(chan string, 1)
Expand All @@ -35,7 +37,7 @@ func upgradeCmd() {
go func() {
defer wg.Done()
for cmd := range ch {
retryDownloadCmd(cmd)
retryDownloadCmd(cmd, dir)
atomic.AddInt64(&num, 1)
fmt.Printf("[busy working]: upgrade command:<%d/%d> => %s\n", atomic.LoadInt64(&num), l, cmd)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

const (
version = "pls version: [v0.1.2]"
version = "pls version: [v0.1.3]"
)

func NewVersionCommand() *cobra.Command {
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/chenjiandongx/pls

go 1.15

require (
github.com/MichaelMure/go-term-markdown v0.1.3
github.com/fatih/color v1.9.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.1
)
Loading

0 comments on commit ff68510

Please sign in to comment.