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" "path/filepath"
) )
const (
CONFIG_DIR_PERMS = 0o755
CONFIG_FILE_PERMS = 0o600
)
var ConfigFileNotFoundErr = errors.New("config file for provided path not found") var ConfigFileNotFoundErr = errors.New("config file for provided path not found")
// fileExists checks if a file exists at a given path // 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 // Get or make config dir path
configDir = filepath.Join(configDir, fig.applicationName) 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 // Get the path to the provided file name within the config directory
configFilePath := filepath.Join(configDir, filename) 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) 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) 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" "git.iamthefij.com/iamthefij/notify-to-slack/figures"
) )
const (
BASE_10 = 10
)
var ( var (
applicationName = "notify-to-slack" applicationName = "notify-to-slack"
version = "dev" version = "dev"
@ -61,10 +65,12 @@ func maybeUsername() string {
func printUsage() { func printUsage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Reads in and does stuff\n") fmt.Fprintf(os.Stderr, "Reads in and does stuff\n")
configPath, err := figures.NewFiguration(applicationName).GetConfigFilePath("") configPath, err := figures.NewFiguration(applicationName).GetConfigFilePath("")
if err == nil { if err == nil {
fmt.Fprintf(os.Stderr, "Config directory is %s\n", configPath) fmt.Fprintf(os.Stderr, "Config directory is %s\n", configPath)
} }
golf.PrintDefaults() golf.PrintDefaults()
} }
@ -85,10 +91,13 @@ func main() {
if *showHelp { if *showHelp {
printUsage() printUsage()
return return
} }
if *showVersion { if *showVersion {
printVersion() printVersion()
return return
} }
@ -99,6 +108,7 @@ func main() {
// User set a new hook url // User set a new hook url
if *hookURL != "" { if *hookURL != "" {
config.HookURL = *hookURL config.HookURL = *hookURL
err = figures.WriteConfig(config) err = figures.WriteConfig(config)
if err != nil { if err != nil {
panic(fmt.Sprintf("Could not write to config file: %s", err)) panic(fmt.Sprintf("Could not write to config file: %s", err))
@ -117,11 +127,15 @@ func main() {
// Execute nested command // Execute nested command
if *command != "" { if *command != "" {
c := ShellCommand(*command) c := ShellCommand(*command)
var footer string var footer string
output, err := c.CombinedOutput()
color := "good" color := "good"
output, err := c.CombinedOutput()
if err != nil { if err != nil {
color = "danger" color = "danger"
if exitError, ok := err.(*exec.ExitError); ok { if exitError, ok := err.(*exec.ExitError); ok {
footer = fmt.Sprintf("status %d", exitError.ExitCode()) footer = fmt.Sprintf("status %d", exitError.ExitCode())
} else { } else {
@ -135,7 +149,7 @@ func main() {
AuthorSubname: maybeHostname(), AuthorSubname: maybeHostname(),
Text: string(output), Text: string(output),
Footer: footer, 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(), AuthorSubname: maybeHostname(),
Text: message, Text: message,
Footer: fmt.Sprintf("status %d", *lastStatus), 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 // Empty out message to avoid duplicating in the message content
message = "" message = ""
} }
// Maybe prepend an @channel // Maybe prepend an @channel