Skip to content

Commit c308889

Browse files
committed
feat: ✨ add use command
1 parent 07cd316 commit c308889

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/cmd/use.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//Package cmd
2+
/*
3+
Copyright © 2024 UnreadCode <i@unreadcode.com>
4+
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"fmt"
11+
"github.com/spf13/cobra"
12+
"pvm/utils"
13+
"regexp"
14+
)
15+
16+
// useCmd represents the use command
17+
var useCmd = &cobra.Command{
18+
Use: "use",
19+
Short: "Switch to the specified PHP version",
20+
Long: `Quickly switch between different PHP versions.
21+
usage example:
22+
pvm use 8.0
23+
`,
24+
25+
Run: useRun,
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(useCmd)
30+
}
31+
32+
func useRun(cmd *cobra.Command, args []string) {
33+
if len(args) == 0 {
34+
utils.PrintMsg("Please specify a PHP version", "Warning", 1)
35+
}
36+
version := args[0]
37+
// 是一个有效的版本号
38+
if !regexp.MustCompile(`^\d+\.\d+$`).MatchString(version) {
39+
utils.PrintMsg("Invalid PHP version number.", "Error", 1)
40+
}
41+
// 是否已经在使用
42+
if currentVersion := utils.GetCurrentPhpVersion(); currentVersion == version {
43+
utils.PrintMsg(fmt.Sprintf("Already using PHP v%s", version), "Info", 0)
44+
}
45+
// 是否已经安装
46+
if !utils.IsInstalled(version) {
47+
utils.PrintMsg(fmt.Sprintf("PHP v%s is not installed.", version), "Error", 1)
48+
}
49+
// 切换到指定版本
50+
if err := utils.SwitchToVersion(version); err != nil {
51+
utils.PrintMsg(err.Error(), "Error", 1)
52+
}
53+
utils.PrintMsg(fmt.Sprintf("Switched to PHP v%s", version), "Success", 0)
54+
}

src/utils/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const (
1010

1111
PVM_ROOT = "PVM_ROOT"
1212

13+
PHP_PATH = "PHP_PATH"
14+
1315
RELEASES = "https://windows.php.net/downloads/releases/releases.json"
1416

1517
DOWNLOAD = "https://windows.php.net/downloads/releases/"

src/utils/utils.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Zip struct {
3030
}
3131

3232
var PvmRoot = os.Getenv(PVM_ROOT)
33+
var PhpPath = os.Getenv(PHP_PATH)
3334

3435
var MsgTypeMap = map[string]color.Attribute{
3536
"Error": color.FgRed,
@@ -314,6 +315,38 @@ func UninstallPhpVersion(version string) error {
314315
return nil
315316
}
316317

318+
// SwitchToVersion 切换到指定版本
319+
func SwitchToVersion(version string) error {
320+
targetDir := filepath.Join(PvmRoot, fmt.Sprintf("v%s", version))
321+
if hasPermission() {
322+
os.Remove(PhpPath)
323+
if err := os.Symlink(targetDir, PhpPath); err != nil {
324+
return err
325+
}
326+
return nil
327+
} else {
328+
uacRun("pvm", "use", version)
329+
}
330+
return nil
331+
}
332+
333+
// 是否具有管理员权限
334+
func hasPermission() bool {
335+
if _, err := os.Open("\\\\.\\PHYSICALDRIVE0"); err != nil {
336+
return false
337+
}
338+
return true
339+
}
340+
341+
// 请求管理员权限运行程序
342+
func uacRun(command ...string) {
343+
//This executable file comes from https://jianqiezhushou.com/
344+
//Original author https://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/
345+
cmd := exec.Command("elevate.exe", "-k")
346+
cmd.Args = append(cmd.Args, command...)
347+
cmd.Run()
348+
}
349+
317350
// 下载composer installer
318351
func downloadComposer(installer string) error {
319352
resp, err := http.Get(COMPOSER)

0 commit comments

Comments
 (0)