Linting updates

This commit is contained in:
IamTheFij 2021-12-14 09:49:19 -08:00
parent 60f45e6245
commit 3abbcc36ba
2 changed files with 24 additions and 6 deletions

View File

@ -9,6 +9,11 @@ import (
"path/filepath"
)
const (
CONFIG_DIR_PERMS = 0o755
CONFIG_FILE_PERMS = 0o600
)
var ConfigFileNotFoundErr = errors.New("config file for provided path not found")
// fileExists checks if a file exists at a given path
@ -40,7 +45,7 @@ func (fig figuration) GetConfigFilePath(filename string) (string, error) {
// Get or make config dir path
configDir = filepath.Join(configDir, fig.applicationName)
_ = os.MkdirAll(configDir, 0o755)
_ = os.MkdirAll(configDir, CONFIG_DIR_PERMS)
// Get the path to the provided file name within the config directory
configFilePath := filepath.Join(configDir, filename)
@ -84,7 +89,7 @@ func (fig figuration) WriteConfig(name string, data interface{}) error {
return fmt.Errorf("failed converting config to json: %w", err)
}
if err = ioutil.WriteFile(configPath, contents, 0o600); err != nil {
if err = ioutil.WriteFile(configPath, contents, CONFIG_FILE_PERMS); err != nil {
return fmt.Errorf("error writing config to file: %w", err)
}

21
main.go
View File

@ -17,6 +17,10 @@ import (
"git.iamthefij.com/iamthefij/notify-to-slack/figures"
)
const (
BASE_10 = 10
)
var (
applicationName = "notify-to-slack"
version = "dev"
@ -61,10 +65,12 @@ func maybeUsername() string {
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Reads in and does stuff\n")
configPath, err := figures.NewFiguration(applicationName).GetConfigFilePath("")
if err == nil {
fmt.Fprintf(os.Stderr, "Config directory is %s\n", configPath)
}
golf.PrintDefaults()
}
@ -85,10 +91,13 @@ func main() {
if *showHelp {
printUsage()
return
}
if *showVersion {
printVersion()
return
}
@ -99,6 +108,7 @@ func main() {
// User set a new hook url
if *hookURL != "" {
config.HookURL = *hookURL
err = figures.WriteConfig(config)
if err != nil {
panic(fmt.Sprintf("Could not write to config file: %s", err))
@ -117,11 +127,15 @@ func main() {
// Execute nested command
if *command != "" {
c := ShellCommand(*command)
var footer string
output, err := c.CombinedOutput()
color := "good"
output, err := c.CombinedOutput()
if err != nil {
color = "danger"
if exitError, ok := err.(*exec.ExitError); ok {
footer = fmt.Sprintf("status %d", exitError.ExitCode())
} else {
@ -135,7 +149,7 @@ func main() {
AuthorSubname: maybeHostname(),
Text: string(output),
Footer: footer,
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), BASE_10)),
})
}
@ -156,12 +170,11 @@ func main() {
AuthorSubname: maybeHostname(),
Text: message,
Footer: fmt.Sprintf("status %d", *lastStatus),
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), BASE_10)),
})
// Empty out message to avoid duplicating in the message content
message = ""
}
// Maybe prepend an @channel