提交 5ff37d02 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

print a msg for permission denied

上级 606d74b1
......@@ -5,6 +5,7 @@
"id": "windows_permission",
"translation": "Windows系统下,需以管理员身份打开cmd命令行窗口。"
},
{
"id": "for_example",
"translation": "e.g. %s"
......
......@@ -18,7 +18,7 @@ func main() {
go func() {
<-c
cleanup()
os.Exit(1)
os.Exit(0)
}()
for {
......
......@@ -17,10 +17,13 @@ import (
"gopkg.in/ini.v1"
"os"
"reflect"
"syscall"
)
func InitConfig() {
vari.ZtfDir = fileUtils.GetZtfDir()
CheckConfigPermission()
constant.ConfigFile = vari.ZtfDir + constant.ConfigFile
vari.Config = getInst()
......@@ -38,7 +41,6 @@ func InitScreenSize() {
}
func SaveConfig(conf model.Config) error {
fileUtils.MkDirIfNeeded(fileUtils.GetZtfDir() + "conf")
conf.Version = constant.ConfigVer
......@@ -46,7 +48,12 @@ func SaveConfig(conf model.Config) error {
cfg := ini.Empty()
cfg.ReflectFrom(&conf)
cfg.SaveTo(constant.ConfigFile)
err := cfg.SaveTo(constant.ConfigFile)
if err != nil {
logUtils.PrintToWithColor(
fmt.Sprintf("Permission denied to open %s for write.", vari.ZtfDir), color.FgRed)
os.Exit(0)
}
vari.Config = ReadCurrConfig()
return nil
......@@ -89,14 +96,12 @@ func ReadCurrConfig() model.Config {
}
func getInst() model.Config {
isSet := len(os.Args) > 1 && (os.Args[1] == "set" || os.Args[1] == "-set")
if !isSet {
CheckConfig()
isSetAction := len(os.Args) > 1 && (os.Args[1] == "set" || os.Args[1] == "-set")
if !isSetAction {
CheckConfigReady()
}
configFile := constant.ConfigFile
ini.MapTo(&vari.Config, configFile)
ini.MapTo(&vari.Config, constant.ConfigFile)
if vari.Config.Version != constant.ConfigVer { // old config file, re-init
if vari.Config.Language != "en" && vari.Config.Language != "zh" {
......@@ -109,9 +114,17 @@ func getInst() model.Config {
return vari.Config
}
func CheckConfig() {
configPath := constant.ConfigFile
if !fileUtils.FileExist(configPath) {
func CheckConfigPermission() {
err := syscall.Access(vari.ZtfDir, syscall.O_RDWR)
if err != nil {
logUtils.PrintToWithColor(
fmt.Sprintf("Permission denied to open %s for write. Please change the dir add it to PATH environment variable.", vari.ZtfDir), color.FgRed)
os.Exit(0)
}
}
func CheckConfigReady() {
if !fileUtils.FileExist(constant.ConfigFile) {
InputForSet()
}
}
......
......@@ -134,13 +134,14 @@ func GetZtfDir() string { // where ztf command in
var dir string
arg1 := strings.ToLower(os.Args[0])
if strings.Index(arg1, "build") > -1 || strings.Index(arg1, "bin") > -1 || strings.Index(arg1, "temp") > -1 { // debug
dir, _ = os.Getwd()
} else {
p, _ := exec.LookPath(os.Args[0]) // filepath.Abs(filepath.Dir(os.Args[0]))
name := filepath.Base(arg1)
if strings.Index(name, "ztf") == 0 {
p, _ := exec.LookPath(os.Args[0])
if strings.Index(p, string(os.PathSeparator)) > -1 {
dir = p[:strings.LastIndex(p, string(os.PathSeparator))]
}
} else { // debug
dir, _ = os.Getwd()
}
dir = UpdateDir(dir)
......@@ -149,11 +150,6 @@ func GetZtfDir() string { // where ztf command in
return dir
}
func GetCurrDir() string { // where you run command from
dir, _ := os.Getwd()
return UpdateDir(dir)
}
func GetLogDir() string {
path := vari.ZtfDir + constant.LogDir
......
......@@ -32,7 +32,7 @@ func main() {
go func() {
<-channel
cleanup()
os.Exit(1)
os.Exit(0)
}()
flagSet = flag.NewFlagSet("atf", flag.ContinueOnError)
......@@ -164,5 +164,4 @@ func init() {
func cleanup() {
color.Unset()
//color.Set(color.FgWhite)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册