Skip to content

Commit

Permalink
Improved readability and reduced redundancy in factory.go
Browse files Browse the repository at this point in the history
Refactored `FactoryMode` and `Handle` functions in factory.go to improve readability and reduce redundancy. Variables `user`, `pass` and `err` are now declared once and their values updated as necessary, replacing the previous repetitious method of returning a new instance of these variables upon each error. The `tlUser`, `tlPass`, and `err` variables in the `Handle` function have also been updated to follow this new method.

This change enhances the readability of the code and prevents the unnecessary multiple declarations and returns of the same variables. It also aligns with the DRY (Don't Repeat Yourself) principle making the codebase maintainable.
  • Loading branch information
Septrum101 committed Sep 28, 2023
1 parent ccf4bb1 commit 723dbeb
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions app/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,49 +134,53 @@ func (f *Factory) SendInfo() error {
}
}

func (f *Factory) FactoryMode() (string, string, error) {
func (f *Factory) FactoryMode() (user string, pass string, err error) {
payload, err := utils.ECBEncrypt([]byte("FactoryMode.gch?mode=2&user=notused"), f.Key)
if err != nil {
return "", "", err
return
}
resp, err := f.cli.R().SetBody(payload).Post("webFacEntry")
if err != nil {
return "", "", err
return
}

dec, err := utils.ECBDecrypt(resp.Body(), f.Key)
if err != nil {
return "", "", err
return
}

u, err := url.ParseQuery(string(bytes.ReplaceAll(dec, []byte("FactoryModeAuth.gch?"), []byte(""))))
if err != nil {
return "", "", err
return
}

return u.Get("user"), u.Get("pass"), nil
user = u.Get("user")
pass = u.Get("pass")

return
}

func (f *Factory) Handle() (string, string, error) {
func (f *Factory) Handle() (tlUser string, tlPass string, err error) {
fmt.Println(strings.Repeat("-", 35))

fmt.Print("step [0] reset factory: ")
if err := f.Reset(); err != nil {

Check failure on line 167 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

inner declaration of var err error
return "", "", fmt.Errorf("reset errors: %v\n", err)
return

Check failure on line 168 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

result parameter err not in scope at return
} else {
fmt.Println("ok")
}

fmt.Print("step [1] request factory mode: ")
if err := f.ReqFactoryMode(); err != nil {

Check failure on line 174 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

inner declaration of var err error
return "", "", err
return

Check failure on line 175 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

result parameter err not in scope at return
} else {
fmt.Println("ok")
}

fmt.Print("step [2] send sq: ")
ver, err := f.SendSq()
if err != nil {
return "", "", err
return
} else {
fmt.Println("ok")
}
Expand All @@ -185,27 +189,27 @@ func (f *Factory) Handle() (string, string, error) {
switch ver {
case 1:
if err := f.CheckLoginAuth(); err != nil {

Check failure on line 191 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

inner declaration of var err error
return "", "", err
return

Check failure on line 192 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

result parameter err not in scope at return
}
case 2:
if err := f.SendInfo(); err != nil {

Check failure on line 195 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

inner declaration of var err error
return "", "", err
return

Check failure on line 196 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

result parameter err not in scope at return
}
if err := f.CheckLoginAuth(); err != nil {

Check failure on line 198 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

inner declaration of var err error
return "", "", err
return

Check failure on line 199 in app/factory/factory.go

View workflow job for this annotation

GitHub Actions / goreleaser

result parameter err not in scope at return
}
}
fmt.Println("ok")

fmt.Print("step [4] enter factory mode: ")
tlUser, tlPass, err := f.FactoryMode()
tlUser, tlPass, err = f.FactoryMode()
if err != nil {
return "", "", err
return
} else {
fmt.Println("ok")
}

fmt.Println(strings.Repeat("-", 35))

return tlUser, tlPass, nil
return
}

0 comments on commit 723dbeb

Please sign in to comment.