-
Notifications
You must be signed in to change notification settings - Fork 8
Rps 10094 show account and project information #103
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
az-smartling
merged 4 commits into
master
from
RPS-10094_Show_account_and_project_information
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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,81 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
|
|
||
| output "github.com/Smartling/smartling-cli/output/projects" | ||
| "github.com/Smartling/smartling-cli/services/helpers/rlog" | ||
| projectconfig "github.com/Smartling/smartling-cli/services/projects/config" | ||
|
|
||
| "golang.org/x/term" | ||
| ) | ||
|
|
||
| // ShowConfigBanner prints the --show-config banner to stdout when the | ||
| // flag is set, and — only on an interactive terminal — prompts the operator | ||
| // with "Continue? [y/N]:" before letting the command proceed. | ||
| func ShowConfigBanner(ctx context.Context) error { | ||
| if !showConfig || isInit { | ||
| return nil | ||
| } | ||
| config, err := Config() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read config: %w", err) | ||
| } | ||
| var extendedConfig projectconfig.Extended | ||
| extendedConfig.InjectConfig(config) | ||
| client, err := Client(ctx) | ||
| if err != nil { | ||
| rlog.Errorf("failed to create API client: %s", err) | ||
| } else if config.ProjectID != "" { | ||
| projectDetails, err := client.GetProjectDetails(ctx, config.ProjectID) | ||
| if err != nil { | ||
| rlog.Errorf("failed to fetch project details: %s", err) | ||
| } | ||
| if projectDetails != nil { | ||
| extendedConfig.InjectProject(*projectDetails) | ||
| } | ||
| } | ||
| if !showConfigAndMaybePrompt(extendedConfig, os.Stdout, os.Stderr, os.Stdin, stdinIsTerminal()) { | ||
|
az-smartling marked this conversation as resolved.
|
||
| return errors.New("operation aborted by user") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func showConfigAndMaybePrompt(config projectconfig.Extended, stdoutW, stderrW io.Writer, stdinR io.Reader, stdinIsTerminal bool) bool { | ||
| _ = output.RenderPlain(stdoutW, config) | ||
|
|
||
| if !stdinIsTerminal { | ||
| return true | ||
| } | ||
|
|
||
| _, _ = fmt.Fprint(stderrW, "Continue? [y/N]: ") | ||
| if !confirmContinue(stdinR) { | ||
| _, _ = fmt.Fprintln(stderrW, "aborted") | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func confirmContinue(r io.Reader) bool { | ||
| scanner := bufio.NewScanner(r) | ||
| if !scanner.Scan() { | ||
| return false | ||
| } | ||
| switch strings.ToLower(strings.TrimSpace(scanner.Text())) { | ||
| case "y", "yes": | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // stdinIsTerminal reports whether stdin is connected to an interactive terminal. | ||
| func stdinIsTerminal() bool { | ||
| return term.IsTerminal(int(os.Stdin.Fd())) | ||
| } | ||
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,94 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| projectconfig "github.com/Smartling/smartling-cli/services/projects/config" | ||
| ) | ||
|
|
||
| func testExtendedConfig() projectconfig.Extended { | ||
| return projectconfig.Extended{ | ||
| ProjectID: "p-789", | ||
| AccountUID: "a-456", | ||
| Name: "Acme Localization", | ||
| Locale: "en-US: English (United States)", | ||
| Status: "active", | ||
| UserID: "u-123", | ||
| ConfigFile: "/tmp/smartling.yml", | ||
| Sources: "project=flag account=config user=env", | ||
| } | ||
| } | ||
|
|
||
| func TestConfirmContinue(t *testing.T) { | ||
| tests := []struct { | ||
| name, input string | ||
| want bool | ||
| }{ | ||
| {"y lowercase", "y\n", true}, | ||
| {"Y uppercase", "Y\n", true}, | ||
| {"yes", "yes\n", true}, | ||
| {"YES", "YES\n", true}, | ||
| {"n", "n\n", false}, | ||
| {"N", "N\n", false}, | ||
| {"no", "no\n", false}, | ||
| {"empty line", "\n", false}, | ||
| {"eof", "", false}, | ||
| {"whitespace around y", " y \n", true}, | ||
| {"whitespace around yes", " yes \n", true}, | ||
| {"unrelated text", "foo\n", false}, | ||
| {"y-prefixed word yeti", "yeti\n", false}, | ||
| {"y-prefixed word yikes", "yikes\n", false}, | ||
| {"yy", "yy\n", false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := confirmContinue(strings.NewReader(tt.input)); got != tt.want { | ||
| t.Errorf("confirmContinue(%q) = %v, want %v", tt.input, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestShowConfigAndMaybePrompt_NonTTY(t *testing.T) { | ||
| var stdout, stderr bytes.Buffer | ||
| got := showConfigAndMaybePrompt(testExtendedConfig(), &stdout, &stderr, strings.NewReader(""), false) | ||
|
|
||
| if !got { | ||
| t.Errorf("showConfigAndMaybePrompt should return true in non-TTY case, got false") | ||
| } | ||
| if !strings.Contains(stdout.String(), "Smartling CLI configuration:") { | ||
| t.Errorf("stdout should contain banner, got: %q", stdout.String()) | ||
| } | ||
| if stderr.String() != "" { | ||
| t.Errorf("stderr should be empty (no prompt) in non-TTY case, got: %q", stderr.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestShowConfigAndMaybePrompt_TTYConfirm(t *testing.T) { | ||
| var stdout, stderr bytes.Buffer | ||
| got := showConfigAndMaybePrompt(testExtendedConfig(), &stdout, &stderr, strings.NewReader("y\n"), true) | ||
|
|
||
| if !got { | ||
| t.Errorf("showConfigAndMaybePrompt should return true on 'y', got false") | ||
| } | ||
| if !strings.Contains(stdout.String(), "Smartling CLI configuration:") { | ||
| t.Errorf("stdout should contain banner, got: %q", stdout.String()) | ||
| } | ||
| if !strings.Contains(stderr.String(), "Continue?") { | ||
| t.Errorf("stderr should contain prompt, got: %q", stderr.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestShowConfigAndMaybePrompt_TTYAbort(t *testing.T) { | ||
| var stdout, stderr bytes.Buffer | ||
| got := showConfigAndMaybePrompt(testExtendedConfig(), &stdout, &stderr, strings.NewReader("n\n"), true) | ||
|
|
||
| if got { | ||
| t.Errorf("showConfigAndMaybePrompt should return false on 'n', got true") | ||
| } | ||
| if !strings.Contains(stderr.String(), "aborted") { | ||
| t.Errorf("stderr should contain 'aborted' on abort, got: %q", stderr.String()) | ||
| } | ||
| } |
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
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,56 @@ | ||
| package projects | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/Smartling/smartling-cli/services/helpers/table" | ||
| projectconfig "github.com/Smartling/smartling-cli/services/projects/config" | ||
| ) | ||
|
|
||
| func RenderTable(config projectconfig.Extended) error { | ||
| tableWriter := table.NewTableWriter(os.Stdout) | ||
| for _, row := range buildInfoRows(config) { | ||
| if _, err := fmt.Fprintf(tableWriter, "%s\t%s\n", row...); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return table.Render(tableWriter) | ||
| } | ||
|
|
||
| // RenderPlain writes a multi-line summary of the resolved configuration to w. | ||
| // Used by the --show-config persistent flag. Every line is prefixed with "> " | ||
| // so the banner is easy to grep out of mixed output if needed. | ||
| func RenderPlain(w io.Writer, config projectconfig.Extended) error { | ||
| lines := []string{ | ||
| "Smartling CLI configuration:", | ||
| fmt.Sprintf(" Config file: %s", config.ConfigFile), | ||
| fmt.Sprintf(" User: %s", config.UserID), | ||
| fmt.Sprintf(" Account: %s", config.AccountUID), | ||
| fmt.Sprintf(" Project: %s", config.ProjectID), | ||
| fmt.Sprintf(" Project Name: %s", config.Name), | ||
| fmt.Sprintf(" Locale: %s", config.Locale), | ||
| fmt.Sprintf(" Status: %s", config.Status), | ||
| fmt.Sprintf(" Sources: %s", config.Sources), | ||
| } | ||
| for _, line := range lines { | ||
| if _, err := fmt.Fprintf(w, "> %s\n", line); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func buildInfoRows(config projectconfig.Extended) [][]any { | ||
| return [][]any{ | ||
| {"ID", config.ProjectID}, | ||
| {"ACCOUNT", config.AccountUID}, | ||
| {"NAME", config.Name}, | ||
| {"LOCALE", config.Locale}, | ||
| {"STATUS", config.Status}, | ||
| {"USER", config.UserID}, | ||
| {"CONFIG", config.ConfigFile}, | ||
| {"SOURCES", config.Sources}, | ||
| } | ||
| } |
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.