Skip to content

Commit 8207e7f

Browse files
authoredMay 31, 2024
Merge pull request #4 from JGugino/dev
Grunt - v1.2.1
2 parents f719a99 + 209be2a commit 8207e7f

15 files changed

+282
-164
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
# Go workspace file
2121
go.work
2222

23-
bin/
23+
bin/
24+
dist/

‎.goreleaser.yaml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
4+
# The lines below are called `modelines`. See `:help modeline`
5+
# Feel free to remove those if you don't want/need to use them.
6+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
7+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
8+
9+
version: 1
10+
11+
before:
12+
hooks:
13+
# You may remove this if you don't use go modules.
14+
- go mod tidy
15+
16+
builds:
17+
- env:
18+
- CGO_ENABLED=0
19+
goos:
20+
- linux
21+
- windows
22+
- darwin
23+
24+
archives:
25+
- format: tar.gz
26+
# this name template makes the OS and Arch compatible with the results of `uname`.
27+
name_template: >-
28+
{{ .ProjectName }}_
29+
{{- title .Os }}_
30+
{{- if eq .Arch "amd64" }}x86_64
31+
{{- else if eq .Arch "386" }}i386
32+
{{- else }}{{ .Arch }}{{ end }}
33+
{{- if .Arm }}v{{ .Arm }}{{ end }}
34+
# use zip for windows archives
35+
format_overrides:
36+
- goos: windows
37+
format: zip
38+
39+
changelog:
40+
sort: asc
41+
filters:
42+
exclude:
43+
- "^docs:"
44+
- "^test:"

