From 6dbbc7b30d7a9c88f79dda8ddb1abed7b4f744f2 Mon Sep 17 00:00:00 2001 From: Raul Catalinas <105791463+RaulCatalinas@users.noreply.github.com> Date: Mon, 22 Jul 2024 20:21:25 +0200 Subject: [PATCH] enhance: improve error messages for better user and developer clarity - Enhanced error messages to be more descriptive for users. - Improved error messages to aid in easier debugging. --- internal/constants/errors.go | 43 +++++++++++++++ internal/handlers/handlers_options.go | 22 ++++++-- internal/user-input/add-commitlint.go | 5 +- internal/user-input/package-managers.go | 5 +- internal/user-input/publishToNpm.go | 5 +- internal/utils/commitlint.go | 71 ++++++++++++++++++------- internal/utils/console.go | 28 ++++++++++ internal/utils/dependencies.go | 34 ++++++++---- internal/utils/errors.go | 7 +++ internal/utils/husky-library.go | 50 +++++++++++++---- internal/utils/npm.go | 44 ++++++++++++--- internal/utils/package-json.go | 21 ++++++-- internal/utils/user-os.go | 28 ++++++++-- internal/utils/utils.go | 49 +++++++++++------ 14 files changed, 335 insertions(+), 77 deletions(-) create mode 100644 internal/constants/errors.go create mode 100644 internal/utils/console.go create mode 100644 internal/utils/errors.go diff --git a/internal/constants/errors.go b/internal/constants/errors.go new file mode 100644 index 0000000..efd9ce1 --- /dev/null +++ b/internal/constants/errors.go @@ -0,0 +1,43 @@ +package constants + +var ERROR_MESSAGES = map[string]string{ + "NotFound": "The {fileName} file wasn't found in the current directory.", + + "Default": "Something went wrong, please try again later, if the error persists please report it on " + ISSUES + ".", + + "Dependencies": "An error occurred while installing dependencies, please try again later, if the error persists please report it on " + ISSUES + ".", + + "Husky": "An error has occurred during the Husky configuration process, please try again later, if the error persists please report it on " + ISSUES + ".", + + "CommitLintConfig": "An error has occurred during the commitlint configuration process, please try again later, if the error persists please report it on " + ISSUES + ".", + + "AddCommitLint": "An error occurred while asking if you wanna add commitlint, please try again later, if the error persists please report it on " + ISSUES + ".", + + "PackageManagerSelection": "An error occurred while selecting the package manager, please try again later, if the error persists, please report it on " + ISSUES + ".", + + "CreateEmptyFile": "An error occurred while creating the empty file. Please try again later, if the error persists please report it on " + ISSUES + ".", + + "CreateFolder": "An error occurred while creating the folder: {folderName}, please try again later, if the error persists, please report it on " + ISSUES + ".", + + "ReadFile": "An error has occurred while reading the {fileName} file, please try again later, if the error persists, please report it on " + ISSUES + ".", + + "WriteFile": "An error has occurred while writing to the {fileName} file, please try again later, if the error persists, please report it on " + ISSUES + ".", + + "ConfigFilesCreate": "An error occurred while creating configuration files. Please try again later, if the error persists please report it on " + ISSUES + ".", + + "PublishConfirmation": "An error occurred while confirming npm publication. Please try again later, if the error persists, please report it on" + ISSUES + ".", + + "NpmIgnoreWrite": "An error occurred while writing to the '.npmignore' file. Please try again later, if the error persists please report it on " + ISSUES + ".", + + "GitHubRepoOpen": "An error occurred while opening the GitHub repository in a new browser tab. Please try again later, if the error persists please report it on" + ISSUES + ".", + + "JsonUnmarshal": "Replace this with an error message that's descriptive and easily understandable by any developer.", + + "JsonMarshal": "Replace this with an error message that's descriptive and easily understandable by any developer.", + + "GetWorkingDirectory": "Replace this with an error message that's descriptive and easily understandable by any developer.", + + "InvalidTypeForFilesToAdd": "Replace this with an error message that's descriptive and easily understandable by any developer.", + + "CheckingFolderOrFile": "Replace this with an error message that's descriptive and easily understandable by any developer.", +} diff --git a/internal/handlers/handlers_options.go b/internal/handlers/handlers_options.go index 9f03d10..cba64ee 100644 --- a/internal/handlers/handlers_options.go +++ b/internal/handlers/handlers_options.go @@ -11,14 +11,21 @@ import ( ) func HandlerOptionCollaborate() { - utils.WriteMessage("info", "Opening the GitHub repository...") + utils.WriteMessage(utils.WriteMessageProps{ + Type: "info", + Message: "Opening the GitHub repository...", + }) time.Sleep(5 * time.Millisecond) err := webbrowser.Open(constants.REPOSITORY) if err != nil { - utils.WriteMessage("error", utils.GetErrorMessage("Open webbrowser")) + utils.WriteMessage(utils.WriteMessageProps{ + Type: "error", + Message: utils.GetErrorMessage("GitHubRepoOpen"), + }) + os.Exit(1) } } @@ -36,7 +43,8 @@ func HandlerOptionBuild() { PackageJsonPath: constants.PATH_PACKAGE_JSON, UseCommitlint: useCommitlint, ShouldPublishToNpm: shouldPublishToNpm, - }) + }, + ) if useCommitlint { utils.GenerateCommitlintConfig( @@ -44,8 +52,12 @@ func HandlerOptionBuild() { PackageManagerToUse: packageManagerToUse, PackageJsonPath: constants.PATH_PACKAGE_JSON, ShouldPublishToNpm: shouldPublishToNpm, - }) + }, + ) } - utils.WriteMessage("success", "All tasks were completed") + utils.WriteMessage(utils.WriteMessageProps{ + Type: "success", + Message: "All tasks were completed", + }) } diff --git a/internal/user-input/add-commitlint.go b/internal/user-input/add-commitlint.go index 37f7259..c6c875a 100644 --- a/internal/user-input/add-commitlint.go +++ b/internal/user-input/add-commitlint.go @@ -25,7 +25,10 @@ func AddCommitlint() bool { err := survey.Ask(questions, &answers) if err != nil { - utils.WriteMessage("error", utils.GetErrorMessage("HuskyConfigCleanOrder")) + utils.WriteMessage(utils.WriteMessageProps{ + Type: "error", + Message: utils.GetErrorMessage("AddCommitLint"), + }) os.Exit(1) } diff --git a/internal/user-input/package-managers.go b/internal/user-input/package-managers.go index 1218ed9..e4cffb7 100644 --- a/internal/user-input/package-managers.go +++ b/internal/user-input/package-managers.go @@ -29,7 +29,10 @@ func GetPackageManager() types.PackageManager { err := survey.Ask(questions, &answers) if err != nil { - utils.WriteMessage("error", utils.GetErrorMessage("PackageManagerSelection")) + utils.WriteMessage(utils.WriteMessageProps{ + Type: "error", + Message: utils.GetErrorMessage("PackageManagerSelection"), + }) os.Exit(1) } diff --git a/internal/user-input/publishToNpm.go b/internal/user-input/publishToNpm.go index 8ab639d..0b4ad2e 100644 --- a/internal/user-input/publishToNpm.go +++ b/internal/user-input/publishToNpm.go @@ -25,7 +25,10 @@ func ShouldPublishToNpm() bool { err := survey.Ask(questions, &answers) if err != nil { - utils.WriteMessage("error", utils.GetErrorMessage("ShouldPublishToNpm")) + utils.WriteMessage(utils.WriteMessageProps{ + Type: "error", + Message: utils.GetErrorMessage("ShouldPublishToNpm"), + }) os.Exit(1) } diff --git a/internal/utils/commitlint.go b/internal/utils/commitlint.go index d34dc06..ede3794 100644 --- a/internal/utils/commitlint.go +++ b/internal/utils/commitlint.go @@ -17,25 +17,38 @@ type CommitlintProps struct { func GenerateCommitlintConfig(commitlintProps CommitlintProps) { defer func() { if r := recover(); r != nil { - WriteMessage("error", GetErrorMessage("Commitlint")) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("Commitlint"), + }) + os.Exit(1) } }() - WriteMessage("config", "Configuring commitlint...") - - InstallDependencies(InstallProps{PackageManagerToUse: commitlintProps.PackageManagerToUse, PackagesToInstall: []string{ - "lint-staged", - "@commitlint/cli", - "@commitlint/config-conventional", - }}) - - addScript(addScriptProps{PackageJsonPath: commitlintProps.PackageJsonPath, ScriptsToAdd: []packageJsonScript{ - {Key: "lint", Value: "eslint src"}, - {Key: "lint:fix", Value: "eslint src --fix"}, - {Key: "format", Value: "prettier src --check"}, - {Key: "format:write", Value: "prettier src --write"}, - }}) + WriteMessage(WriteMessageProps{ + Type: "config", + Message: "Configuring commitlint...", + }) + + InstallDependencies(InstallProps{ + PackageManagerToUse: commitlintProps.PackageManagerToUse, + PackagesToInstall: []string{ + "lint-staged", + "@commitlint/cli", + "@commitlint/config-conventional", + }, + }) + + addScript(addScriptProps{ + PackageJsonPath: commitlintProps.PackageJsonPath, + ScriptsToAdd: []packageJsonScript{ + {Key: "lint", Value: "eslint src"}, + {Key: "lint:fix", Value: "eslint src --fix"}, + {Key: "format", Value: "prettier src --check"}, + {Key: "format:write", Value: "prettier src --write"}, + }, + }) createCommitlintConfigFiles(commitlintProps.PackageManagerToUse) @@ -43,40 +56,60 @@ func GenerateCommitlintConfig(commitlintProps CommitlintProps) { modifyNpmIgnore([]string{".lintstagedrc", "commitlint.config.js"}) } - WriteMessage("success", "commitlint's configuration generated successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "commitlint's configuration generated successfully", + }) } func createCommitlintConfigFiles(packageManagerToUse types.PackageManager) { - WriteMessage("info", "Creating configuration files...") + WriteMessage(WriteMessageProps{ + Type: "info", + Message: "Creating configuration files...", + }) var wg sync.WaitGroup wg.Add(1) + go func() { defer wg.Done() - writeFile(".husky/commit-msg", []byte(constants.COMMITLINT_CONFIG[string(packageManagerToUse)])) + + writeFile( + ".husky/commit-msg", + []byte(constants.COMMITLINT_CONFIG[string(packageManagerToUse)]), + ) }() wg.Add(1) + go func() { defer wg.Done() + content := "export default { extends: ['@commitlint/config-conventional'] }" + writeFile("commitlint.config.js", []byte(content)) }() wg.Add(1) + go func() { defer wg.Done() + content := `{ "src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}": [ "prettier src --check", "eslint src --max-warnings 0" ] }` + writeFile(".lintstagedrc", []byte(content)) }() wg.Wait() - WriteMessage("success", "Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created successfully", + }) } diff --git a/internal/utils/console.go b/internal/utils/console.go new file mode 100644 index 0000000..e45ca31 --- /dev/null +++ b/internal/utils/console.go @@ -0,0 +1,28 @@ +package utils + +import ( + "fmt" + + "github.com/RaulCatalinas/HuskyBC/internal/types" +) + +type WriteMessageProps struct { + Type types.MessageType + Message string +} + +func WriteMessage(props WriteMessageProps) { + switch props.Type { + case "success": + fmt.Println("\033[32m" + props.Message + "\033[0m") + + case "info": + fmt.Println("\033[36m" + props.Message + "\033[0m") + + case "error": + fmt.Println("\033[31m" + props.Message + "\033[0m") + + case "config": + fmt.Println("\033[37m" + props.Message + "\033[0m") + } +} diff --git a/internal/utils/dependencies.go b/internal/utils/dependencies.go index e577f47..13c42a7 100644 --- a/internal/utils/dependencies.go +++ b/internal/utils/dependencies.go @@ -1,7 +1,6 @@ package utils import ( - "fmt" "os" "os/exec" @@ -26,18 +25,21 @@ func promiseSpawn(command string, args []string) error { func InstallDependencies(props InstallProps) { defer func() { if r := recover(); r != nil { - WriteMessage("error", GetErrorMessage("Dependencies")) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("Dependencies"), + }) + os.Exit(1) } }() - installationCommand, exists := constants.INSTALLATION_COMMANDS[types.PackageManager(props.PackageManagerToUse)] - if !exists { - WriteMessage("error", "Invalid package manager") - os.Exit(1) - } + message := "Installing dependencies using: " + props.PackageManagerToUse + "..." - WriteMessage("info", fmt.Sprintf("Installing dependencies using: %s...", props.PackageManagerToUse)) + WriteMessage(WriteMessageProps{ + Type: "info", + Message: string(message), + }) /* FIXME: Problemas al ejecutan bun, dice esto: error: Module not found "...\HuskyBC\node_modules\husky\bin.mjs" @@ -48,12 +50,24 @@ func InstallDependencies(props InstallProps) { hice una prueba haciendo "bun add husky" directamente y es el mismo caso, lo que quiere decir que HuskyBC no presenta problemas el problema viene desde el mismo bun */ + + installationCommand := constants.INSTALLATION_COMMANDS[props.PackageManagerToUse] + args := append([]string{installationCommand}, append(props.PackagesToInstall, "-D")...) + err := promiseSpawn(string(props.PackageManagerToUse), args) + if err != nil { - WriteMessage("error", GetErrorMessage(err.Error())) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("Dependencies"), + }) + os.Exit(1) } - WriteMessage("success", "Dependencies installed successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "Dependencies installed successfully", + }) } diff --git a/internal/utils/errors.go b/internal/utils/errors.go new file mode 100644 index 0000000..110e248 --- /dev/null +++ b/internal/utils/errors.go @@ -0,0 +1,7 @@ +package utils + +import "github.com/RaulCatalinas/HuskyBC/internal/constants" + +func GetErrorMessage(error string) string { + return constants.ERROR_MESSAGES[error] +} diff --git a/internal/utils/husky-library.go b/internal/utils/husky-library.go index 856e3e8..eed34e4 100644 --- a/internal/utils/husky-library.go +++ b/internal/utils/husky-library.go @@ -13,27 +13,44 @@ type Props struct { } func GenerateHuskyConfig(props Props) { - WriteMessage("info", "Generating Husky's Configuration...") + WriteMessage(WriteMessageProps{ + Type: "config", + Message: "Generating Husky's Configuration...", + }) - InstallDependencies(InstallProps{PackageManagerToUse: props.PackageManagerToUse, PackagesToInstall: []string{"husky"}}) + InstallDependencies(InstallProps{ + PackageManagerToUse: props.PackageManagerToUse, + PackagesToInstall: []string{"husky"}, + }) createHuskyConfigFiles(props.PackageManagerToUse, props.UseCommitlint) - addNecessaryScriptsToPakageJson(props.PackageJsonPath, props.PackageManagerToUse, props.ShouldPublishToNpm) + addNecessaryScriptsToPakageJson( + props.PackageJsonPath, + props.PackageManagerToUse, + props.ShouldPublishToNpm, + ) if props.ShouldPublishToNpm { modifyNpmIgnore(".husky") } - WriteMessage("success", "Husky's configuration generated successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "Husky's configuration generated successfully", + }) } func createHuskyConfigFiles(packageManagerToUse types.PackageManager, useCommitlint bool) { - WriteMessage("info", "Creating configuration file...") + WriteMessage(WriteMessageProps{ + Type: "info", + Message: "Creating configuration file...", + }) CheckinFolderOrFile(constants.PATH_DIR_HUSKY, true) var preCommitFileValue string + if useCommitlint { preCommitFileValue = constants.LINT_STAGED_CONFIG[types.PackageManager(packageManagerToUse)] } else { @@ -42,7 +59,10 @@ func createHuskyConfigFiles(packageManagerToUse types.PackageManager, useCommitl writeFile(constants.PATH_DIR_HUSKY+"/pre-commit", []byte(preCommitFileValue)) - WriteMessage("info", "Configuration file (pre-commit) created successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "Configuration file (pre-commit) created successfully", + }) } type packageJsonScript struct { @@ -50,13 +70,20 @@ type packageJsonScript struct { Value string } -func addNecessaryScriptsToPakageJson(packaJsonPath string, packageManagerToUse types.PackageManager, sholdPublishToNpm bool) { - WriteMessage("info", "Add necessary scripts to "+packaJsonPath) +func addNecessaryScriptsToPakageJson( + packageJsonPath string, + packageManagerToUse types.PackageManager, + shouldPublishToNpm bool, +) { + WriteMessage(WriteMessageProps{ + Type: "info", + Message: "Adding necessary scripts to package.json...", + }) var huskyScriptsForYarn interface{} var scriptsToAdd interface{} - if sholdPublishToNpm { + if shouldPublishToNpm { huskyScriptsForYarn = packageJsonScript{Key: "postintall", Value: "husky"} } else { huskyScriptsForYarn = []packageJsonScript{ @@ -72,5 +99,8 @@ func addNecessaryScriptsToPakageJson(packaJsonPath string, packageManagerToUse t scriptsToAdd = huskyScriptsForYarn } - addScript(addScriptProps{ScriptsToAdd: scriptsToAdd, PackageJsonPath: packaJsonPath}) + addScript(addScriptProps{ + ScriptsToAdd: scriptsToAdd, + PackageJsonPath: packageJsonPath, + }) } diff --git a/internal/utils/npm.go b/internal/utils/npm.go index adf2606..53b545b 100644 --- a/internal/utils/npm.go +++ b/internal/utils/npm.go @@ -9,18 +9,31 @@ import ( func modifyNpmIgnore(filesToAdd interface{}) { defer func() { if r := recover(); r != nil { - WriteMessage("error", GetErrorMessage("NpmIgnoreWrite")) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("NpmIgnoreWrite"), + }) + os.Exit(1) } }() - WriteMessage("info", "Writing in the file \".npmignore\"...") + WriteMessage(WriteMessageProps{ + Type: "info", + Message: "Writing in the file \".npmignore\"...", + }) dir, err := os.Getwd() + if err != nil { - WriteMessage("error", "get wd") + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("GetWorkingDirectory"), + }) + os.Exit(1) } + npmIgnoreFilePath := fmt.Sprintf("%s/.npmignore", dir) if !exists(npmIgnoreFilePath) { @@ -30,6 +43,7 @@ func modifyNpmIgnore(filesToAdd interface{}) { data := readFile(npmIgnoreFilePath) ignoredFiles := string(data) + if ignoredFiles == "" { var filesToWrite string switch v := filesToAdd.(type) { @@ -38,13 +52,21 @@ func modifyNpmIgnore(filesToAdd interface{}) { case []string: filesToWrite = strings.Join(v, "\n") default: - WriteMessage("error", "Invalid type for filesToAdd") + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("InvalidTypeForFilesToAdd"), + }) + os.Exit(1) } writeFile(npmIgnoreFilePath, []byte(filesToWrite)) - WriteMessage("info", "\".npmignore\" file modified successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "\".npmignore\" file modified successfully", + }) + return } @@ -53,6 +75,7 @@ func modifyNpmIgnore(filesToAdd interface{}) { }) trimmedIgnoredFilesArray := make([]string, 0, len(ignoredFilesArray)) + for _, file := range ignoredFilesArray { trimmedFile := strings.TrimSpace(file) if trimmedFile != "" { @@ -66,7 +89,11 @@ func modifyNpmIgnore(filesToAdd interface{}) { case []string: trimmedIgnoredFilesArray = append(trimmedIgnoredFilesArray, v...) default: - WriteMessage("error", "Invalid type for filesToAdd") + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("InvalidTypeForFilesToAdd"), + }) + os.Exit(1) } @@ -74,5 +101,8 @@ func modifyNpmIgnore(filesToAdd interface{}) { writeFile(npmIgnoreFilePath, []byte(finalContent)) - WriteMessage("info", "\".npmignore\" file modified successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "\".npmignore\" file modified successfully", + }) } diff --git a/internal/utils/package-json.go b/internal/utils/package-json.go index 131fd62..7b4d9ff 100644 --- a/internal/utils/package-json.go +++ b/internal/utils/package-json.go @@ -30,12 +30,19 @@ func addScript(props addScriptProps) { defer func() { if r := recover(); r != nil { - WriteMessage("error", GetErrorMessage("AddScript")) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("AddScript"), + }) + os.Exit(1) } }() - WriteMessage("info", "Modifying package.json...") + WriteMessage(WriteMessageProps{ + Type: "info", + Message: "Modifying package.json...", + }) if !existsSection(packageJsonPath, "scripts") { createEmptySection(packageJsonPath, "scripts") @@ -53,11 +60,17 @@ func addScript(props addScriptProps) { case packageJsonScript: scriptsSection[v.Key] = v.Value default: - WriteMessage("error", "Invalid type for scriptsToAdd") + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("InvalidTypeForScriptsToAdd"), + }) os.Exit(1) } jsonMarshalIndentAndWriteFile(packageJsonObj, packageJsonPath) - WriteMessage("success", "Package.json modified successfully") + WriteMessage(WriteMessageProps{ + Type: "success", + Message: "Package.json modified successfully", + }) } diff --git a/internal/utils/user-os.go b/internal/utils/user-os.go index aa4b834..140b539 100644 --- a/internal/utils/user-os.go +++ b/internal/utils/user-os.go @@ -2,18 +2,29 @@ package utils import ( "os" + "strings" ) func createFolder(name string) { os.Mkdir(name, 0750) - WriteMessage("info", "Created folder "+name) + message := "Created folder " + name + + WriteMessage(WriteMessageProps{ + Type: "info", + Message: message, + }) } func createFile(name string) { os.NewFile(0750, name) - WriteMessage("info", "Created file "+name) + message := "Created file " + name + + WriteMessage(WriteMessageProps{ + Type: "info", + Message: message, + }) } func CheckinFolderOrFile(path string, folder bool) { @@ -22,16 +33,25 @@ func CheckinFolderOrFile(path string, folder bool) { if err != nil { var folderType string + if folder { folderType = "folder" } else { folderType = "file" } - WriteMessage("error", "When checking "+folderType+": "+path) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("CheckingFolderOrFile"), + }) if os.IsNotExist(err) { - WriteMessage("error", "Not found "+path) + errorMessage := GetErrorMessage("NotFound") + + WriteMessage(WriteMessageProps{ + Type: "error", + Message: strings.Replace(errorMessage, "{fileName}", path, -1), + }) if folder { createFolder(path) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index dad3e64..266ec30 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -2,19 +2,10 @@ package utils import ( "encoding/json" - "fmt" - "log" "os" + "strings" ) -func GetErrorMessage(context string) string { - return fmt.Sprintf("Error in %s", context) -} - -func WriteMessage(messageType string, message string) { - log.Printf("[%s] %s", messageType, message) -} - func exists(filePath string) bool { _, err := os.Stat(filePath) @@ -23,8 +14,13 @@ func exists(filePath string) bool { func createEmptyFile(fileName string) { file, err := os.Create(fileName) + if err != nil { - WriteMessage("error", GetErrorMessage("CreateFile")) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("CreateEmptyFile"), + }) + os.Exit(1) } defer file.Close() @@ -32,8 +28,15 @@ func createEmptyFile(fileName string) { func readFile(filename string) []byte { dataByte, err := os.ReadFile(filename) + if err != nil { - WriteMessage("error", GetErrorMessage("Read file "+filename)) + errorMessage := GetErrorMessage("ReadFile") + + WriteMessage(WriteMessageProps{ + Type: "error", + Message: strings.Replace(errorMessage, "{fileName}", filename, -1), + }) + os.Exit(1) } @@ -42,15 +45,26 @@ func readFile(filename string) []byte { func writeFile(filename string, newData []byte) { err := os.WriteFile(filename, newData, 0644) + if err != nil { - WriteMessage("error", GetErrorMessage("Write file "+filename)) + errorMessage := GetErrorMessage("WriteFile") + + WriteMessage(WriteMessageProps{ + Type: "error", + Message: strings.Replace(errorMessage, "{fileName}", filename, -1), + }) } } func jsonMarshalIndent(packageJsonObj map[string]interface{}) []byte { newData, err := json.MarshalIndent(packageJsonObj, "", " ") + if err != nil { - WriteMessage("error", GetErrorMessage("Json marshal indent")) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("JsonMarshal"), + }) + os.Exit(1) } @@ -61,8 +75,13 @@ func jsonUnmarshal(data []byte) map[string]interface{} { var packageJsonObj map[string]interface{} err := json.Unmarshal(data, &packageJsonObj) + if err != nil { - WriteMessage("error", GetErrorMessage("Json unmarshal")) + WriteMessage(WriteMessageProps{ + Type: "error", + Message: GetErrorMessage("JsonUnmarshal"), + }) + os.Exit(1) }