-
Notifications
You must be signed in to change notification settings - Fork 57
feat(internal/librarian): add debug command with env subcommand #6576
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6065d82
feat(internal/librarian): add debug command with env subcommand
hj690 0fac391
gracefully handle error from Directory etc
hj690 8a19589
Merge remote-tracking branch 'upstream/main' into feat/env-command
hj690 f2b4128
Merge branch 'main' into feat/env-command
hj690 5aa9075
remove empty lines
hj690 b34c3fa
propogate writer error
hj690 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package librarian | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/googleapis/librarian/internal/cache" | ||
| "github.com/googleapis/librarian/internal/librarian/golang" | ||
| "github.com/googleapis/librarian/internal/librarian/java" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| // debugCommand returns the CLI command for librarian debugging tools. | ||
| func debugCommand() *cli.Command { | ||
| return &cli.Command{ | ||
| Name: "debug", | ||
| Usage: "various debugging commands", | ||
| UsageText: "librarian debug [command]", | ||
| Commands: []*cli.Command{ | ||
| envCommand(), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // envCommand returns the CLI command for printing the librarian environment. | ||
| func envCommand() *cli.Command { | ||
| return &cli.Command{ | ||
| Name: "env", | ||
| Usage: "print environment variables for the librarian command line interface.", | ||
| UsageText: "librarian debug env", | ||
| Description: `env prints the librarian interpretation of the environment it is run in. | ||
| This includes the resolved LIBRARIAN_CACHE and LIBRARIAN_BIN paths, | ||
| as well as the language-specific tool installation directories.`, | ||
| Action: func(ctx context.Context, cmd *cli.Command) error { | ||
| return runEnv(cmd.Root().Writer) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func runEnv(w io.Writer) error { | ||
| cacheDir := dirOrErr(cache.Directory()) | ||
| buildDir := dirOrErr(cache.BinDirectory()) | ||
| goToolsDir := dirOrErr(golang.InstallDir()) | ||
| javaToolsDir := dirOrErr(java.InstallDir()) | ||
| var b strings.Builder | ||
| fmt.Fprintf(&b, "LIBRARIAN_CACHE=%s\n", cacheDir) | ||
| fmt.Fprintf(&b, "LIBRARIAN_BIN=%s\n", buildDir) | ||
| fmt.Fprintln(&b) | ||
| fmt.Fprintln(&b, "Language-specific tool installation directories:") | ||
| fmt.Fprintf(&b, " golang: %s\n", goToolsDir) | ||
| fmt.Fprintf(&b, " java: %s\n", javaToolsDir) | ||
| _, err := io.WriteString(w, b.String()) | ||
| return err | ||
| } | ||
|
|
||
| // dirOrErr converts a directory path and potential error into a string. If an error | ||
| // occurred, it returns a formatted error string; otherwise, it returns the directory path. | ||
| func dirOrErr(dir string, err error) string { | ||
| if err != nil { | ||
| return fmt.Sprintf("<error: %v>", err) | ||
| } | ||
| return dir | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package librarian | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRunEnv(t *testing.T) { | ||
| cacheDir := t.TempDir() | ||
| binDir := t.TempDir() | ||
| t.Setenv("LIBRARIAN_CACHE", cacheDir) | ||
| t.Setenv("LIBRARIAN_BIN", binDir) | ||
| var buf bytes.Buffer | ||
| if err := runEnv(&buf); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got := buf.String() | ||
| wants := []string{ | ||
| fmt.Sprintf("LIBRARIAN_CACHE=%s", cacheDir), | ||
| fmt.Sprintf("LIBRARIAN_BIN=%s", binDir), | ||
| fmt.Sprintf("golang: %s", filepath.Join(binDir, "go_tools")), | ||
| fmt.Sprintf("java: %s", filepath.Join(binDir, "java_tools")), | ||
| } | ||
| for _, want := range wants { | ||
| if !strings.Contains(got, want) { | ||
| t.Errorf("runEnv() output missing %q\ngot:\n%s", want, got) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestRunEnv_Error(t *testing.T) { | ||
| // Unset environment variables to force path resolution errors. | ||
| t.Setenv("LIBRARIAN_CACHE", "") | ||
| t.Setenv("LIBRARIAN_BIN", "") | ||
| t.Setenv("HOME", "") | ||
| t.Setenv("XDG_CACHE_HOME", "") | ||
| var buf bytes.Buffer | ||
| if err := runEnv(&buf); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got := buf.String() | ||
| wants := []string{ | ||
| "LIBRARIAN_CACHE=<error:", | ||
|
hj690 marked this conversation as resolved.
|
||
| "LIBRARIAN_BIN=<error:", | ||
| "golang: <error:", | ||
| "java: <error:", | ||
| } | ||
| for _, want := range wants { | ||
| if !strings.Contains(got, want) { | ||
| t.Errorf("runEnv() output missing %q\ngot:\n%s", want, got) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.