1 Star 0 Fork 0

walter/jenv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
root.go 2.66 KB
一键复制 编辑 原始数据 按行查看 历史
wuchenxi03 提交于 2025-11-01 12:22 +08:00 . update
package cmd
import (
"fmt"
"os"
"runtime"
"gitee.com/gitwcx/jenv/sdk/config"
style2 "gitee.com/gitwcx/jenv/sdk/style"
"gitee.com/gitwcx/jenv/sdk/sys"
"github.com/spf13/cobra"
)
var Version = "dev"
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "jenv",
Short: "A Java version manager",
Long: `Jenv is a command-line tool for managing multiple Java versions.
It allows you to easily switch between different Java versions,
add new Java installations, and manage your Java environment.
First time setup:
➜ Run 'jenv add-to-path' to add jenv to your system PATH
➜ Run 'jenv scan <dir>' to find and add Java installations
➜ Run 'jenv use <name>' to select a Java version`,
Version: Version,
}
func init() {
rootCmd.SetVersionTemplate(`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "version %s" .Version}}
Author: WhyWhatHow (https://github.com/WhyWhatHow)
Email: whywhathow.fun@gmail.com
License: Apache License 2.0
`)
// Check admin privileges - different logic for different platforms
if runtime.GOOS == "windows" {
// Windows always requires administrator privileges
if !sys.IsAdmin() {
fmt.Printf("%s: %s\n",
style2.Error.Render("Error"),
style2.Error.Render("Administrator privileges required"))
os.Exit(1)
}
} else {
// Linux/Unix: Only warn if not root, don't exit
if !sys.IsAdmin() {
fmt.Printf("%s: %s\n",
style2.Warning.Render("Warning"),
style2.Warning.Render("Running without root privileges. Some features may be limited."))
fmt.Printf("%s: %s\n",
style2.Info.Render("Info"),
style2.Info.Render("jenv will use user-level configuration and symlinks."))
}
}
// Initialize configuration system
cfg, err := config.GetInstance()
if err != nil {
fmt.Printf("%s: %s\n%s\n",
style2.Error.Render("Error"),
style2.Error.Render("Failed to initialize configuration"),
style2.Error.Render(err.Error()))
os.Exit(1)
}
// Apply saved theme if exists
if cfg.Theme != "" {
if theme, ok := style2.GetThemeByName(cfg.Theme); ok {
style2.ApplyTheme(theme)
}
}
// Check if jenv has been initialized
if !cfg.Initialized {
fmt.Printf("%s: %s\n",
style2.Warning.Render("Warning"),
style2.Warning.Render("jenv has not been initialized yet"))
fmt.Printf("%s: %s\n",
style2.Info.Render("Info"),
style2.Info.Render("Run 'jenv init' to set up jenv for first-time use"))
fmt.Println()
}
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gitwcx/jenv.git
git@gitee.com:gitwcx/jenv.git
gitwcx
jenv
jenv
v1.3.0

搜索帮助