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 (
// 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 = ""
defaultClientSecret = ""
)
const (
httpReadTimeout = 5 * time.Second
httpWriteTimeout = 10 * time.Second
httpIdleTimeout = 120 * time.Second
)
type slackApp struct {
clientID, clientSecret, redirectURI 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
server := http.Server{
Addr: app.listenHost,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
ReadTimeout: httpReadTimeout,
WriteTimeout: httpWriteTimeout,
IdleTimeout: httpIdleTimeout,
}
http.HandleFunc(app.listenPath, func(w http.ResponseWriter, r *http.Request) {

View File

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

65
main.go
View File

@ -10,23 +10,19 @@ import (
"github.com/slack-go/slack"
)
var (
version = "dev"
)
var version = "dev"
// statusInfo contains all args passed from the command line
type statusInfo struct {
// status contents
emoji, statusText string
duration time.Duration
snooze bool
}
// domain and login management
login, makeDefault bool
domain string
// other
showVersion bool
// commandOptions contains non-status options passed to the command
type commandOptions struct {
login, makeDefault, showVersion bool
domain string
}
// 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.
func readFlags() statusInfo {
func readFlags() (statusInfo, commandOptions) {
// Non-status flags
login := flag.Bool("login", false, "login to a Slack workspace")
domain := flag.String("domain", "", "domain to set status on")
@ -111,16 +107,16 @@ func readFlags() statusInfo {
statusText := strings.Join(args, " ")
return statusInfo{
duration: *duration,
snooze: *snooze,
emoji: *emoji,
statusText: statusText,
login: *login,
domain: *domain,
makeDefault: *makeDefault,
showVersion: *showVersion,
}
duration: *duration,
snooze: *snooze,
emoji: *emoji,
statusText: statusText,
}, commandOptions{
login: *login,
domain: *domain,
makeDefault: *makeDefault,
showVersion: *showVersion,
}
}
// loginAndSave will return a client after a new login flow and save the results
@ -152,6 +148,7 @@ func loginAndSave(domain string) (*slack.Client, error) {
// getClient returns a client either via the provided login or default login
func getClient(domain string) (*slack.Client, error) {
var accessToken string
var err error
if domain == "" {
@ -170,21 +167,23 @@ func getClient(domain string) (*slack.Client, error) {
}
func main() {
args := readFlags()
status, options := readFlags()
if args.showVersion {
if options.showVersion {
fmt.Println("version:", version)
return
}
var client *slack.Client
var err error
// If the new-auth flag is present, force an auth flow
if args.login {
client, err = loginAndSave(args.domain)
if options.login {
client, err = loginAndSave(options.domain)
} else {
client, err = getClient(args.domain)
client, err = getClient(options.domain)
}
// 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))
}
// If a domain is provided and asked to make deafult, save it to config
if args.makeDefault && args.domain != "" {
if err = saveDefaultLogin(args.domain); err != nil {
log.Fatal(fmt.Errorf("failed saving default domain %s: %w", args.domain, err))
// If a domain is provided and asked to make default, save it to config
if options.makeDefault && options.domain != "" {
if err = saveDefaultLogin(options.domain); err != nil {
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 {
fmt.Println("error setting status")
panic(err)
}
if args.snooze {
_, err = client.SetSnooze(int(args.duration.Minutes()))
if status.snooze {
_, err = client.SetSnooze(int(status.duration.Minutes()))
if err != nil {
fmt.Println("error setting snooze")
panic(err)