Skip to content

Commit

Permalink
纯工具模式
Browse files Browse the repository at this point in the history
  • Loading branch information
陈壁浩 committed Jul 18, 2024
1 parent e47ade2 commit c84ae1c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 30 deletions.
2 changes: 2 additions & 0 deletions docs/src/guide/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ clone git 地址:`[email protected]/chenbihao/gob.git`

下载地址: [Releases](https://github.com/chenbihao/gob/releases)

在 gob 目录中运行命令 `go run main.go build self`

## 初始化项目

使用命令 `gob new` 在当前目录创建子项目
Expand Down
22 changes: 10 additions & 12 deletions framework/command/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,16 @@ var appStartCommand = &cobra.Command{
}
// 父进程直接打印启动成功信息,不做任何操作
if child != nil {
fmt.Println("成功启动进程:", processName)
fmt.Println("进程pid:", child.Pid)
fmt.Println("基础路径:", appService.BaseFolder())
fmt.Println("配置路径:", appService.ConfigFolder())
fmt.Println("日志路径:", appService.LogFolder())
fmt.Println("运行路径:", appService.RuntimeFolder())
fmt.Println("成功启动进程:", processName+" 进程pid:", child.Pid)
showAppAddress := appAddress
if strings.HasPrefix(appAddress, ":") {
showAppAddress = "http://localhost" + showAppAddress
}
fmt.Println("监听地址:", showAppAddress)
fmt.Println("基础路径:", appService.BaseFolder())
fmt.Println("日志路径:", appService.LogFolder())
fmt.Println("运行路径:", appService.RuntimeFolder())
fmt.Println("配置路径:", appService.ConfigFolder())
return nil
}
defer cntxt.Release()
Expand All @@ -268,17 +267,16 @@ var appStartCommand = &cobra.Command{
}

util.SetProcessTitle(processName)
fmt.Println("成功启动进程:", processName)
fmt.Println("进程pid:", content)
fmt.Println("基础路径:", appService.BaseFolder())
fmt.Println("配置路径:", appService.ConfigFolder())
fmt.Println("日志路径:", appService.LogFolder())
fmt.Println("运行路径:", appService.RuntimeFolder())
fmt.Println("成功启动进程:", processName+" 进程pid:", content)
showAppAddress := appAddress
if strings.HasPrefix(appAddress, ":") {
showAppAddress = "http://localhost" + showAppAddress
}
fmt.Println("监听地址:", showAppAddress)
fmt.Println("基础路径:", appService.BaseFolder())
fmt.Println("日志路径:", appService.LogFolder())
fmt.Println("运行路径:", appService.RuntimeFolder())
fmt.Println("配置路径:", appService.ConfigFolder())
if err := startAppServe(container, server); err != nil {
fmt.Println(err)
}
Expand Down
1 change: 0 additions & 1 deletion framework/command/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// AddKernelCommands will add all command/* to root command
func AddKernelCommands(root *cobra.Command) {
InitCronCommands(root)

root.AddCommand(initAppCommand()) // 挂载 app 命令
root.AddCommand(initCronCommand()) // 挂载 cron 命令
root.AddCommand(initEnvCommand()) // 挂载 env 命令
Expand Down
11 changes: 11 additions & 0 deletions framework/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type Container interface {
// Bind 绑定一个服务提供者,如果关键字凭证已经存在,会进行替换操作,返回 error
Bind(provider ServiceProvider) error

// MustBind 绑定一个服务提供者,如果关键字凭证已经存在,会进行替换操作,如果绑定失败,那么会 panic。
MustBind(provider ServiceProvider)

// IsBind 关键字凭证是否已经绑定服务提供者
IsBind(key string) bool

Expand Down Expand Up @@ -72,6 +75,14 @@ func (container *GobContainer) Bind(provider ServiceProvider) error {
return nil
}

// MustBind 将服务容器和关键字做了绑定
func (container *GobContainer) MustBind(provider ServiceProvider) {
err := container.Bind(provider)
if err != nil {
panic(err)
}
}

func (container *GobContainer) IsBind(key string) bool {
return container.findServiceProvider(key) != nil
}
Expand Down
24 changes: 21 additions & 3 deletions framework/provider/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package config
import (
"bytes"
"fmt"
"github.com/chenbihao/gob/framework/util"
"gopkg.in/yaml.v3"
"log"
"os"
Expand All @@ -25,7 +26,8 @@ import (
type ConfigService struct {
c framework.Container // 容器
folder string // 文件夹
keyDelim string // 路径的分隔符,默认为点
keyDelim string // key 路径的分隔符,默认为点
toolMode bool // 工具运行模式(通过 go install 安装至 $GOPATH/bin )
lock sync.RWMutex // 配置文件读写锁
envMaps map[string]string // 所有的环境变量
confMaps map[string]interface{} // 配置文件结构,key为文件名
Expand All @@ -39,22 +41,38 @@ func NewConfigService(params ...interface{}) (interface{}, error) {
container := params[0].(framework.Container)
envFolder := params[1].(string)
envMaps := params[2].(map[string]string)
toolMode := false

// 纯工具模式 ( 兼容 go install )
if envMaps["runMode"] == "tool" {
toolMode = true
}

// 检查文件夹是否存在
if _, err := os.Stat(envFolder); os.IsNotExist(err) {
return nil, errors.New("folder " + envFolder + " not exist: " + err.Error())
if toolMode || util.CheckBinaryFileInTheGOPATH() {
err = nil
toolMode = true
log.Println("纯工具模式,未能加载配置!")
} else {
return nil, errors.New("folder " + envFolder + " not exist: " + err.Error())
}
}

// 实例化
gobConf := &ConfigService{
c: container,
folder: envFolder,
keyDelim: ".",
toolMode: toolMode,
envMaps: envMaps,
confMaps: map[string]interface{}{},
confRaws: map[string][]byte{},
keyDelim: ".",
lock: sync.RWMutex{},
}
if toolMode {
return gobConf, nil
}

// 读取每个文件
files, err := os.ReadDir(envFolder)
Expand Down
20 changes: 19 additions & 1 deletion framework/util/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
)

func IsWindows() bool {
Expand All @@ -18,13 +19,30 @@ func IsNotWindows() bool {
func GetExecDirectory() string {
file, err := os.Getwd()
if err == nil {
//return file + "/"
return file + string(os.PathSeparator) // Error: daemon: Non-POSIX OS is not supported
}
fmt.Println("获取执行目录失败:err=", err.Error())
return ""
}

// GetBinaryFileDirectory 获取当前执行程序的二进制文件目录
func GetBinaryFileDirectory() string {
file, err := os.Executable()
if err == nil {
return file + string(os.PathSeparator) // Error: daemon: Non-POSIX OS is not supported
}
fmt.Println("获取二进制文件目录失败:err=", err.Error())
return ""
}

// 检查当前执行程序二进制是否在gopath
func CheckBinaryFileInTheGOPATH() bool {
var homePath = os.Getenv("HOMEPATH")
binaryPath := GetBinaryFileDirectory()
goBinPath := filepath.Join(homePath, "go", "bin")
return strings.Contains(binaryPath, goBinPath)
}

// GetRootDirectory 获取当前项目根目录(根据 .go-root 文件识别)
func GetRootDirectory() (string, error) {
executable, err := os.Getwd()
Expand Down
6 changes: 1 addition & 5 deletions framework/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import (
func Exists(path string) bool {
// os.Stat 获取文件信息
if _, err := os.Stat(path); err != nil {
if os.IsExist(err) {
return true
}
return false
return os.IsExist(err)
}
return true
}
Expand Down Expand Up @@ -81,7 +78,6 @@ func SubDir(folder string) ([]string, error) {
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {

// Get the data
resp, err := http.Get(url)
if err != nil {
Expand Down
14 changes: 6 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ import (
"github.com/chenbihao/gob/framework/provider/sls"
"github.com/chenbihao/gob/framework/provider/ssh"
"github.com/chenbihao/gob/framework/provider/trace"
"os"
)

func main() {

// 如果仅当做脚手架来用的话,仅挂载new命令,不绑定各类服务提供者(暂时解决构建后无配置目录问题)
args := os.Args
if len(args) > 1 && args[1] == "new" {
container := framework.NewGobContainer()
_ = console.RunRootCommand(container, true)
return
}
//args := os.Args
//if len(args) > 1 && args[1] == "new" {
// container := framework.NewGobContainer()
// _ = console.RunRootCommand(container, true)
// return
//}

// 初始化服务容器
container := framework.NewGobContainer()
Expand Down

0 comments on commit c84ae1c

Please sign in to comment.