-
Notifications
You must be signed in to change notification settings - Fork 13
feat: service message flag support #574
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
5 commits
Select commit
Hold shift + click to select a range
606ac34
feat: service message flag support
chen-keinan f440567
test: add service message test
chen-keinan 0ad7508
refactor: print struct and func name
chen-keinan fc4decb
feat: add service msg example
chen-keinan 868e72a
test: fix mock
chen-keinan 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
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,78 @@ | ||
| package servicemessages | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/OctopusDeploy/cli/pkg/constants" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| type Provider interface { | ||
| ServiceMessage(messageName string, values any) | ||
| } | ||
|
|
||
| type provider struct { | ||
| printer *OutputPrinter | ||
| } | ||
|
|
||
| func NewProvider(printer *OutputPrinter) Provider { | ||
| return &provider{ | ||
| printer: printer, | ||
| } | ||
| } | ||
|
|
||
| func (p *provider) ServiceMessage(messageName string, values any) { | ||
| serviceMessageEnabled := viper.GetBool(constants.FlagEnableServiceMessages) | ||
| if !serviceMessageEnabled { | ||
| return | ||
| } | ||
|
|
||
| teamCityEnvVar := os.Getenv("TEAMCITY_VERSION") | ||
| if teamCityEnvVar == "" { | ||
| p.printer.Error("service messages are only supported in TeamCity builds") | ||
| return | ||
| } | ||
|
|
||
| switch t := values.(type) { | ||
| case string: | ||
| p.printer.Info(fmt.Sprintf("##teamcity[%s %s]\n", messageName, t)) | ||
| case map[string]string: | ||
| mapMsg := p.mapToStringMsg(t, messageName) | ||
| p.printer.Info(mapMsg) | ||
| default: | ||
| p.printer.Error("Unsupported service message value type") | ||
| } | ||
| } | ||
|
|
||
| type OutputPrinter struct { | ||
| Out io.Writer | ||
| Err io.Writer | ||
| } | ||
|
|
||
| func NewOutputPrinter(out io.Writer, err io.Writer) *OutputPrinter { | ||
| return &OutputPrinter{ | ||
| Out: out, | ||
| Err: err, | ||
| } | ||
| } | ||
|
|
||
| func (p *OutputPrinter) Info(msg string) { | ||
| fmt.Fprint(p.Out, msg) | ||
| } | ||
|
|
||
| func (p *OutputPrinter) Error(msg string) { | ||
| fmt.Fprint(p.Err, msg) | ||
| } | ||
|
|
||
| func (p *provider) mapToStringMsg(m map[string]string, messageName string) string { | ||
| var builder strings.Builder | ||
| builder.WriteString(fmt.Sprintf("##teamcity[%s", messageName)) | ||
| for key, value := range m { | ||
| builder.WriteString(fmt.Sprintf(" %s=%s", key, value)) | ||
| } | ||
| builder.WriteString("]") | ||
| return builder.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package servicemessages | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/OctopusDeploy/cli/pkg/constants" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func TestServiceMessage(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| servicemessages bool | ||
| teamCityEnv bool | ||
| messsageName string | ||
| key string | ||
| value any | ||
| stdout *bytes.Buffer | ||
| stderr *bytes.Buffer | ||
| want string | ||
| wantErr string | ||
| }{ | ||
| {"service message flag is not enabled", false, false, "testMessage", "key1", "value1", &bytes.Buffer{}, &bytes.Buffer{}, "", ""}, | ||
| {"service message enabled with teamcity envvar and map value", true, true, "testMessage", "key1", map[string]string{"key": "value"}, &bytes.Buffer{}, &bytes.Buffer{}, "##teamcity[testMessage key=value]", ""}, | ||
| {"service message enabled without teamcity envvar", true, false, "testMessage", "key1", "value1", &bytes.Buffer{}, &bytes.Buffer{}, "", "service messages are only supported in TeamCity builds"}, | ||
| {"service message enabled with teamcity envvar and string value", true, true, "testMessage", "key1", "value", &bytes.Buffer{}, &bytes.Buffer{}, "##teamcity[testMessage value]\n", ""}, | ||
| {"service message enabled with teamcity envvar and unsupported value", true, true, "testMessage", "key1", []string{"dsdsd"}, &bytes.Buffer{}, &bytes.Buffer{}, "", "Unsupported service message value type"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| setupArgs(t, constants.FlagEnableServiceMessages, tt.servicemessages) | ||
| setupEnvVar(t, "TEAMCITY_VERSION", "2021.1", tt.teamCityEnv) | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| NewProvider(NewOutputPrinter(tt.stdout, tt.stderr)).ServiceMessage(tt.messsageName, tt.value) | ||
| if tt.want != "" { | ||
| got := tt.stdout.String() | ||
| if got != tt.want { | ||
| t.Errorf("Expected output:\n%s\nGot:\n%s", tt.want, got) | ||
| } | ||
| } | ||
| if tt.wantErr != "" { | ||
| e := tt.stderr.String() | ||
| if e != tt.wantErr { | ||
| t.Errorf("Expected error output:\n%s\nGot:\n%s", tt.wantErr, e) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func setupArgs(t *testing.T, key string, value bool) { | ||
| viper.Reset() | ||
| viper.Set(constants.FlagEnableServiceMessages, value) | ||
| } | ||
|
|
||
| func setupEnvVar(t *testing.T, key, value string, set bool) { | ||
| if set { | ||
| t.Setenv(key, value) | ||
| } else { | ||
| t.Setenv(key, "") | ||
| } | ||
| } |
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
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.
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.
added for usage example