Skip to content

Commit

Permalink
Replace log.Fatal by log.Printf and remove exit(1) if return
Browse files Browse the repository at this point in the history
  • Loading branch information
JeGoi committed Aug 10, 2023
1 parent 76fec80 commit 4f7901e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
48 changes: 26 additions & 22 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func main() {
TempPATH = os.Getenv("tmp")
if TempPATH == "" {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("invalidTempPATH"), walk.MsgBoxOK)
log.Fatal("Failed found a temporary directory")
log.Println("Failed found a temporary directory")
os.Exit(1)
}

currentWorkingDirectory, err := os.Executable()
Expand Down Expand Up @@ -121,10 +122,10 @@ func main() {
},
}.Run()); err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("errorMainWindow"), walk.MsgBoxOK)
log.Fatal("Failed opening main window: ", err)
log.Println("Failed opening main window: ", err)
os.Remove(TempPATH + "\\" + "pf_bg.png")
os.Exit(1)
}
os.Remove(TempPATH + "\\" + "pf_bg.png")
os.Exit(0)
}

Expand All @@ -145,7 +146,7 @@ func Configure() {
err := writeProfileToLocalFile("profile.xml", PROFILE_URL)
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotRetrieveProfileFile"), walk.MsgBoxOK)
log.Fatal("Failed loading profile: ", err)
log.Println("Failed loading profile: ", err)
os.Exit(1)
}

Expand All @@ -154,7 +155,8 @@ func Configure() {
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotReadProfileData"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed reading profile: ", err)
log.Println("Failed reading profile: ", err)
os.Exit(1)
}

// Decode converted xml profile
Expand All @@ -165,7 +167,8 @@ func Configure() {
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotDecodeProfileFile"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed decoding profile: ", err)
log.Println("Failed decoding profile: ", err)
os.Exit(1)
}

