-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: command gf build
does not accept file path from command line
#3417
Conversation
} else { | ||
mlog.Fatal("build file path is empty or main.go not found in current working directory") | ||
// Check and use Arg(2) as the file path. | ||
if !gfile.Exists(file) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我想是否判断一下file
为空,如果为空的话直接赋值cBuildDefaultFile
,后续流程直接使用变量file
就好?
if file == "" {
file = cBuildDefaultFile
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
按照您的设想,我调整代码进行测试
fmt.Println("------------ in --------------------")
g.Dump(in)
fmt.Println("------------ parser --------------------")
g.Dump(parser.GetArgAll())
if file == "" {
// file = parser.GetArg(2).String()
// // Check and use the main.go file.
// if gfile.Exists(cBuildDefaultFile) {
file = cBuildDefaultFile
// } else {
// mlog.Fatal("build file path is empty or main.go not found in current working directory")
// }
}
测试结果如下:
gf build -mod vendor \
-v 0.0.19 \
-n detect_hardware_os \
-a amd64,arm64 -s linux \
-p ./bin \
-e "-trimpath -ldflags '\
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.BuildTime=2024-03-26 09:51:48\" \
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitTime=2024-03-22 15:19:40\" \
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitHash=8ae7ca71106991a4268\" \
-X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitCount=19\" \
'" \
cmd/v3/main.go
// --out
------------ in --------------------
{
File: "",
Name: "detect_hardware_os",
Version: "0.0.19",
Arch: "amd64,arm64",
System: "linux",
Output: "",
Path: "./bin",
Extra: "-trimpath -ldflags '-X \"github.com/clh021/detect_hardware_os/service/cmd/version.BuildTime=2024-03-26 09:51:48\" -X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitTime=2024-03-22 15:19:40\" -X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitHash=8ae7ca71106991a4268\" -X \"github.com/clh021/detect_hardware_os/service/cmd/version.GitCount=19\" '",
Mod: "vendor",
Cgo: false,
VarMap: {},
PackSrc: "",
PackDst: "internal/packed/build_pack_data.go",
ExitWhenError: false,
DumpENV: false,
}
------------ parser --------------------
[
"/usr/local/hostbin/gf",
"build",
"cmd/v3/main.go",
]
再次尝试去除参数的测试
$ gf build -mod vendor \
-v 0.0.19 \
-n detect_hardware_os \
-a amd64,arm64 -s linux \
-p ./bin \
-e "-trimpath" \
cmd/v3/main.go
------------ in --------------------
{
File: "",
Name: "detect_hardware_os",
Version: "0.0.19",
Arch: "amd64,arm64",
System: "linux",
Output: "",
Path: "./bin",
Extra: "-trimpath",
Mod: "vendor",
Cgo: false,
VarMap: {},
PackSrc: "",
PackDst: "internal/packed/build_pack_data.go",
ExitWhenError: false,
DumpENV: false,
}
------------ parser --------------------
[
"gf",
"build",
"cmd/v3/main.go",
]
2024-03-26 09:57:09.283 start building...
2024-03-26 09:57:09.283 go build -o ./bin/0.0.19/linux_amd64/detect_hardware_os -mod=vendor -trimpath main.go
2024-03-26 09:57:09.288 failed to build, os:linux, arch:amd64, error:
cannot find module providing package main.go: import lookup disabled by -mod=vendor
you may use command option "--debug" to enable debug info and check the details
去除所有 -e 参数的测试
$ gf build -mod vendor \
-v 0.0.19 \
-n detect_hardware_os \
-a amd64,arm64 -s linux \
-p ./bin \
cmd/v3/main.go
------------ in --------------------
{
File: "cmd/v3/main.go",
Name: "detect_hardware_os",
Version: "0.0.19",
Arch: "amd64,arm64",
System: "linux",
Output: "",
Path: "./bin",
Extra: "",
Mod: "vendor",
Cgo: false,
VarMap: {},
PackSrc: "",
PackDst: "internal/packed/build_pack_data.go",
ExitWhenError: false,
DumpENV: false,
}
------------ parser --------------------
[
"gf",
"build",
"cmd/v3/main.go",
]
2024-03-26 09:57:21.770 start building...
经测试,前面的 parser.GetArg(2).String() 目前来说是必要的。
parser 的参数更为可靠。只要出现了 -e 参数就会丢失 File 参数。(这让我想到了我此前发现的另一个问题,命令行参数解析的部分太过复杂,我没能找出具体原因)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我多测试了一下,发现只要把 cmd/v3/main.go
的参数放在所有选项的前面 in.File
就可以接收到参数。
换句话说,要求参数必须在所有选项的前面。这和我平时用 go build
的习惯略有差异。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,感谢你的验证,我来看看呢。
gf build
does not accept file path from command line
问题描述:
经多次测试只要添加 -X 参数就会出现这个问题。
所以看了下源码:
源码中只判断了 cBuildDefaultFile 是否存在,存在就用默认的,不存在就退出了。
这样前面
file = parser.GetArg(2).String()
这一行代码就失去了存在的意义。本次 PR 适应没有 -X 参数和 有 -X 参数的情况。