This commit is contained in:
gaoshuaixing
2023-09-25 19:02:07 +08:00
parent 7d9df5875a
commit 196e2dbcce
10 changed files with 129 additions and 40 deletions

View File

@@ -82,7 +82,7 @@ module.exports = {
go: {
directory: './go',
cmd: 'go',
args: ['run', '-tags=fts5', './main.go', '--wd=../', '--env=dev', '--port=6789'],
args: ['run', './main.go', '--ee-env=dev'],
},
},
};

View File

@@ -3,6 +3,7 @@ package eapp
import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"os/exec"
@@ -10,7 +11,7 @@ import (
"path/filepath"
"strings"
"electron-egg/elog"
"electron-egg/eerror"
"electron-egg/eutil"
)
@@ -19,31 +20,46 @@ const (
)
var (
ENV = "dev" // 'dev' 'prod'
ENV = "prod" // 'dev' 'prod'
// progressBar float64 // 0 ~ 100
// progressDesc string // description
HttpServer = false
AppName = ""
)
var (
BaseDir, _ = os.Getwd()
HomeDir string // electron-egg home directory
PublicDir string // electron-egg public directory
LogDir string // electron-egg logs directory
UserHomeDir string // OS user home directory
BaseDir, _ = os.Getwd()
HomeDir string // electron-egg home directory
PublicDir string // electron-egg public directory
UserHomeDir string // OS user home directory
AppUserDataDir string // electron app.getPath('userData')
)
func init() {
HomeDir = filepath.Join(BaseDir, "..")
PublicDir = filepath.Join(HomeDir, "public")
UserHomeDir, _ = getUserHomeDir()
}
func New() {
fmt.Println("new electron-egg for go")
func Run() {
fmt.Println("result:")
logger := elog.GetLogger()
eeEnv := flag.String("ee-env", "prod", "dev/prod")
eeName := flag.String("ee-name", "", "app name")
eeAppUserData := flag.String("ee-app-user-data", "", "The folder where you store your application configuration files")
flag.Parse()
ENV = *eeEnv
AppName = *eeName
AppUserDataDir = *eeAppUserData
if AppName == "" {
eerror.Throw("The software ee-name must be set")
}
fmt.Println("ENV:", ENV)
fmt.Println("AppName:", AppName)
fmt.Println("AppUserDataDir:", AppUserDataDir)
initDirectory()
//logger := elog.GetLogger()
//logger := elog.CreateLogger()
logger.Infof("hconf example success tttt")
// logger.Infof("hconf example success tttt")
}
// Pwd gets the path of current working directory.
@@ -54,6 +70,40 @@ func Pwd() string {
return filepath.Dir(pwd)
}
func initDirectory() {
HomeDir = filepath.Join(BaseDir, "..")
PublicDir = filepath.Join(HomeDir, "public")
UserHomeDir, _ = getUserHomeDir()
userHomeConfDir := filepath.Join(UserHomeDir, ".config", AppName)
//workDataConf := filepath.Join(userHomeConfDir, "workdata.json")
if !eutil.FileIsExist(userHomeConfDir) {
if err := os.MkdirAll(userHomeConfDir, 0755); err != nil && !os.IsExist(err) {
errMsg := fmt.Sprintf("create user home conf folder [%s] failed: %s", userHomeConfDir, err)
eerror.Throw(errMsg)
}
}
logDir := filepath.Join(HomeDir, "logs")
if ENV == "prod" {
logDir = filepath.Join(AppUserDataDir, "logs")
if AppUserDataDir != "" && eutil.FileIsExist(AppUserDataDir) {
logDir = filepath.Join(AppUserDataDir, "logs")
}
}
if !eutil.FileIsExist(logDir) {
if err := os.MkdirAll(logDir, 0755); err != nil && !os.IsExist(err) {
errMsg := fmt.Sprintf("create logs folder [%s] failed: %s", logDir, err)
eerror.Throw(errMsg)
}
}
fmt.Println("HomeDir:", HomeDir)
fmt.Println("PublicDir:", PublicDir)
fmt.Println("UserHomeDir:", UserHomeDir)
fmt.Println("userHomeConfDir:", userHomeConfDir)
fmt.Println("logDir:", logDir)
}
func getUserHomeDir() (string, error) {
user, err := user.Current()
if nil == err {

7
go/eerror/code.go Normal file
View File

@@ -0,0 +1,7 @@
package eerror
const (
ErrorAppNameIsEmpty = 1001
ErrorCreateUserHomeConfDir = 1002
ErrorCreateLogDir = 1003
)

28
go/eerror/error.go Normal file
View File

@@ -0,0 +1,28 @@
package eerror
import (
"fmt"
"os"
E "github.com/pkg/errors"
)
func Throw(msg string) {
throw(msg, nil, 0)
}
func ThrowWrap(msg string, err error) {
throw(msg, err, 0)
}
func throw(msg string, err error, code int) {
var errInfo error
if err != nil {
errInfo = E.Wrap(err, msg)
} else {
errInfo = E.New(msg)
}
fmt.Printf("Error: %+v", errInfo)
os.Exit(code)
}

View File

@@ -1,15 +0,0 @@
package ehelper
import (
"fmt"
"os"
"strings"
)
func GetEnv() {
envs := os.Environ()
for _, env := range envs {
cache := strings.Split(env, "=")
fmt.Printf("%v = %v\n", cache[0], cache[1])
}
}

View File

@@ -2,6 +2,7 @@ package elog
import (
"fmt"
stdlog "log"
"os"
"path/filepath"
@@ -11,10 +12,11 @@ import (
)
var (
LogDir string // electron-egg logs directory
LogName = "ee-go.log"
LogPath string
zlogger *zap.Logger
logger *zap.SugaredLogger
LogPath string
LogName = "ee-go.log"
)
type LogConfig struct {
@@ -26,12 +28,17 @@ type LogConfig struct {
}
func init() {
Mode := "dev"
if Mode == "dev" {
LogPath = filepath.Join(WorkingDir, "logs", LogName)
} else {
LogPath = filepath.Join(TempDir, LogName)
dir, err := os.Getwd()
if err != nil {
stdlog.Printf("get current directory failed: %s", err)
dir = "./"
}
LogDir = dir
LogPath = filepath.Join(LogDir, LogName)
}
func SetLogPath(path string) {
LogPath = path
}
// 负责设置 encoding 的日志格式

9
go/eutil/file.go Normal file
View File

@@ -0,0 +1,9 @@
package eutil
import "os"
func FileIsExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}

View File

@@ -4,6 +4,7 @@ go 1.20
require (
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil/v3 v3.23.8
go.uber.org/zap v1.26.0
)

View File

@@ -12,6 +12,8 @@ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=

View File

@@ -5,5 +5,5 @@ import (
)
func main() {
eapp.Run()
eapp.New()
}