// Get data from the mobileconfig file
Expand Down Expand Up @@ -206,12 +209,12 @@ func Configure() {
alertMessage := T("cannotGenerateCertificateFile")
userCertDecode, err = createCertTempFile(TempPATH, userCert, userAuth, fileExtension, alertMessage)
if err != nil {
log.Fatal("Failed creating profile: ", err)
log.Println("Failed creating profile: ", err)
os.Exit(1)
}
err = addCertToMachine(userCertDecode, CERTUTIL_PROGRAM_PATH)
if err != nil {
log.Fatal("Failed adding Cert: ", err)
log.Println("Failed adding Cert: ", err)
os.Exit(1)
}
// Certificate of Authority configuration
Expand All @@ -223,20 +226,20 @@ func Configure() {
alertMessage := T("cannotGenerateCAFile")
caFileBinary, err = createCertTempFile(TempPATH, caCert, caName, fileExtension, alertMessage)
if err != nil {
log.Fatal("Failed creating profile: ", err)
log.Println("Failed creating profile: ", err)
os.Exit(1)
}
err = addCAToMachine(caFileBinary, CERTUTIL_PROGRAM_PATH)
if err != nil {
log.Fatal("Failed adding CA: ", err)
log.Println("Failed adding CA: ", err)
os.Exit(1)
}
}
default:
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("Unexpected PayloadType {{.PayloadType}} please contact your local support.", map[string]interface{}{
"PayloadType": payloadType,
}), walk.MsgBoxOK)
log.Fatal("Unexpected PayloadType: ", payloadType)
log.Println("Unexpected PayloadType: ", payloadType)
os.Exit(1)
}
}
Expand Down Expand Up @@ -296,13 +299,13 @@ func configureWifi(xmlPlistProfile map[string]interface{}, wifiIndex int,eapType
// executes the template
templateToFile, err := executeTemplate(WIFI_PEAP_TEMPLATE_NAME, WIFI_PEAP_TEMPLATE, elementsToReplaceInTemplate)
if err != nil {
log.Fatal("Failed executing template: ", err)
log.Println("Failed executing template: ", err)
os.Exit(1)
}
// creates profile file with the executed template
err = createProfileFile(templateToFile)
if err != nil {
log.Fatal("Failed creating profile file: ", err)
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
// adds the new profile to Windows with netsh command
Expand All @@ -313,7 +316,8 @@ func configureWifi(xmlPlistProfile map[string]interface{}, wifiIndex int,eapType
if err != nil {
os.Remove(caFileBinary)
os.Remove("profile.xml")
log.Fatal("Unable to get CA fingerprint: ", err)
log.Println("Unable to get CA fingerprint: ", err)
os.Exit(1)
}
elementsToReplaceInTemplate = Template{
ProfileName: ssidString,
Expand All @@ -326,20 +330,20 @@ func configureWifi(xmlPlistProfile map[string]interface{}, wifiIndex int,eapType
os.Remove(caFileBinary)
templateToFile, err = executeTemplate(WIFI_TLS_TEMPLATE_NAME, WIFI_TLS_TEMPLATE, elementsToReplaceInTemplate)
if err != nil {
log.Fatal("Failed executing template: ", err)
log.Println("Failed executing template: ", err)
os.Exit(1)
}
err = createProfileFile(templateToFile)
if err != nil {
log.Fatal("Failed creating profile file: ", err)
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, addWLANProfileCommand, WLAN_ERROR_MESSAGE, wlanSuccessMessage)
}
if (eapType != EAPTYPE_TLS) && (eapType != EAPTYPE_PEAP) {
// error handling
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("unexpectedEAPType"), walk.MsgBoxOK)
log.Fatal("Incorrect EAP type: ", eapType)
log.Println("Incorrect EAP type: ", eapType)
os.Exit(1)
}
} else {
Expand Down Expand Up @@ -379,12 +383,12 @@ func configureWifi(xmlPlistProfile map[string]interface{}, wifiIndex int,eapType
}
templateToFile, err := executeTemplate(WIFI_OPEN_TEMPLATE_NAME, WIFI_OPEN_TEMPLATE, elementsToReplaceInTemplate)
if err != nil {
log.Fatal("Failed executing template: ", err)
log.Println("Failed executing template: ", err)
os.Exit(1)
}
err = createProfileFile(templateToFile)
if err != nil {
log.Fatal("Failed creating profile file: ", err)
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, addWLANProfileCommand, WLAN_ERROR_MESSAGE, wlanSuccessMessage)
Expand Down Expand Up @@ -416,23 +420,23 @@ func configureWired(xmlPlistProfile map[string]interface{}, wiredIndex int,eapTy
if eapType == EAPTYPE_PEAP {
err := createProfileFile(WIRED_PEAP_TEMPLATE)
if err != nil {
log.Fatal("Failed creating profile file: ", err)
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, wiredNetshCommand, WIRED_ERROR_MESSAGE, WIRED_SUCCESS_MESSAGE)
}
if eapType == EAPTYPE_TLS {
err := createProfileFile(WIRED_TLS_TEMPLATE)
if err != nil {
log.Fatal("Failed creating profile file: ", err)
log.Println("Failed creating profile file: ", err)
os.Exit(1)
}
addProfileToMachine(profileFile, wiredNetshCommand, WIRED_ERROR_MESSAGE, WIRED_SUCCESS_MESSAGE)
}
if (eapType != EAPTYPE_TLS) && (eapType != EAPTYPE_PEAP) {
// error handling
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("unexpectedEAPType"), walk.MsgBoxOK)
log.Fatal("Incorrect EAP type: ", eapType)
log.Println("Incorrect EAP type: ", eapType)
os.Exit(1)
}
}
31 changes: 15 additions & 16 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func createLanguageFile(currentDir, translationLanguage, languageFileName string
languageFile, err := os.Create(currentDir + "\\" + languageFileName)
if err != nil {
walk.MsgBox(windowMsgBox, "Error", "Unable to create the language file, please contact your local support.", walk.MsgBoxOK)
log.Fatal("Failed creating language file: ", err)
log.Println("Failed creating language file: ", err)
return err
}
// close file
Expand All @@ -35,7 +35,7 @@ func createLanguageFile(currentDir, translationLanguage, languageFileName string
_, err = io.Copy(languageFile, strings.NewReader(translationLanguage))
if err != nil {
walk.MsgBox(windowMsgBox, "Error", "Unable to write into language file, please contact your local support.", walk.MsgBoxOK)
log.Fatal("Failed writing language constant to file: ", err)
log.Println("Failed writing language constant to file: ", err)
return err
}
log.Print("Language file successfully created.")
Expand All @@ -47,19 +47,19 @@ func base64ToPng(BACKGROUND_IMAGE_PF, tempPath string) (error, string) {
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(BACKGROUND_IMAGE_PF))
decodeBase64ToPng, _, err := image.Decode(reader)
if err != nil {
log.Fatal("Unable to decode base 64 background image: ", err)
log.Println("Unable to decode base 64 background image: ", err)
}
//Encode from image format to writer
pngFilename := "pf_bg.png"
pngFilePath := tempPath + "\\" + pngFilename
backgroundFile, err := os.Create(pngFilePath)
if err != nil {
log.Fatal("Unable to open or create background image: ", err)
log.Println("Unable to open or create background image: ", err)
return err, pngFilename
}
err = png.Encode(backgroundFile, decodeBase64ToPng)
if err != nil {
log.Fatal(err)
log.Println(err)
os.Remove(pngFilePath)
return err, pngFilename
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func executeTemplate(nameTemplate, constTemplate string, templateToApply Templat
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotParseTemplate"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed parsing: ", err)
log.Println("Failed parsing: ", err)
return "", err
}
// executes the template into the open file
Expand All @@ -122,14 +122,14 @@ func executeTemplate(nameTemplate, constTemplate string, templateToApply Templat
log.Println("Error: ", err)
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotExecuteTemplate"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed executing: ", err)
log.Println("Failed executing: ", err)
return templateBuffer.String(), err
}
// handles error
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotCreateWLANProfile"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed creating WLANProfile: ", err)
log.Println("Failed creating WLANProfile: ", err)
return templateBuffer.String(), err
}
return templateBuffer.String(), nil
Expand All @@ -144,7 +144,7 @@ func createProfileFile(templateToFile string) error {
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotCreateProfileFile"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed creating profile file: ", err)
log.Println("Failed creating profile file: ", err)
return err
}
// close file
Expand All @@ -155,7 +155,7 @@ func createProfileFile(templateToFile string) error {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotWriteIntoProfileFile"), walk.MsgBoxOK)
os.Remove("profile.xml")
os.Remove(profileFilePath)
log.Fatal("Failed writing template to file: ", err)
log.Println("Failed writing template to file: ", err)
return err
}
os.Remove("profile.xml")
Expand All @@ -170,7 +170,7 @@ func addProfileToMachine(profileFile string, cmd *exec.Cmd, ErrorMessage, Succes
if err != nil {
log.Printf("Failed adding profile: output: %s\n", output, err)
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), ErrorMessage, walk.MsgBoxOK)
log.Fatal("Failed adding profile: ", err, output)
log.Println("Failed adding profile: ", err, output)
return err
} else {
walk.MsgBox(windowMsgBox, "Information:", SuccessMessage, walk.MsgBoxOK)
Expand All @@ -186,7 +186,7 @@ func createCertTempFile(tempPath, certificate, fileName, fileExtension, alertMes
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotCreateCertTempFile"), walk.MsgBoxOK)
// clean up
os.Remove("profile.xml")
log.Fatal("Failed creating temp file: ", err)
log.Println("Failed creating temp file: ", err)
return file.Name(), err
}
certName := file.Name()
Expand All @@ -196,21 +196,20 @@ func createCertTempFile(tempPath, certificate, fileName, fileExtension, alertMes
// handle error, exit if needed
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotdecodeCertificateFile"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed decoding certificate: ", err)
os.Exit(1)
log.Println("Failed decoding certificate: ", err)
return certName, err
}
// write into new file
if _, err := file.Write(decodedCertificate); err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotWriteIntoTempFile"), walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed writing decoded certificate into temp file: ", err)
log.Println("Failed writing decoded certificate into temp file: ", err)
return certName, err
}
if err := file.Close(); err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), alertMessage, walk.MsgBoxOK)
os.Remove("profile.xml")
log.Fatal("Failed closing certificate file: ", err)
log.Println("Failed closing certificate file: ", err)
return certName, err
}
return certName, nil
Expand Down

0 comments on commit 4f7901e

Please sign in to comment.