From 3abbcc36ba897a61f64557342a8f21b09e793209 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 14 Dec 2021 09:49:19 -0800 Subject: [PATCH] Linting updates --- figures/figures.go | 9 +++++++-- main.go | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/figures/figures.go b/figures/figures.go index 9b1efea..2631eac 100644 --- a/figures/figures.go +++ b/figures/figures.go @@ -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) } diff --git a/main.go b/main.go index 3b6a135..8ccb1d4 100644 --- a/main.go +++ b/main.go @@ -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