forked from wtfutil/wtf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84763f3
commit 9f94e8c
Showing
10 changed files
with
436 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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,110 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/logrusorgru/aurora" | ||
"github.com/olebedev/config" | ||
"github.com/wtfutil/wtf/support" | ||
) | ||
|
||
const exitMessageHeader = ` | ||
____ __ ____ .___________. _______ | ||
\ \ / \ / / | || ____| | ||
\ \/ \/ / ----| |-----| |__ | ||
\ / | | | __| | ||
\ /\ / | | | | | ||
\__/ \__/ |__| |__| | ||
the personal information dashboard for your terminal | ||
` | ||
|
||
// DisplayExitMessage displays the onscreen exit message when the app quits | ||
func (wtfApp *WtfApp) DisplayExitMessage() { | ||
githubAPIKey := readGitHubAPIKey(wtfApp.config) | ||
ghUser := support.NewGitHubUser(githubAPIKey) | ||
|
||
exitMessageIsDisplayable := readDisplayableConfig(wtfApp.config) | ||
|
||
wtfApp.displayExitMsg(ghUser, exitMessageIsDisplayable) | ||
} | ||
|
||
/* -------------------- Unexported Functions -------------------- */ | ||
|
||
func (wtfApp *WtfApp) displayExitMsg(ghUser *support.GitHubUser, exitMessageIsDisplayable bool) string { | ||
_ = ghUser.Load() | ||
|
||
// If a sponsor or contributor and opt out of seeing the exit message, do not display it | ||
if (ghUser.IsContributor || ghUser.IsSponsor) && !exitMessageIsDisplayable { | ||
return "" | ||
} | ||
|
||
msgs := []string{} | ||
|
||
msgs = append(msgs, aurora.Magenta(exitMessageHeader).String()) | ||
|
||
if ghUser.IsContributor { | ||
msgs = append(msgs, wtfApp.contributorThankYouMessage()) | ||
} | ||
|
||
if ghUser.IsSponsor { | ||
msgs = append(msgs, wtfApp.sponsorThankYouMessage()) | ||
} | ||
|
||
if !ghUser.IsContributor && !ghUser.IsSponsor { | ||
msgs = append(msgs, wtfApp.supportRequestMessage()) | ||
} | ||
|
||
displayMsg := strings.Join(msgs, "\n") | ||
|
||
fmt.Println(displayMsg) | ||
|
||
return displayMsg | ||
} | ||
|
||
// readDisplayableConfig figures out whether or not the exit message should be displayed | ||
// per the user's wishes. It allows contributors and sponsors to opt out of the exit message | ||
func readDisplayableConfig(cfg *config.Config) bool { | ||
displayExitMsg := cfg.UBool("wtf.exitMessage.display", true) | ||
return displayExitMsg | ||
} | ||
|
||
// readGitHubAPIKey attempts to find a GitHub API key somewhere in the configuration file | ||
func readGitHubAPIKey(cfg *config.Config) string { | ||
apiKey := cfg.UString("wtf.exitMessage.githubAPIKey", os.Getenv("WTF_GITHUB_TOKEN")) | ||
if apiKey != "" { | ||
return apiKey | ||
} | ||
|
||
moduleConfig, err := cfg.Get("wtf.mods.github") | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return moduleConfig.UString("apiKey", "") | ||
} | ||
|
||
/* -------------------- Messaging -------------------- */ | ||
|
||
func (wtfApp *WtfApp) contributorThankYouMessage() string { | ||
str := " On behalf of all the users of WTF, thank you for contributing to the source code." | ||
str += fmt.Sprintf(" %s", aurora.Green("You rock.")) | ||
|
||
return str | ||
} | ||
|
||
func (wtfApp *WtfApp) sponsorThankYouMessage() string { | ||
str := " Your sponsorship of WTF makes a difference. Thank you for sponsoring and supporting WTF." | ||
str += fmt.Sprintf(" %s", aurora.Green("You're awesome.")) | ||
|
||
return str | ||
} | ||
|
||
func (wtfApp *WtfApp) supportRequestMessage() string { | ||
str := " The development and maintenance of WTF is supported by sponsorships.\n" | ||
str += fmt.Sprintf(" Please consider sponsoring WTF at %s\n", aurora.Green("https://github.com/sponsors/senorprogrammer")) | ||
|
||
return str | ||
} |
This file contains 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,71 @@ | ||
package app | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/wtfutil/wtf/support" | ||
"gotest.tools/assert" | ||
) | ||
|
||
func Test_displayExitMessage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
isDisplayable bool | ||
isContributor bool | ||
isSponsor bool | ||
compareWith string | ||
expected string | ||
}{ | ||
{ | ||
name: "when not displayable", | ||
isDisplayable: false, | ||
isContributor: true, | ||
isSponsor: true, | ||
compareWith: "equals", | ||
expected: "", | ||
}, | ||
{ | ||
name: "when contributor", | ||
isDisplayable: true, | ||
isContributor: true, | ||
compareWith: "contains", | ||
expected: "thank you for contributing", | ||
}, | ||
{ | ||
name: "when sponsor", | ||
isDisplayable: true, | ||
isSponsor: true, | ||
compareWith: "contains", | ||
expected: "Thank you for sponsoring", | ||
}, | ||
{ | ||
name: "when user", | ||
isDisplayable: true, | ||
isContributor: false, | ||
isSponsor: false, | ||
compareWith: "contains", | ||
expected: "supported by sponsorships", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
wtfApp := WtfApp{} | ||
ghUser := &support.GitHubUser{ | ||
IsContributor: tt.isContributor, | ||
IsSponsor: tt.isSponsor, | ||
} | ||
|
||
actual := wtfApp.displayExitMsg(ghUser, tt.isDisplayable) | ||
|
||
if tt.compareWith == "equals" { | ||
assert.Equal(t, actual, tt.expected) | ||
} | ||
|
||
if tt.compareWith == "contains" { | ||
assert.Equal(t, true, strings.Contains(actual, tt.expected)) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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 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 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 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 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.