‎README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Grunt
2-
[![Go Report Card](https://goreportcard.com/badge/github.com/JGugino/grunt)](https://goreportcard.com/report/github.com/JGugino/grunt)       [![GitHub Release](https://img.shields.io/github/v/release/JGugino/grunt?sort=date&display_name=release&style=plastic)](https://github.com/JGugino/grunt/releases/tag/v1.2)
3-
4-
1+
![grunt logo](logo.webp)
2+
<br/>
3+
<br/>
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/JGugino/grunt)](https://goreportcard.com/report/github.com/JGugino/grunt) &nbsp; &nbsp; &nbsp; [![GitHub Release](https://img.shields.io/github/v/release/JGugino/grunt?sort=date&display_name=release&style=plastic)](https://github.com/JGugino/grunt/releases/tag/v1.1)
55

66

77
Grunt is a project templating utility which uses a JSON file to define the structure of the project
@@ -187,4 +187,4 @@ grunt example name="example-project"
187187
*this snippet will execute the 'example' config with the "name" argument inside the current working directory.*
188188

189189
## Accessing Logs
190-
Whenever grunt runs it will logs it's outputs to `general.log`, and any errors that may occur will be logged to `errors.log`. You can access these files either directly by going to the `.grunt/logs` folder inside of your home directory, or you can use the `grunt logs {type}` command and specify the type of log you want to view. So you would run the `grunt logs general` to print out the entire contents of the general log, and `grunt logs error` to view the errors logs.
190+
Whenever grunt runs it will logs it's outputs to `general.log`, and any errors that may occur will be logged to `errors.log`. You can access these files either directly by going to the `.grunt/logs` folder inside of your home directory, or you can use the `grunt log {type}` command and specify the type of log you want to view. So you would run the `grunt log general` to print out the entire contents of the general log, and `grunt log error` to view the errors logs.

‎cmd/config_cmd.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func (cmd *ConfigCmd) Execute(cmdIdentifier string, configsFolder string, workin
1919
config, err := utils.LoadConfig(configsFolder, cmdIdentifier)
2020

2121
if err != nil {
22-
utils.PrintError(fmt.Sprintf("Unable to find config '%s' inside of %s", cmdIdentifier, configsFolder), false)
22+
utils.PrintError(fmt.Sprintf("Unable to find config '%s' inside of %s", cmdIdentifier, configsFolder), false, true)
2323
os.Exit(0)
2424
}
2525

26-
utils.PrintInfo(fmt.Sprintf("Config '%s' has been loaded", cmdIdentifier))
26+
utils.PrintInfo(fmt.Sprintf("Config '%s' has been loaded", cmdIdentifier), true)
2727

2828
//execute config inside current working directory if a path isn't defined
2929
createPath, err := utils.GrabArgFromSlice(os.Args, "-p")
@@ -45,15 +45,15 @@ func (cmd *ConfigCmd) Execute(cmdIdentifier string, configsFolder string, workin
4545

4646
//If it is not found it will display a warning in the terminal and log to the general log file
4747
if err != nil {
48-
utils.PrintWarning(fmt.Sprintf("Defined arg '%s' is unused", arg))
48+
utils.PrintWarning(fmt.Sprintf("Defined arg '%s' is unused", arg), true)
4949
return nil
5050
}
5151

5252
//If it exists add it to the slice of existing arguments
5353
commandArgs = append(commandArgs, cmd)
5454
}
5555

56-
utils.PrintInfo(fmt.Sprintf("Starting grunt in '%s'", createPath.Value))
56+
utils.PrintInfo(fmt.Sprintf("Starting grunt in '%s'", createPath.Value), true)
5757

5858
if !flags.SkipCreation {
5959

@@ -64,9 +64,9 @@ func (cmd *ConfigCmd) Execute(cmdIdentifier string, configsFolder string, workin
6464

6565
utils.HandleError(err, false)
6666

67-
utils.PrintAction("Directories have been created")
67+
utils.PrintAction("Directories have been created", true)
6868
} else {
69-
utils.PrintInfo("## Skipping directory creation ##")
69+
utils.PrintInfo("## Skipping directory creation ##", true)
7070
}
7171

7272
//Check if there is a flag to skip file creation
@@ -76,12 +76,12 @@ func (cmd *ConfigCmd) Execute(cmdIdentifier string, configsFolder string, workin
7676

7777
utils.HandleError(err, false)
7878

79-
utils.PrintAction("Files have been created")
79+
utils.PrintAction("Files have been created", true)
8080
} else {
81-
utils.PrintInfo("## Skipping file creation ##")
81+
utils.PrintInfo("## Skipping file creation ##", true)
8282
}
8383
} else {
84-
utils.PrintInfo("## Skipping directory & file creation ##")
84+
utils.PrintInfo("## Skipping directory & file creation ##", true)
8585
}
8686

8787
//Checks if there is a flag to skip command execution
@@ -90,14 +90,14 @@ func (cmd *ConfigCmd) Execute(cmdIdentifier string, configsFolder string, workin
9090

9191
utils.HandleError(err, false)
9292

93-
utils.PrintAction("All commands have been executed")
93+
utils.PrintAction("All commands have been executed", true)
9494
} else {
95-
utils.PrintInfo("## Skipping command execution ##")
95+
utils.PrintInfo("## Skipping command execution ##", true)
9696
}
9797

9898
timeTook := time.Since(startingTime)
9999

100-
utils.PrintInfo(fmt.Sprintf("Config '%s' execution has completed: %s", cmdIdentifier, timeTook.String()))
100+
utils.PrintInfo(fmt.Sprintf("Config '%s' execution has completed: %s", cmdIdentifier, timeTook.String()), true)
101101

102102
return nil
103103
}

‎cmd/create_cmd.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (cmd *CreateCmd) Execute(args []string, configFolder string, contentFolder
2020
configName := utils.AddConfigExt(args[0])
2121
dirName := args[0]
2222

23-
utils.PrintInfo(fmt.Sprintf("Starting config creation for '%s'", configName))
23+
utils.PrintInfo(fmt.Sprintf("Starting config creation for '%s'", configName), true)
2424

2525
channel := make(chan error)
2626

@@ -63,7 +63,7 @@ func (cmd *CreateCmd) Execute(args []string, configFolder string, contentFolder
6363
return err
6464
}
6565

66-
utils.PrintAction(fmt.Sprintf("Config file '%s' has been created", configName))
66+
utils.PrintAction(fmt.Sprintf("Config file '%s' has been created", configName), true)
6767

6868
//Create the new directory inside then content folder with the specified name
6969
err = utils.CreateDirectory(contentFolder, dirName)
@@ -72,9 +72,9 @@ func (cmd *CreateCmd) Execute(args []string, configFolder string, contentFolder
7272
return err
7373
}
7474

75-
utils.PrintAction(fmt.Sprintf("Content folder '%s' has been created", dirName))
75+
utils.PrintAction(fmt.Sprintf("Content folder '%s' has been created", dirName), true)
7676

77-
utils.PrintInfo(fmt.Sprintf("Config creation for '%s' has completed", configName))
77+
utils.PrintInfo(fmt.Sprintf("Config creation for '%s' has completed", configName), true)
7878

7979
return nil
8080
}

‎cmd/init_cmd.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ type InitCmd struct {
66
}
77

88
func (cmd *InitCmd) Execute(homeDir string, rootFolder string, rootDirExist bool) error {
9-
return utils.CreateInitDirectoriesIfDontExist(homeDir, rootFolder, rootDirExist)
9+
err := utils.CreateInitDirectoriesIfDontExist(homeDir, rootFolder, rootDirExist)
10+
11+
if err != nil {
12+
return err
13+
}
14+
15+
return nil
1016
}

‎cmd/logs_cmd.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6+
"path"
67
"strings"
78

89
"github.com/JGugino/grunt/utils"
9-
"github.com/TwiN/go-color"
1010
)
1111

