Update linting

This commit is contained in:
IamTheFij 2021-02-09 10:20:38 -08:00
parent 03399ac35d
commit 461fa0f4e7
4 changed files with 75 additions and 42 deletions

29
.golangci.yml Normal file
View File

@ -0,0 +1,29 @@
---
linters:
enable:
- asciicheck
- bodyclose
- dogsled
- dupl
- exhaustive
- gochecknoinits
- gocognit
- gocritic
- gocyclo
- goerr113
- gofumpt
- goimports
- gomnd
- goprintffuncname
- gosec
- interfacer
- maligned
- misspell
- nakedret
- nestif
- nlreturn
- noctx
- unparam
- wsl
disable:
- gochecknoglobals

14
auth.go
View File

@ -14,11 +14,17 @@ import (
) )
var ( var (
// These are set via build flags but can be overriden via environment variables. // These are set via build flags but can be overridden via environment variables.
defaultClientID = "" defaultClientID = ""
defaultClientSecret = "" defaultClientSecret = ""
) )
const (
httpReadTimeout = 5 * time.Second
httpWriteTimeout = 10 * time.Second
httpIdleTimeout = 120 * time.Second
)
type slackApp struct { type slackApp struct {
clientID, clientSecret, redirectURI string clientID, clientSecret, redirectURI string
scopes, userScopes []string scopes, userScopes []string
@ -45,9 +51,9 @@ func (app slackApp) listenForCode() (string, error) {
// Also, should generate TLS certificate to use since https is a required scheme // Also, should generate TLS certificate to use since https is a required scheme
server := http.Server{ server := http.Server{
Addr: app.listenHost, Addr: app.listenHost,
ReadTimeout: 5 * time.Second, ReadTimeout: httpReadTimeout,
WriteTimeout: 10 * time.Second, WriteTimeout: httpWriteTimeout,
IdleTimeout: 120 * time.Second, IdleTimeout: httpIdleTimeout,
} }
http.HandleFunc(app.listenPath, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc(app.listenPath, func(w http.ResponseWriter, r *http.Request) {

View File

@ -10,9 +10,7 @@ import (
"path/filepath" "path/filepath"
) )
var ( var errUnknownDomain = errors.New("unknown domain")
errUnknownDomain = errors.New("unknown domain")
)
type configData struct { type configData struct {
DefaultDomain string DefaultDomain string
@ -29,7 +27,7 @@ func getConfigFilePath(filename string) (string, error) {
} }
configDir = filepath.Join(configDir, configApplicationName) configDir = filepath.Join(configDir, configApplicationName)
_ = os.MkdirAll(configDir, 0755) _ = os.MkdirAll(configDir, 0o755)
configFile := filepath.Join(configDir, filename) configFile := filepath.Join(configDir, filename)
// Handle migration of old config file path // Handle migration of old config file path
@ -46,6 +44,7 @@ func getConfigFilePath(filename string) (string, error) {
if fileExists(legacyConfigFile) { if fileExists(legacyConfigFile) {
log.Printf("Migrating config from %s to %s\n", legacyConfigFile, configFile) log.Printf("Migrating config from %s to %s\n", legacyConfigFile, configFile)
err = os.Rename(legacyConfigFile, configFile) err = os.Rename(legacyConfigFile, configFile)
if err != nil { if err != nil {
err = fmt.Errorf( err = fmt.Errorf(
@ -98,7 +97,7 @@ func writeConfig(config configData) 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, 0600); err != nil { if err = ioutil.WriteFile(configPath, contents, 0o600); err != nil {
return fmt.Errorf("error writing config to file: %w", err) return fmt.Errorf("error writing config to file: %w", err)
} }

45
main.go
View File

@ -10,23 +10,19 @@ import (
"github.com/slack-go/slack" "github.com/slack-go/slack"
) )
var ( var version = "dev"
version = "dev"
)
// statusInfo contains all args passed from the command line // statusInfo contains all args passed from the command line
type statusInfo struct { type statusInfo struct {
// status contents
emoji, statusText string emoji, statusText string
duration time.Duration duration time.Duration
snooze bool snooze bool
}
// domain and login management // commandOptions contains non-status options passed to the command
login, makeDefault bool type commandOptions struct {
login, makeDefault, showVersion bool
domain string domain string
// other
showVersion bool
} }
// getExipirationTime returns epoch time that status should expire from the duration. // getExipirationTime returns epoch time that status should expire from the duration.
@ -74,7 +70,7 @@ func readDurationArgs(args []string) ([]string, *time.Duration) {
} }
// readFlags will read all flags off the command line. // readFlags will read all flags off the command line.
func readFlags() statusInfo { func readFlags() (statusInfo, commandOptions) {
// Non-status flags // Non-status flags
login := flag.Bool("login", false, "login to a Slack workspace") login := flag.Bool("login", false, "login to a Slack workspace")
domain := flag.String("domain", "", "domain to set status on") domain := flag.String("domain", "", "domain to set status on")
@ -115,7 +111,7 @@ func readFlags() statusInfo {
snooze: *snooze, snooze: *snooze,
emoji: *emoji, emoji: *emoji,
statusText: statusText, statusText: statusText,
}, commandOptions{
login: *login, login: *login,
domain: *domain, domain: *domain,
makeDefault: *makeDefault, makeDefault: *makeDefault,
@ -152,6 +148,7 @@ func loginAndSave(domain string) (*slack.Client, error) {
// getClient returns a client either via the provided login or default login // getClient returns a client either via the provided login or default login
func getClient(domain string) (*slack.Client, error) { func getClient(domain string) (*slack.Client, error) {
var accessToken string var accessToken string
var err error var err error
if domain == "" { if domain == "" {
@ -170,21 +167,23 @@ func getClient(domain string) (*slack.Client, error) {
} }
func main() { func main() {
args := readFlags() status, options := readFlags()
if args.showVersion { if options.showVersion {
fmt.Println("version:", version) fmt.Println("version:", version)
return return
} }
var client *slack.Client var client *slack.Client
var err error var err error
// If the new-auth flag is present, force an auth flow // If the new-auth flag is present, force an auth flow
if args.login { if options.login {
client, err = loginAndSave(args.domain) client, err = loginAndSave(options.domain)
} else { } else {
client, err = getClient(args.domain) client, err = getClient(options.domain)
} }
// We encountered some error in logging in // We encountered some error in logging in
@ -193,21 +192,21 @@ func main() {
log.Fatal(fmt.Errorf("failed to get or save client: %w", err)) log.Fatal(fmt.Errorf("failed to get or save client: %w", err))
} }
// If a domain is provided and asked to make deafult, save it to config // If a domain is provided and asked to make default, save it to config
if args.makeDefault && args.domain != "" { if options.makeDefault && options.domain != "" {
if err = saveDefaultLogin(args.domain); err != nil { if err = saveDefaultLogin(options.domain); err != nil {
log.Fatal(fmt.Errorf("failed saving default domain %s: %w", args.domain, err)) log.Fatal(fmt.Errorf("failed saving default domain %s: %w", options.domain, err))
} }
} }
err = client.SetUserCustomStatus(args.statusText, args.emoji, args.getExpirationTime()) err = client.SetUserCustomStatus(status.statusText, status.emoji, status.getExpirationTime())
if err != nil { if err != nil {
fmt.Println("error setting status") fmt.Println("error setting status")
panic(err) panic(err)
} }
if args.snooze { if status.snooze {
_, err = client.SetSnooze(int(args.duration.Minutes())) _, err = client.SetSnooze(int(status.duration.Minutes()))
if err != nil { if err != nil {
fmt.Println("error setting snooze") fmt.Println("error setting snooze")
panic(err) panic(err)