Skip to content

Commit

Permalink
Fix goconst & lll linters issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hbollon committed Feb 7, 2021
1 parent cd0b043 commit 07d08be
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 70 deletions.
9 changes: 6 additions & 3 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func (bot *IGopher) connectToInstagramWebDriver() {
}
randomSleepCustom(10, 15)
// Check if login was successful
if bot.SeleniumStruct.IsElementPresent(selenium.ByXPATH, "//*[@aria-label='Home'] | //button[text()='Save Info'] | //button[text()='Not Now']") {
if bot.SeleniumStruct.IsElementPresent(selenium.ByXPATH,
"//*[@aria-label='Home'] | //button[text()='Save Info'] | //button[text()='Not Now']") {
log.Info("Login Successful!")
} else {
log.Warnf("Instagram does not ask for informations saving, the login process may have failed.")
Expand Down Expand Up @@ -97,14 +98,16 @@ func (bot *IGopher) sendMessageWebDriver(user, message string) (bool, error) {
randomSleepCustom(6, 10)

// Type and select user to dm
if find, err := bot.SeleniumStruct.WaitForElement("//*[@id=\"react-root\"]/section/div[2]/div/div[1]/div/div[2]/input", "xpath", 10); err == nil && find {
if find, err := bot.SeleniumStruct.WaitForElement(
"//*[@id=\"react-root\"]/section/div[2]/div/div[1]/div/div[2]/input", "xpath", 10); err == nil && find {
elem, _ := bot.SeleniumStruct.GetElement("//*[@id=\"react-root\"]/section/div[2]/div/div[1]/div/div[2]/input", "xpath")
log.Debug("Finded an retrieved user searchbar")
if res := SimulateHandWriting(elem, user); !res {
return false, errors.New("Error during user searching")
}
randomSleep()
usernames, err := bot.SeleniumStruct.WebDriver.FindElements(selenium.ByXPATH, "//div[@aria-labelledby]/div/span//img[@data-testid='user-avatar']")
usernames, err := bot.SeleniumStruct.WebDriver.FindElements(selenium.ByXPATH,
"//div[@aria-labelledby]/div/span//img[@data-testid='user-avatar']")
if err != nil {
return false, errors.New("Error during user selection")
}
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ func CheckEnvironment() {
dir = filepath.FromSlash(dir)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.Mkdir(dir, 0755); err != nil {
log.Fatalf("Error during creation of '%s' sub-directory, check root directory permissions or try to create it manually\nMkdir error:\n%v", dir, err)
log.Fatalf("Error during creation of '%s' sub-directory,"+
" check root directory permissions or try to create it manually\nMkdir error:\n%v", dir, err)
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const (
//
// Update this periodically.
desiredFirefoxVersion = "68.0.1"

windowsOs = "windows"
)

type file struct {
Expand Down Expand Up @@ -78,7 +80,7 @@ var (
name: "sauce-connect.zip",
path: downloadDirectory + "sauce-connect.zip",
rename: []string{downloadDirectory + "sc-4.6.3-win32", downloadDirectory + "sauce-connect"},
os: "windows",
os: windowsOs,
compressed: true,
},
}
Expand Down Expand Up @@ -137,7 +139,7 @@ func addChrome(ctx context.Context, latestChromeBuild string) error {
targetDriverPath string
)

if runtime.GOOS == "windows" {
if runtime.GOOS == windowsOs {
prefixOS = "Win_x64"
lastChangeFile = "Win_x64/LAST_CHANGE"
chromeFilename = "chrome-win.zip"
Expand Down Expand Up @@ -204,7 +206,7 @@ func addChrome(ctx context.Context, latestChromeBuild string) error {
// If `desiredVersion` is empty, the the latest version will be used.
// Otherwise, the specific version will be used.
func addFirefox(desiredVersion string) {
if runtime.GOOS == "windows" {
if runtime.GOOS == windowsOs {
if desiredVersion == "" {
files = append(files, file{
// This is a recent nightly. Update this path periodically.
Expand All @@ -217,7 +219,9 @@ func addFirefox(desiredVersion string) {
} else {
files = append(files, file{
// This is a recent nightly. Update this path periodically.
url: "https://download-installer.cdn.mozilla.net/pub/firefox/releases/" + url.PathEscape(desiredVersion) + "/en-US/firefox-" + url.PathEscape(desiredVersion) + ".exe",
url: "https://download-installer.cdn.mozilla.net/pub/firefox/releases/" +
url.PathEscape(desiredVersion) + "/en-US/firefox-" +
url.PathEscape(desiredVersion) + ".exe",
name: "firefox.exe",
path: downloadDirectory + "firefox.exe",
compressed: false,
Expand All @@ -237,7 +241,9 @@ func addFirefox(desiredVersion string) {
} else {
files = append(files, file{
// This is a recent nightly. Update this path periodically.
url: "https://download-installer.cdn.mozilla.net/pub/firefox/releases/" + url.PathEscape(desiredVersion) + "/linux-x86_64/en-US/firefox-" + url.PathEscape(desiredVersion) + ".tar.bz2",
url: "https://download-installer.cdn.mozilla.net/pub/firefox/releases/" +
url.PathEscape(desiredVersion) + "/linux-x86_64/en-US/firefox-" +
url.PathEscape(desiredVersion) + ".tar.bz2",
name: "firefox.tar.bz2",
path: downloadDirectory + "firefox.tar.bz2",
compressed: true,
Expand Down Expand Up @@ -266,11 +272,12 @@ func DownloadDependencies(downloadBrowsers, downloadLatest, forceDl bool) {
addFirefox(firefoxVersion)
}

if err := addLatestGithubRelease(ctx, "SeleniumHQ", "htmlunit-driver", "htmlunit-driver-.*-jar-with-dependencies.jar", "htmlunit-driver.jar", false); err != nil {
if err := addLatestGithubRelease(ctx, "SeleniumHQ", "htmlunit-driver", "htmlunit-driver-.*-jar-with-dependencies.jar",
"htmlunit-driver.jar", false); err != nil {
log.Errorf("Unable to find the latest HTMLUnit Driver: %s", err)
}

if runtime.GOOS == "windows" {
if runtime.GOOS == windowsOs {
if err := addLatestGithubRelease(ctx, "mozilla", "geckodriver", "geckodriver-.*win64.zip", "geckodriver.zip", true); err != nil {
log.Errorf("Unable to find the latest Geckodriver: %s", err)
}
Expand Down Expand Up @@ -349,7 +356,7 @@ func extractFile(file file) error {
switch path.Ext(file.name) {
case ".zip":
log.Debugf("Unzipping %q", file.path)
if runtime.GOOS == "windows" {
if runtime.GOOS == windowsOs {
if err := exec.Command("tar", "-xf", file.path, "-C", downloadDirectory).Run(); err != nil {
return fmt.Errorf("Error unzipping %q: %v", file.path, err)
}
Expand Down
31 changes: 19 additions & 12 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
"github.com/tebeka/selenium/chrome"
)

const (
locatorID = "ID"
locatorName = "NAME"
locatorXPath = "XPATH"
locatorCSS = "CSS"
)

var (
seleniumPath = filepath.FromSlash("./lib/selenium-server.jar")
chromePath, chromeDriverPath, geckoDriverPath string
Expand Down Expand Up @@ -123,13 +130,13 @@ func (s *Selenium) IsElementPresent(by, value string) bool {
// GetElement wait for element and then return when it's available
func (s *Selenium) GetElement(elementTag, locator string) (selenium.WebElement, error) {
locator = strings.ToUpper(locator)
if locator == "ID" && s.IsElementPresent(selenium.ByID, elementTag) {
if locator == locatorID && s.IsElementPresent(selenium.ByID, elementTag) {
return s.WebDriver.FindElement(selenium.ByID, elementTag)
} else if locator == "NAME" && s.IsElementPresent(selenium.ByName, elementTag) {
} else if locator == locatorName && s.IsElementPresent(selenium.ByName, elementTag) {
return s.WebDriver.FindElement(selenium.ByName, elementTag)
} else if locator == "XPATH" && s.IsElementPresent(selenium.ByXPATH, elementTag) {
} else if locator == locatorXPath && s.IsElementPresent(selenium.ByXPATH, elementTag) {
return s.WebDriver.FindElement(selenium.ByXPATH, elementTag)
} else if locator == "CSS" && s.IsElementPresent(selenium.ByCSSSelector, elementTag) {
} else if locator == locatorCSS && s.IsElementPresent(selenium.ByCSSSelector, elementTag) {
return s.WebDriver.FindElement(selenium.ByCSSSelector, elementTag)
} else {
log.Debugf("Incorrect locator '%s'", locator)
Expand All @@ -140,13 +147,13 @@ func (s *Selenium) GetElement(elementTag, locator string) (selenium.WebElement,
// GetElements wait for elements and then return when they're available
func (s *Selenium) GetElements(elementTag, locator string) ([]selenium.WebElement, error) {
locator = strings.ToUpper(locator)
if locator == "ID" && s.IsElementPresent(selenium.ByID, elementTag) {
if locator == locatorID && s.IsElementPresent(selenium.ByID, elementTag) {
return s.WebDriver.FindElements(selenium.ByID, elementTag)
} else if locator == "NAME" && s.IsElementPresent(selenium.ByName, elementTag) {
} else if locator == locatorName && s.IsElementPresent(selenium.ByName, elementTag) {
return s.WebDriver.FindElements(selenium.ByName, elementTag)
} else if locator == "XPATH" && s.IsElementPresent(selenium.ByXPATH, elementTag) {
} else if locator == locatorXPath && s.IsElementPresent(selenium.ByXPATH, elementTag) {
return s.WebDriver.FindElements(selenium.ByXPATH, elementTag)
} else if locator == "CSS" && s.IsElementPresent(selenium.ByCSSSelector, elementTag) {
} else if locator == locatorCSS && s.IsElementPresent(selenium.ByCSSSelector, elementTag) {
return s.WebDriver.FindElements(selenium.ByCSSSelector, elementTag)
} else {
log.Debugf("Incorrect locator '%s'", locator)
Expand All @@ -168,10 +175,10 @@ func (s *Selenium) WaitForElement(elementTag, locator string, delay int) (bool,
case <-timeout:
return false, errors.New("Timed out : element not found")
case <-tick.C:
if (locator == "ID" && s.IsElementPresent(selenium.ByID, elementTag)) ||
(locator == "NAME" && s.IsElementPresent(selenium.ByName, elementTag)) ||
(locator == "XPATH" && s.IsElementPresent(selenium.ByXPATH, elementTag)) ||
(locator == "CSS" && s.IsElementPresent(selenium.ByCSSSelector, elementTag)) {
if (locator == locatorID && s.IsElementPresent(selenium.ByID, elementTag)) ||
(locator == locatorName && s.IsElementPresent(selenium.ByName, elementTag)) ||
(locator == locatorXPath && s.IsElementPresent(selenium.ByXPATH, elementTag)) ||
(locator == locatorCSS && s.IsElementPresent(selenium.ByCSSSelector, elementTag)) {
return true, nil
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/tui/genericMenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ func (m model) UpdateGenericMenu(msg tea.Msg) (model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
case ctrlC:
return m, tea.Quit

case "ctrl+b":
case ctrlB:
m.screen = settingsMenu

case "up", "k":
case up, "k":
if m.genericMenuScreen.cursor > 0 {
m.genericMenuScreen.cursor--
}

case "down", "j":
case down, "j":
if m.genericMenuScreen.cursor < len(m.genericMenuScreen.choices)-1 {
m.genericMenuScreen.cursor++
}

case "enter":
case enter:
switch m.genericMenuScreen.cursor {
case 0:
switch m.settingsChoice {
Expand Down
8 changes: 4 additions & 4 deletions internal/tui/homePage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ func (m model) UpdateHomePage(msg tea.Msg) (model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
case ctrlC:
return m, tea.Quit

case "up", "k":
case up, "k":
if m.homeScreen.cursor > 0 {
m.homeScreen.cursor--
}

case "down", "j":
case down, "j":
if m.homeScreen.cursor < len(m.homeScreen.choices)-1 {
m.homeScreen.cursor++
}

case "enter":
case enter:
errorMessage = ""
switch m.homeScreen.cursor {
case 0:
Expand Down
13 changes: 7 additions & 6 deletions internal/tui/settingsBoolScreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ func (m model) UpdateSettingsBoolMenu(msg tea.Msg) (model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
case ctrlC:
return m, tea.Quit

case "ctrl+b":
case ctrlB:
m.screen = settingsMenu

case "up", "k":
case up, "k":
if m.settingsTrueFalseScreen.cursor > 0 {
m.settingsTrueFalseScreen.cursor--
}

case "down", "j":
case down, "j":
if m.settingsTrueFalseScreen.cursor < len(m.settingsTrueFalseScreen.choices)-1 {
m.settingsTrueFalseScreen.cursor++
}

case "enter":
case enter:
switch m.settingsTrueFalseScreen.cursor {
case 0:
switch m.settingsChoice {
Expand Down Expand Up @@ -87,7 +87,8 @@ func (m model) ViewSettingsBoolMenu() string {
s = fmt.Sprintf("\nDo you want to enable %s module? (Default: %s)\n\n", keyword("AutoDM"), keyword("true"))

case autodmGreetingEnablingSettings:
s = fmt.Sprintf("\nDo you want to enable %s sub-module with %s? (Default: %s)\n\n", keyword("Greeting"), keyword("AutoDm"), keyword("true"))
s = fmt.Sprintf("\nDo you want to enable %s sub-module with %s? (Default: %s)\n\n",
keyword("Greeting"), keyword("AutoDm"), keyword("true"))

case quotasEnablingSettings:
s = fmt.Sprintf("\nDo you want to enable %s module? (Default: %s)\n\n", keyword("Quotas"), keyword("true"))
Expand Down
26 changes: 14 additions & 12 deletions internal/tui/settingsInputsScreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
log "github.com/sirupsen/logrus"
)

const invalidInputMsg = "Invalid input, please check all fields.\n\n"

func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
case ctrlC:
return m, tea.Quit

case "ctrl+b":
case ctrlB:
errorMessage = ""
m.screen = settingsMenu

case "enter":
case enter:
if m.settingsInputsScreen.index == len(m.settingsInputsScreen.input) {
switch m.settingsChoice {
case accountSettings:
Expand All @@ -30,7 +32,7 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
}
err := validate.Struct(acc)
if err != nil {
errorMessage = "Invalid input, please check all fields.\n\n"
errorMessage = invalidInputMsg
break
} else {
config.Account = acc
Expand All @@ -46,7 +48,7 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
}
err := validate.Struct(scr)
if err != nil {
errorMessage = "Invalid input, please check all fields.\n\n"
errorMessage = invalidInputMsg
break
} else {
config.SrcUsers = scr
Expand All @@ -62,7 +64,7 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
}
err := validate.Struct(dm)
if err != nil {
errorMessage = "Invalid input, please check all fields.\n\n"
errorMessage = invalidInputMsg
break
} else {
config.AutoDm.DmTemplates = dm.DmTemplates
Expand All @@ -75,7 +77,7 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
}
err := validate.Struct(gre)
if err != nil {
errorMessage = "Invalid input, please check all fields.\n\n"
errorMessage = invalidInputMsg
break
} else {
config.AutoDm.Greeting.Template = gre.Template
Expand All @@ -92,7 +94,7 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
}
err := validate.Struct(quo)
if err != nil {
errorMessage = "Invalid input, please check all fields.\n\n"
errorMessage = invalidInputMsg
break
} else {
config.Quotas.DmDay = quo.DmDay
Expand All @@ -101,7 +103,7 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
m.screen = settingsMenu
}
} else {
errorMessage = "Invalid input, please check all fields.\n\n"
errorMessage = invalidInputMsg
}
case scheduleSettings:
sche := igopher.ScheduleYaml{
Expand All @@ -110,7 +112,7 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
}
err := validate.Struct(sche)
if err != nil {
errorMessage = "Invalid input, please check all fields.\n\n"
errorMessage = invalidInputMsg
break
} else {
config.Schedule.BeginAt = sche.BeginAt
Expand All @@ -125,11 +127,11 @@ func (m model) UpdateSettingsInputsMenu(msg tea.Msg) (model, tea.Cmd) {
}

// Cycle between inputs
case "tab", "shift+tab", "up", "down":
case "tab", "shift+tab", up, down:
s := msg.String()

// Cycle indexes
if s == "up" || s == "shift+tab" {
if s == up || s == "shift+tab" {
m.settingsInputsScreen.index--
} else {
m.settingsInputsScreen.index++
Expand Down
Loading

0 comments on commit 07d08be

Please sign in to comment.