1212
type LogsCmd struct {
1313
}
1414

15-
func (cmd *LogsCmd) Execute(logsFolder string, args []string) error {
15+
func (cmd *LogsCmd) Execute(configsFolder string, logsFolder string, contentFolder string, args []string) error {
1616
if len(args) <= 0 {
1717
return errors.New("invalid-log-args")
1818
}
@@ -29,7 +29,8 @@ func (cmd *LogsCmd) Execute(logsFolder string, args []string) error {
2929
splitContent := strings.Split(string(logContent), "\n")
3030

3131
for i, v := range splitContent {
32-
fmt.Printf(color.InBlue("[%d] %s\n"), i+1, v)
32+
utils.PrintInfo(fmt.Sprintf("[%d] %s\n", i+1, v), false)
33+
3334
}
3435

3536
return nil
@@ -43,7 +44,32 @@ func (cmd *LogsCmd) Execute(logsFolder string, args []string) error {
4344
splitContent := strings.Split(string(logContent), "\n")
4445

4546
for i, v := range splitContent {
46-
fmt.Printf(color.InRed("[%d] %s\n"), i+1, v)
47+
utils.PrintError(fmt.Sprintf("[%d] %s\n", i+1, v), false, false)
48+
}
49+
return nil
50+
} else if logType == "configs" {
51+
files, err := utils.GetFilesInDirectory(configsFolder)
52+
53+
if err != nil {
54+
return err
55+
}
56+
57+
utils.PrintInfo(fmt.Sprintf("Config Files - %s", configsFolder), true)
58+
utils.PrintInfo("---------------------------------", true)
59+
utils.PrintInfo("Config Name | Has Content Folder", true)
60+
utils.PrintInfo("---------------------------------", true)
61+
for id, f := range files {
62+
fileName := strings.Split(f, ".")[0]
63+
64+
exists := utils.PathExists(path.Join(contentFolder, fileName))
65+
66+
contentExists := "no"
67+
68+
if exists {
69+
contentExists = "yes"
70+
}
71+
72+
utils.PrintInfo(fmt.Sprintf("%d) %s | %s ", id+1, f, contentExists), true)
4773
}
4874
return nil
4975
}

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/JGugino/grunt
22

3-
go 1.22.2
3+
go 1.22.3
44

55
require github.com/TwiN/go-color v1.4.1

‎logo.webp

3.84 KB
Binary file not shown.

‎main.go

+19-15
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,36 @@ import (
99
)
1010

1111
const (
12-
I_INIT = "init"
13-
I_CREATE = "create"
14-
I_LOGS = "logs"
12+
I_INIT = "init"
13+
I_CREATE = "create"
14+
I_LOGS = "log"
15+
I_CONFIGS = "config"
1516
)
1617

1718
func main() {
1819
if len(os.Args) <= 1 {
19-
utils.PrintError("Invalid usage", false)
20-
utils.PrintError("grunt {identifier} {args}", false)
21-
utils.PrintError("* If the path is not defined, the current working directory is used.", true)
22-
os.Exit(0)
20+
utils.PrintError("Invalid usage", false, false)
21+
utils.PrintError("grunt {identifier} {args}", false, false)
22+
utils.PrintError("* If the path is not defined, the current working directory is used.", true, false)
23+
os.Exit(1)
2324
}
2425

2526
//Gets the command identifier
2627
cmdIdentifier := os.Args[1]
28+
cmdArgs := os.Args[2:]
2729

2830
//Determines the users home dir
2931
homeDir, err := os.UserHomeDir()
3032

3133
utils.HandleError(err, true)
3234

35+
//check for .grunt folder and create if it doesn't exist
36+
rootDirExist := utils.PathExists(path.Join(homeDir, ".grunt"))
37+
38+
if !rootDirExist {
39+
utils.PrintError("grunt hasn't been initialized, please run `grunt init`", false, false)
40+
}
41+
3342
//Determines the users current working directory
3443
workingDir, err := os.Getwd()
3544

@@ -41,11 +50,6 @@ func main() {
4150
logsFolder := path.Join(rootFolder, "logs")
4251
contentFolder := path.Join(rootFolder, "content")
4352

44-
//check for .grunt folder and create if it doesn't exist
45-
rootDirExist := utils.PathExists(rootFolder)
46-
47-
utils.HandleError(err, true)
48-
4953
switch cmdIdentifier {
5054
case I_INIT:
5155
//Run the init command to create the root grunt folders
@@ -56,14 +60,14 @@ func main() {
5660
case I_CREATE:
5761
//Run the create command to create a template config file and content folder with the specified name
5862
createCmd := cmd.CreateCmd{}
59-
err := createCmd.Execute(os.Args[2:], configsFolder, contentFolder)
63+
err := createCmd.Execute(cmdArgs, configsFolder, contentFolder)
6064

6165
utils.HandleError(err, true)
6266

6367
case I_LOGS:
64-
//Run the logs command to print out either the general or error logs
68+
//Run the logs command to print out the general or error logs, or the available configs
6569
logsCmd := cmd.LogsCmd{}
66-
err := logsCmd.Execute(logsFolder, os.Args[2:])
70+
err := logsCmd.Execute(configsFolder, logsFolder, contentFolder, cmdArgs)
6771

6872
utils.HandleError(err, true)
6973

‎utils/config_ctrls.go

+5-38
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,6 @@ type ActiveFlags struct {
5151
SkipCommands bool
5252
}
5353

54-
func CreateInitDirectoriesIfDontExist(homeDir string, rootFolder string, exist bool) error {
55-
if !exist {
56-
//Create root dir
57-
err := CreateDirectory(homeDir, ".grunt")
58-
59-
if err != nil {
60-
return err
61-
}
62-
63-
//create config/content/logs folders
64-
err = CreateDirectory(rootFolder, "configs")
65-
if err != nil {
66-
return err
67-
}
68-
69-
err = CreateDirectory(rootFolder, "content")
70-
if err != nil {
71-
return err
72-
}
73-
74-
err = CreateDirectory(rootFolder, "logs")
75-
if err != nil {
76-
return err
77-
}
78-
79-
PrintInfo(fmt.Sprintf("Created '.grunt' directory inside your home directory, %s", rootFolder))
80-
os.Exit(0)
81-
}
82-
83-
PrintInfo(fmt.Sprintf(".grunt directory already exists inside your home directory, %s", rootFolder))
84-
return nil
85-
}
86-
8754
func LoadConfig(path string, configId string) (*ConfigFile, error) {
8855
configContents, err := ReadWholeFile(path, AddConfigExt(configId))
8956

@@ -125,8 +92,8 @@ func DetermineFileContent(config ConfigFile, content string) (string, error) {
12592
return content, nil
12693
}
12794

128-
func (config *ConfigFile) DetermineFlags() ActiveFlags {
129-
return ActiveFlags{
95+
func (config *ConfigFile) DetermineFlags() *ActiveFlags {
96+
return &ActiveFlags{
13097
SkipFiles: StringExistsInSlice(config.Flags, SKIP_FILES_FLAG),
13198
SkipDirs: StringExistsInSlice(config.Flags, SKIP_DIRS_FLAG),
13299
SkipCreation: StringExistsInSlice(config.Flags, SKIP_CREATION_FLAG),
@@ -236,13 +203,13 @@ func (config *ConfigFile) ExecuteCommands(executePath string, definedArgs []Comm
236203
commandReturn := <-channel
237204

238205
if commandReturn.Err != nil {
239-
PrintError(fmt.Sprintf("Command '%s %s' has failed to execute", command.Command, TurnSliceIntoString(cmdArgs)), false)
206+
PrintError(fmt.Sprintf("Command '%s %s' has failed to execute", command.Command, TurnSliceIntoString(cmdArgs)), false, true)
240207
return commandReturn.Err
241208
}
242209

243-
PrintAction(fmt.Sprintf("Command '%s %s' has been executed", command.Command, TurnSliceIntoString(cmdArgs)))
210+
PrintAction(fmt.Sprintf("Command '%s %s' has been executed", command.Command, TurnSliceIntoString(cmdArgs)), true)
244211
if len(commandReturn.Output) > 0 {
245-
PrintAction(fmt.Sprintf("\n###OUTPUT###\n%s", commandReturn.Output))
212+
PrintAction(fmt.Sprintf("\n###OUTPUT###\n%s", commandReturn.Output), true)
246213
}
247214
}
248215
return nil

‎utils/file_ctrls.go

+10-29
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"fmt"
55
"os"
66
"path"
7-
"time"
87
)
98

109
const (
1110
DIR_PERMISSIONS = 0700
1211
CONFIG_EXT = ".json"
1312
)
1413

14+
// Returns true if the path exists and false if it doesnt
1515
func PathExists(path string) bool {
1616
if _, err := os.Stat(path); os.IsNotExist(err) {
1717
return false
@@ -34,7 +34,7 @@ func CreateNewFile(filePath string, fileName string, fileContents string, channe
3434
file, err := os.Create(path.Join(filePath, fileName))
3535

3636
if err != nil {
37-
PrintError(fmt.Sprintf("Unable to create the file %s in path %s", fileName, filePath), false)
37+
PrintError(fmt.Sprintf("Unable to create the file %s in path %s", fileName, filePath), false, true)
3838
channel <- err
3939
return
4040
}
@@ -44,7 +44,7 @@ func CreateNewFile(filePath string, fileName string, fileContents string, channe
4444
_, writeErr := file.WriteString(fileContents)
4545

4646
if writeErr != nil {
47-
PrintError(fmt.Sprintf("Unable to write to the file %s in path %s", fileName, filePath), false)
47+
PrintError(fmt.Sprintf("Unable to write to the file %s in path %s", fileName, filePath), false, true)
4848
channel <- writeErr
4949
return
5050
}
@@ -58,37 +58,18 @@ func CreateDirectory(dirPath string, dirName string) error {
5858
return err
5959
}
6060

61-
func AppendToLogFile(logType string, logContent string) error {
62-
homeDir, err := os.UserHomeDir()
61+
func GetFilesInDirectory(dirPath string) ([]string, error) {
62+
files, err := os.ReadDir(dirPath)
6363

64-
if err != nil {
65-
return err
66-
}
67-
68-
errorLogPath := path.Join(homeDir, ".grunt", "logs", "errors.log")
69-
generalLogPath := path.Join(homeDir, ".grunt", "logs", "general.log")
64+
names := make([]string, 0)
7065

71-
var selectedLogPath string
72-
73-
if logType == "error" {
74-
selectedLogPath = errorLogPath
75-
} else if logType == "general" {
76-
selectedLogPath = generalLogPath
77-
}
78-
79-
log, err := os.OpenFile(selectedLogPath,
80-
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
8166
if err != nil {
82-
return err
67+
return nil, err
8368
}
8469

85-
defer log.Close()
86-
87-
timestampedContent := time.Now().Format("Mon Jan _2 15:04:05 2006") + " - " + logContent + "\n"
88-
89-
if _, err := log.WriteString(timestampedContent); err != nil {
90-
return err
70+
for _, v := range files {
71+
names = append(names, v.Name())
9172
}
9273

93-
return nil
74+
return names, nil
9475
}

‎utils/init_ctrls.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func CreateInitDirectoriesIfDontExist(homeDir string, rootFolder string, exist bool) error {
9+
if !exist {
10+
//Create root dir
11+
err := CreateDirectory(homeDir, ".grunt")
12+
13+
if err != nil {
14+
return err
15+
}
16+
17+
//create config/content/logs folders
18+
err = CreateDirectory(rootFolder, "configs")
19+
if err != nil {
20+
return err
21+
}
22+
23+
err = CreateDirectory(rootFolder, "content")
24+
if err != nil {
25+
return err
26+
}
27+
28+
err = CreateDirectory(rootFolder, "logs")
29+
if err != nil {
30+
return err
31+
}
32+
33+
PrintInfo(fmt.Sprintf("Created '.grunt' directory inside your home directory, %s", rootFolder), true)
34+
os.Exit(0)
35+
}
36+
37+
PrintInfo(fmt.Sprintf(".grunt directory already exists inside your home directory, %s", rootFolder), true)
38+
return nil
39+
}

‎utils/logging.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
"time"
8+
9+
"github.com/TwiN/go-color"
10+
)
11+
12+
// Handles errors and has the ability to exit the program if fatal
13+
func HandleError(err error, fatal bool) {
14+
if err != nil {
15+
if err.Error() == "no-create-name" {
16+
PrintWarning("You must provide a name for the config", true)
17+
os.Exit(1)
18+
} else if err.Error() == "invalid-log-type" {
19+
PrintError("Invalid log type (general, error, or configs)", false, true)
20+
os.Exit(1)
21+
} else if err.Error() == "invalid-log-args" {
22+
PrintError("Invalid log args", false, true)
23+
os.Exit(1)
24+
}
25+
26+
PrintError(err.Error(), false, true)
27+
if fatal {
28+
os.Exit(1)
29+
}
30+
}
31+
}
32+
33+
func AppendToLogFile(logType string, logContent string) error {
34+
homeDir, err := os.UserHomeDir()
35+
36+
if err != nil {
37+
return err
38+
}
39+
40+
errorLogPath := path.Join(homeDir, ".grunt", "logs", "errors.log")
41+
generalLogPath := path.Join(homeDir, ".grunt", "logs", "general.log")
42+
43+
var selectedLogPath string
44+
45+
if logType == "error" {
46+
selectedLogPath = errorLogPath
47+
} else if logType == "general" {
48+
selectedLogPath = generalLogPath
49+
}
50+
51+
log, err := os.OpenFile(selectedLogPath,
52+
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
53+
if err != nil {
54+
return err
55+
}
56+
57+
defer log.Close()
58+
59+
timestampedContent := time.Now().Format("Mon Jan _2 15:04:05 2006") + " - " + logContent + "\n"
60+
61+
if _, err := log.WriteString(timestampedContent); err != nil {
62+
return err
63+
}
64+
65+
return nil
66+
}
67+
68+
// Prints an error in black over a red background and logs it to the logs folder under 'errors.log'
69+
func PrintError(msg string, urgent bool, log bool) {
70+
if urgent {
71+
fmt.Println(color.InBlackOverRed(msg))
72+
if log {
73+
AppendToLogFile("error", " [ERROR] - "+msg)
74+
}
75+
return
76+
}
77+
78+
fmt.Println(color.InRed(msg))
79+
AppendToLogFile("error", " [ERROR] - "+msg)
80+
}
81+
82+
// Prints program info in blue and logs it to the logs folder under 'general.log'
83+
func PrintInfo(msg string, log bool) {
84+
fmt.Println(color.InBlue(msg))
85+
if log {
86+
AppendToLogFile("general", " [INFO] - "+msg)
87+
}
88+
}
89+
90+
// Prints program action in green and logs it to the logs folder under 'general.log'
91+
func PrintAction(msg string, log bool) {
92+
fmt.Println(color.InGreen(msg))
93+
if log {
94+
AppendToLogFile("general", " [ACTION] - "+msg)
95+
}
96+
}
97+
98+
// Prints program warning in black on a yellow background and logs it to the logs folder under 'general.log'
99+
func PrintWarning(msg string, log bool) {
100+
fmt.Println(color.InBlackOverYellow(msg))
101+
if log {
102+
AppendToLogFile("general", " [WARNING] - "+msg)
103+
}
104+
}

‎utils/utils.go

-54
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ package utils
33
import (
44
"errors"
55
"fmt"
6-
"os"
76
"os/exec"
87
"strings"
9-
10-
"github.com/TwiN/go-color"
118
)
129

1310
type CommandArg struct {
@@ -122,54 +119,3 @@ func ExecuteCommand(channel chan CommandReturn, command string, args []string) {
122119

123120
channel <- commandReturn
124121
}
125-
126-
// Handles errors and has the ability to exit the program if fatal
127-
func HandleError(err error, fatal bool) {
128-
if err != nil {
129-
if err.Error() == "no-create-name" {
130-
PrintWarning("You must provide a name for the config")
131-
os.Exit(1)
132-
} else if err.Error() == "invalid-log-type" {
133-
PrintError("Invalid log type (general or error)", false)
134-
os.Exit(1)
135-
} else if err.Error() == "invalid-log-args" {
136-
PrintError("Invalid log args", false)
137-
os.Exit(1)
138-
}
139-
140-
PrintError(err.Error(), false)
141-
if fatal {
142-
os.Exit(1)
143-
}
144-
}
145-
}
146-
147-
// Prints an error in black over a red background and logs it to the logs folder under 'errors.log'
148-
func PrintError(msg string, urgent bool) {
149-
if urgent {
150-
fmt.Println(color.InBlackOverRed(msg))
151-
AppendToLogFile("error", " [ERROR] - "+msg)
152-
return
153-
}
154-
155-
fmt.Println(color.InRed(msg))
156-
AppendToLogFile("error", " [ERROR] - "+msg)
157-
}
158-
159-
// Prints program info in blue and logs it to the logs folder under 'general.log'
160-
func PrintInfo(msg string) {
161-
fmt.Println(color.InBlue(msg))
162-
AppendToLogFile("general", " [INFO] - "+msg)
163-
}
164-
165-
// Prints program action in green and logs it to the logs folder under 'general.log'
166-
func PrintAction(msg string) {
167-
fmt.Println(color.InGreen(msg))
168-
AppendToLogFile("general", " [ACTION] - "+msg)
169-
}
170-
171-
// Prints program warning in black on a yellow background and logs it to the logs folder under 'general.log'
172-
func PrintWarning(msg string) {
173-
fmt.Println(color.InBlackOverYellow(msg))
174-
AppendToLogFile("general", " [WARNING] - "+msg)
175-
}

0 commit comments

Comments
 (0)
Please sign in to comment.