47 lines
896 B
Go
47 lines
896 B
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
|
|
netmod "netcli/internal/net"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
var rootCmd = &cobra.Command{
|
|
Use: "netctl",
|
|
Short: "网络切换工具",
|
|
Long: "netctl 是一个用于切换网卡静态IP/DHCP的 CLI 工具",
|
|
}
|
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
// This is called by main.main().
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
// 通用命令运行函数
|
|
func run(args ...string) {
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
out, err := cmd.CombinedOutput()
|
|
fmt.Println(string(out))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// 判断当前程序是否管理员
|
|
func IsAdmin() bool {
|
|
return netmod.IsAdmin()
|
|
}
|
|
|
|
// 提升管理员权限
|
|
func RunAsAdmin() error {
|
|
return netmod.RunAsAdmin()
|
|
}
|