refactor: log errors

This commit is contained in:
Stavros
2025-01-24 18:24:20 +02:00
parent 35854f5ce4
commit f61b6dbad4
6 changed files with 19 additions and 28 deletions

View File

@@ -1,7 +1,6 @@
package cmd
import (
"os"
cmd "tinyauth/cmd/user"
"tinyauth/internal/api"
"tinyauth/internal/auth"
@@ -38,7 +37,6 @@ var rootCmd = &cobra.Command{
if config.UsersFile == "" && config.Users == "" {
log.Fatal().Msg("No users provided")
os.Exit(1)
}
usersString := config.Users
@@ -108,14 +106,12 @@ func Execute() {
err := rootCmd.Execute()
if err != nil {
log.Fatal().Err(err).Msg("Failed to execute command")
os.Exit(1)
}
}
func HandleError(err error, msg string) {
if err != nil {
log.Fatal().Err(err).Msg(msg)
os.Exit(1)
}
}

View File

@@ -3,7 +3,6 @@ package create
import (
"errors"
"fmt"
"os"
"strings"
"github.com/charmbracelet/huh"
@@ -18,9 +17,9 @@ var password string
var docker bool
var CreateCmd = &cobra.Command{
Use: "create",
Use: "create",
Short: "Create a user",
Long: `Create a user either interactively or by passing flags.`,
Long: `Create a user either interactively or by passing flags.`,
Run: func(cmd *cobra.Command, args []string) {
if interactive {
form := huh.NewForm(
@@ -47,13 +46,11 @@ var CreateCmd = &cobra.Command{
if formErr != nil {
log.Fatal().Err(formErr).Msg("Form failed")
os.Exit(1)
}
}
if username == "" || password == "" {
log.Error().Msg("Username and password cannot be empty")
os.Exit(1)
}
log.Info().Str("username", username).Str("password", password).Bool("docker", docker).Msg("Creating user")
@@ -62,7 +59,6 @@ var CreateCmd = &cobra.Command{
if passwordErr != nil {
log.Fatal().Err(passwordErr).Msg("Failed to hash password")
os.Exit(1)
}
passwordString := string(passwordByte)
@@ -80,4 +76,4 @@ func init() {
CreateCmd.Flags().BoolVar(&docker, "docker", false, "Format output for docker")
CreateCmd.Flags().StringVar(&username, "username", "", "Username")
CreateCmd.Flags().StringVar(&password, "password", "", "Password")
}
}

View File

@@ -2,7 +2,6 @@ package verify
import (
"errors"
"os"
"strings"
"github.com/charmbracelet/huh"
@@ -18,9 +17,9 @@ var docker bool
var user string
var VerifyCmd = &cobra.Command{
Use: "verify",
Use: "verify",
Short: "Verify a user is set up correctly",
Long: `Verify a user is set up correctly meaning that it has a correct password.`,
Long: `Verify a user is set up correctly meaning that it has a correct password.`,
Run: func(cmd *cobra.Command, args []string) {
if interactive {
form := huh.NewForm(
@@ -53,23 +52,19 @@ var VerifyCmd = &cobra.Command{
if formErr != nil {
log.Fatal().Err(formErr).Msg("Form failed")
os.Exit(1)
}
}
if username == "" || password == "" || user == "" {
log.Error().Msg("Username, password and user cannot be empty")
os.Exit(1)
if username == "" || password == "" || user == "" {
log.Fatal().Msg("Username, password and user cannot be empty")
}
log.Info().Str("user", user).Str("username", username).Str("password", password).Bool("docker", docker).Msg("Verifying user")
userSplit := strings.Split(user, ":")
if userSplit[1] == "" {
log.Error().Msg("User is not formatted correctly")
os.Exit(1)
log.Fatal().Msg("User is not formatted correctly")
}
if docker {
@@ -79,8 +74,7 @@ var VerifyCmd = &cobra.Command{
verifyErr := bcrypt.CompareHashAndPassword([]byte(userSplit[1]), []byte(password))
if verifyErr != nil || username != userSplit[0] {
log.Error().Msg("Username or password incorrect")
os.Exit(1)
log.Fatal().Msg("Username or password incorrect")
} else {
log.Info().Msg("Verification successful")
}
@@ -93,4 +87,4 @@ func init() {
VerifyCmd.Flags().StringVar(&username, "username", "", "Username")
VerifyCmd.Flags().StringVar(&password, "password", "", "Password")
VerifyCmd.Flags().StringVar(&user, "user", "", "Hash (user:hash combination)")
}
}