Skip to content

Commit cd46300

Browse files
authored
internal/cmd/pdatagen: add support for specifying a work dir (#13890)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR adds a new flag (`-C path`) to the `pdatagen` utility, which allows specifying the workdir for the tool. Currently `pdatagen` expects to find the respective files in`pdata` directory of the `$cwd`. When `pdatagen` is called outside of the `$repo_root` the tool fails, because it cannot find `pdata` and panics. This PR adds an optional `-C path` flag, which would allow `pdatagen` to use an alternative workdir, and also be able to be called outside of the `$repo_root` directory. See #13844 for more details about this PR and the proposed changes. The commit from this PR have been extracted from #13844 . <!-- Issue number if applicable --> #### Link to tracking issue <!--Describe what testing was performed and which tests were added.--> #### Testing <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.-->
1 parent 3b135fb commit cd46300

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

internal/cmd/pdatagen/main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44
package main
55

66
import (
7+
"flag"
8+
"fmt"
9+
"os"
10+
711
"go.opentelemetry.io/collector/internal/cmd/pdatagen/internal/pdata"
812
)
913

10-
func check(e error) {
14+
// checkErr prints the given error and exits when e is non-nil.
15+
func checkErr(e error) {
1116
if e != nil {
12-
panic(e)
17+
fmt.Println(e)
18+
os.Exit(1)
1319
}
1420
}
1521

1622
func main() {
23+
var workdir string
24+
flag.StringVar(&workdir, "C", ".", "set work directory")
25+
flag.Parse()
26+
27+
checkErr(os.Chdir(workdir))
1728
for _, fp := range pdata.AllPackages {
18-
check(fp.GenerateFiles())
19-
check(fp.GenerateTestFiles())
20-
check(fp.GenerateInternalFiles())
21-
check(fp.GenerateInternalTestsFiles())
29+
checkErr(fp.GenerateFiles())
30+
checkErr(fp.GenerateTestFiles())
31+
checkErr(fp.GenerateInternalFiles())
32+
checkErr(fp.GenerateInternalTestsFiles())
2233
}
2334
}

0 commit comments

Comments
 (0)