1 Star 0 Fork 0

walter/jenv

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
add-to-path.go 1.99 KB
Copy Edit Raw Blame History
wuchenxi03 authored 2025-11-01 12:22 +08:00 . update
package cmd
import (
"fmt"
"os"
"path/filepath"
"gitee.com/gitwcx/jenv/sdk/env"
"gitee.com/gitwcx/jenv/sdk/style"
"gitee.com/gitwcx/jenv/sdk/sys"
"github.com/spf13/cobra"
)
var pathCmd = &cobra.Command{
Aliases: []string{"path"},
Use: "add-to-path",
Short: "Add jenv to system PATH",
Long: `Add jenv executable to system PATH environment variable.
This command will add the directory containing the jenv executable
to your system's PATH environment variable, allowing you to run
jenv from any location.
Requires administrator/root privileges to modify system environment variables.`,
Example: ` jenv path
jenv add-to-path`,
Run: runPath,
}
func init() {
rootCmd.AddCommand(pathCmd)
}
func runPath(cmd *cobra.Command, args []string) {
RunAddToPath()
}
func RunAddToPath() {
fmt.Println(style.Header.Render("Adding jenv to System PATH"))
if !sys.IsAdmin() {
fmt.Println(style.Error.Render("Error: You must run this command as administrator/root"))
os.Exit(1)
}
// Get the directory containing the jenv executable
exePath, err := os.Executable()
if err != nil {
fmt.Printf("%s: Unable to locate jenv executable\n%s\n",
style.Error.Render("Error"),
style.Error.Render(err.Error()))
return
}
exeDir := filepath.Dir(exePath)
// Check if path already exists
if env.IsInPath(exeDir) {
fmt.Printf("%s: %s\n",
style.Info.Render("Notice"),
style.Info.Render("jenv is already in your PATH"))
return
}
// Add jenv directory to PATH
if err := env.AddToPath(exeDir); err != nil {
fmt.Printf("%s: Unable to modify PATH environment variable\n%s\n",
style.Error.Render("Error"),
style.Error.Render(err.Error()))
return
}
fmt.Printf("%s\n", style.Success.Render("✓ Successfully added jenv to PATH"))
fmt.Printf("%s: %s\n", style.Name.Render("Location"), style.Path.Render(exeDir))
fmt.Printf("\n%s\n", style.Info.Render("➜ Please restart your terminal to apply the changes"))
fmt.Printf("%s\n", style.Info.Render("➜ Then run 'jenv' to verify the installation"))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gitwcx/jenv.git
git@gitee.com:gitwcx/jenv.git
gitwcx
jenv
jenv
v1.3.0

Search