2022-10-04 13:21:05 +08:00
|
|
|
//----------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
//
|
2023-02-19 23:21:47 +08:00
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2022-10-04 13:21:05 +08:00
|
|
|
//
|
|
|
|
//----------------------------------------
|
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
// 当前进程的命令行参数获取
|
|
|
|
|
|
|
|
package process
|
2022-10-04 13:21:05 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-31 18:00:34 +08:00
|
|
|
"github.com/energye/energy/v2/logger"
|
2022-10-04 13:21:05 +08:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
// Args
|
|
|
|
// 当前进程命令行参数解析实例
|
|
|
|
var Args = parse()
|
2022-10-04 13:21:05 +08:00
|
|
|
|
|
|
|
type _args struct {
|
2023-10-30 13:42:33 +08:00
|
|
|
isMain bool
|
|
|
|
size int
|
|
|
|
value map[string]PRCESS_TYPE
|
2022-10-04 13:21:05 +08:00
|
|
|
}
|
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
func parse() *_args {
|
2022-10-04 13:21:05 +08:00
|
|
|
var args = &_args{value: make(map[string]PRCESS_TYPE, len(os.Args))}
|
|
|
|
args.size = len(os.Args)
|
2023-10-30 13:42:33 +08:00
|
|
|
for _, v := range os.Args {
|
2022-10-04 13:21:05 +08:00
|
|
|
a := strings.Split(v, "=")
|
|
|
|
if len(a) == 2 {
|
|
|
|
args.value[strings.Replace(a[0], "--", "", 1)] = PRCESS_TYPE(a[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := args.value["type"]; ok {
|
|
|
|
args.isMain = v == PT_MAIN
|
|
|
|
} else {
|
|
|
|
args.isMain = true
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *_args) Size() int {
|
|
|
|
return m.size
|
|
|
|
}
|
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
// ProcessType
|
|
|
|
// 返回进程类型
|
2022-10-04 13:21:05 +08:00
|
|
|
func (m *_args) ProcessType() PRCESS_TYPE {
|
|
|
|
if v, ok := m.value["type"]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return PT_MAIN
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *_args) IsMain() bool {
|
|
|
|
return m.isMain
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *_args) IsRender() bool {
|
|
|
|
if v, ok := m.value["type"]; ok {
|
2022-10-28 16:49:35 +08:00
|
|
|
return v == PT_RENDERER
|
2022-10-04 13:21:05 +08:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *_args) IsGPU() bool {
|
|
|
|
if v, ok := m.value["type"]; ok {
|
|
|
|
return v == PT_GPU
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *_args) IsUtility() bool {
|
|
|
|
if v, ok := m.value["type"]; ok {
|
|
|
|
return v == PT_UTILITY
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
// Args
|
|
|
|
// 根据name返回指定的命令行参数值
|
2022-10-04 13:21:05 +08:00
|
|
|
func (m *_args) Args(name string) string {
|
|
|
|
if v, ok := m.value[name]; ok {
|
|
|
|
return string(v)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-05-31 17:41:14 +08:00
|
|
|
// Print
|
|
|
|
// 打印到控制台
|
2022-10-04 13:21:05 +08:00
|
|
|
func (m *_args) Print() {
|
2022-10-05 20:43:25 +08:00
|
|
|
logger.Debug("command line:", m.size)
|
2022-10-04 13:21:05 +08:00
|
|
|
for key, value := range m.value {
|
2023-05-31 17:41:14 +08:00
|
|
|
fmt.Printf("args = [%v = %v]\n", key, value)
|
2022-10-04 13:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PRCESS_TYPE string
|
|
|
|
|
|
|
|
const (
|
2023-05-31 17:41:14 +08:00
|
|
|
PT_MAIN PRCESS_TYPE = "" // main
|
|
|
|
PT_GPU = "gpu-process" // gpu
|
|
|
|
PT_UTILITY = "utility" // utility
|
|
|
|
PT_RENDERER = "renderer" // renderer
|
|
|
|
PT_DEVTOOLS = "devtools" //
|
2022-10-04 13:21:05 +08:00
|
|
|
)
|