Skip to content

Commit

Permalink
Fixed logs for permissions issues case
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rdy committed Aug 7, 2023
1 parent 08bd62f commit 577e374
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 160 deletions.
4 changes: 2 additions & 2 deletions cmd/adminStartServer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"log"
"n0rdy.me/remindme/httpserver"
"n0rdy.me/remindme/logger"

"github.com/spf13/cobra"
)
Expand All @@ -15,7 +15,7 @@ var adminStartServerCmd = &cobra.Command{
Use "start" command instead if you need to start the remindme app.`,
Run: func(cmd *cobra.Command, args []string) {
log.Println("adminStartServer command: called")
logger.Log("adminStartServer command: called")
httpserver.Start()
},
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/at.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package cmd
import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"log"
"n0rdy.me/remindme/common"
"n0rdy.me/remindme/httpclient"
"n0rdy.me/remindme/logger"
"n0rdy.me/remindme/utils"
"time"
)
Expand All @@ -22,7 +22,7 @@ The command expects a reminder message to be provided via the "--about" flag - o
List the upcoming reminders with the "list" command.`,
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("at command: called")
logger.Log("at command: called")

reminder, err := parseAtCmd(cmd)
if err != nil {
Expand All @@ -48,11 +48,11 @@ func parseAtCmd(cmd *cobra.Command) (*common.Reminder, error) {

message, err := flags.GetString(common.AboutFlag)
if err != nil {
log.Println("at command: error while parsing flag: "+common.AboutFlag, err)
logger.Log("at command: error while parsing flag: "+common.AboutFlag, err)
return nil, common.ErrWrongFormattedStringFlag(common.AboutFlag)
}
if message == "" {
log.Println("at command: mandatory flag not provided: " + common.AboutFlag)
logger.Log("at command: mandatory flag not provided: " + common.AboutFlag)
return nil, common.ErrInAtCmdNoMessageProvided
}

Expand All @@ -72,27 +72,27 @@ func calcRemindAtForAtFlag(flags *pflag.FlagSet) (time.Time, error) {

t, err := flags.GetString(common.TimeFlag)
if err != nil {
log.Println("at command: error while parsing flag: "+common.TimeFlag, err)
logger.Log("at command: error while parsing flag: "+common.TimeFlag, err)
return now, common.ErrWrongFormattedStringFlag(common.TimeFlag)
}
am, err := flags.GetString(common.AmFlag)
if err != nil {
log.Println("at command: error while parsing flag: "+common.AmFlag, err)
logger.Log("at command: error while parsing flag: "+common.AmFlag, err)
return now, common.ErrWrongFormattedStringFlag(common.AmFlag)
}
pm, err := flags.GetString(common.PmFlag)
if err != nil {
log.Println("at command: error while parsing flag: "+common.PmFlag, err)
logger.Log("at command: error while parsing flag: "+common.PmFlag, err)
return now, common.ErrWrongFormattedStringFlag(common.PmFlag)
}

if t == "" && am == "" && pm == "" {
log.Println("at command: no time flags provided")
logger.Log("at command: no time flags provided")
return now, common.ErrAtCmdTimeNotProvided
}
// more than 1 time-related flag is provided
if (t != "" && am != "") || (t != "" && pm != "") || (am != "" && pm != "") {
log.Println("at command: more than 1 time flag provided")
logger.Log("at command: more than 1 time flag provided")
return now, common.ErrAtCmdInvalidTimeflagsProvided
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"github.com/spf13/cobra"
"log"
"n0rdy.me/remindme/common"
"n0rdy.me/remindme/httpclient"
"n0rdy.me/remindme/logger"
)

type CancelFlags struct {
Expand All @@ -22,7 +22,7 @@ The command expects a reminder ID to be provided via the "--id" flag - otherwise
List the upcoming reminders with the "list" command.`,
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("cancel command: called")
logger.Log("cancel command: called")

cancelFlags, err := parseCancelCmd(cmd)
if err != nil {
Expand All @@ -49,13 +49,13 @@ func parseCancelCmd(cmd *cobra.Command) (*CancelFlags, error) {
isAll := flags.Lookup(common.AllFlag).Changed
id, err := flags.GetInt(common.IdFlag)
if err != nil {
log.Println("cancel command: error while parsing flag: "+common.IdFlag, err)
logger.Log("cancel command: error while parsing flag: "+common.IdFlag, err)
return nil, common.ErrWrongFormattedIntFlag(common.IdFlag)
}

// catches "no flags provided" and "all flags provided" cases
if (id == 0 && !isAll) || (id != 0 && isAll) {
log.Println("cancel command: invalid flags provided")
logger.Log("cancel command: invalid flags provided")
return nil, common.ErrCancelCmdInvalidFlagsProvided
}

Expand Down
26 changes: 13 additions & 13 deletions cmd/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"github.com/spf13/cobra"
"log"
"n0rdy.me/remindme/common"
"n0rdy.me/remindme/httpclient"
"n0rdy.me/remindme/logger"
"n0rdy.me/remindme/utils"
"time"
)
Expand Down Expand Up @@ -35,7 +35,7 @@ Negative integer values are not accepted - the error will be produced in such ca
List the upcoming reminders with the "list" command.`,
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("change command: called")
logger.Log("change command: called")

changeFlags, err := parseChangeCmd(cmd)
if err != nil {
Expand Down Expand Up @@ -75,61 +75,61 @@ func parseChangeCmd(cmd *cobra.Command) (*ChangeFlags, error) {

id, err := flags.GetInt(common.IdFlag)
if err != nil {
log.Println("change command: error while parsing flag: "+common.IdFlag, err)
logger.Log("change command: error while parsing flag: "+common.IdFlag, err)
return nil, common.ErrWrongFormattedIntFlag(common.IdFlag)
}
if id == 0 {
log.Println("change command: mandatory flag not provided: " + common.IdFlag)
logger.Log("change command: mandatory flag not provided: " + common.IdFlag)
return nil, common.ErrChangeCmdIdNotProvided
}

message, err := flags.GetString(common.AboutFlag)
if err != nil {
log.Println("change command: error while parsing flag: "+common.AboutFlag, err)
logger.Log("change command: error while parsing flag: "+common.AboutFlag, err)
return nil, common.ErrWrongFormattedStringFlag(common.AboutFlag)
}

t, err := flags.GetString(common.TimeFlag)
if err != nil {
log.Println("change command: error while parsing flag: "+common.TimeFlag, err)
logger.Log("change command: error while parsing flag: "+common.TimeFlag, err)
return nil, common.ErrWrongFormattedStringFlag(common.TimeFlag)
}

isPostpone := flags.Lookup(common.PostponeFlag).Changed

// no changes provided
if message == "" && t == "" && !isPostpone {
log.Println("change command: no changes provided")
logger.Log("change command: no changes provided")
return nil, common.ErrChangeCmdInvalidFlagsProvided
}
// both "new time" and "postpone" provided
if t != "" && isPostpone {
log.Println("change command: both new time and postpone provided")
logger.Log("change command: both new time and postpone provided")
return nil, common.ErrChangeCmdInvalidTimeFlagsProvided
}

seconds, err := flags.GetInt(common.SecondsFlag)
if err != nil {
log.Println("change command: error while parsing flag: "+common.SecondsFlag, err)
logger.Log("change command: error while parsing flag: "+common.SecondsFlag, err)
return nil, common.ErrWrongFormattedIntFlag(common.SecondsFlag)
}
minutes, err := flags.GetInt(common.MinutesFlag)
if err != nil {
log.Println("change command: error while parsing flag: "+common.MinutesFlag, err)
logger.Log("change command: error while parsing flag: "+common.MinutesFlag, err)
return nil, common.ErrWrongFormattedIntFlag(common.MinutesFlag)
}
hours, err := flags.GetInt(common.HoursFlag)
if err != nil {
log.Println("change command: error while parsing flag: "+common.SecondsFlag, err)
logger.Log("change command: error while parsing flag: "+common.SecondsFlag, err)
return nil, common.ErrWrongFormattedIntFlag(common.HoursFlag)
}

if seconds < 0 || minutes < 0 || hours < 0 {
log.Println("change command: negative values provided for time shift flags")
logger.Log("change command: negative values provided for time shift flags")
return nil, common.ErrChangeCmdInvalidPostponeDuration
}
if isPostpone && seconds == 0 && minutes == 0 && hours == 0 {
log.Println("change command: no values provided for time shift flags")
logger.Log("change command: no values provided for time shift flags")
return nil, common.ErrChangeCmdPostponeDurationNotProvided
}

Expand Down
12 changes: 6 additions & 6 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
"github.com/spf13/cobra"
"log"
"n0rdy.me/remindme/common"
"n0rdy.me/remindme/logger"
"n0rdy.me/remindme/utils"
"os"
)
Expand All @@ -21,7 +21,7 @@ source <(remindme completion)
Please, check the PowerShell documentation (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-7.3) for more information about loading completions for this shell type.`,
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("completion command: called")
logger.Log("completion command: called")
return setCompletionIfPossible()
},
}
Expand All @@ -43,19 +43,19 @@ func setCompletionIfPossible() error {
case common.FishShell:
return rootCmd.GenZshCompletion(os.Stdout)
case "":
log.Println("completion command: unknown shell type error")
logger.Log("completion command: unknown shell type error")
return common.ErrCompletionCmdUnknownShell
default:
log.Println("completion command: unsupported shell type error: " + shellType)
logger.Log("completion command: unsupported shell type error: " + shellType)
return common.ErrCompletionCmdUnsupportedShell(shellType)
}
case common.WindowsOS:
return rootCmd.GenPowerShellCompletion(os.Stdout)
case "":
log.Println("completion command: unknown OS type error")
logger.Log("completion command: unknown OS type error")
return common.ErrCompletionCmdUnknownOS
default:
log.Println("completion command: unsupported OS type error: " + osType)
logger.Log("completion command: unsupported OS type error: " + osType)
return common.ErrCompletionCmdUnsupportedOs(osType)
}
}
12 changes: 6 additions & 6 deletions cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"io"
"log"
"n0rdy.me/remindme/common"
"n0rdy.me/remindme/logger"
"os"
)

Expand All @@ -16,7 +16,7 @@ var docsCmd = &cobra.Command{
Short: "Generate documentation for remindme command",
Long: "Generate documentation for remindme command.",
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("docs command: called")
logger.Log("docs command: called")

dir, err := getDir(cmd)
if err != nil {
Expand All @@ -35,13 +35,13 @@ func init() {
func getDir(cmd *cobra.Command) (string, error) {
dir, err := cmd.Flags().GetString(common.DirFlag)
if err != nil {
log.Println("docs command: error while parsing flag: "+common.DirFlag, err)
logger.Log("docs command: error while parsing flag: "+common.DirFlag, err)
return "", common.ErrWrongFormattedStringFlag(common.DirFlag)
}

if dir == "" {
if dir, err = os.MkdirTemp("", "remindme"); err != nil {
log.Println("docs command: error while creating temp dir", err)
logger.Log("docs command: error while creating temp dir", err)
return "", common.ErrDocsCmdOnDirCreation
}
}
Expand All @@ -50,12 +50,12 @@ func getDir(cmd *cobra.Command) (string, error) {

func generateDocs(out io.Writer, dir string) error {
if err := doc.GenMarkdownTree(rootCmd, dir); err != nil {
log.Println("docs command: error while generating docs", err)
logger.Log("docs command: error while generating docs", err)
return common.ErrDocsCmdOnDocsGeneration
}
_, err := fmt.Fprintf(out, "Documentation successfully created in %s\n", dir)
if err != nil {
log.Println("docs command: error while writing to output", err)
logger.Log("docs command: error while writing to output", err)
}
return nil
}
18 changes: 9 additions & 9 deletions cmd/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"github.com/spf13/pflag"
"log"
"n0rdy.me/remindme/common"
"n0rdy.me/remindme/httpclient"
"n0rdy.me/remindme/logger"
"n0rdy.me/remindme/utils"
"time"

Expand All @@ -24,7 +24,7 @@ The command expects a reminder message to be provided via the "--about" flag - o
List the upcoming reminders with the "list" command.`,
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("in command: called")
logger.Log("in command: called")

reminder, err := parseInCmd(cmd)
if err != nil {
Expand All @@ -50,11 +50,11 @@ func parseInCmd(cmd *cobra.Command) (*common.Reminder, error) {

message, err := flags.GetString(common.AboutFlag)
if err != nil {
log.Println("in command: error while parsing flag: "+common.AboutFlag, err)
logger.Log("in command: error while parsing flag: "+common.AboutFlag, err)
return nil, common.ErrWrongFormattedStringFlag(common.AboutFlag)
}
if message == "" {
log.Println("in command: mandatory flag not provided: " + common.AboutFlag)
logger.Log("in command: mandatory flag not provided: " + common.AboutFlag)
return nil, common.ErrInAtCmdNoMessageProvided
}

Expand All @@ -74,26 +74,26 @@ func calcRemindAtForInFlag(flags *pflag.FlagSet) (time.Time, error) {

seconds, err := flags.GetInt(common.SecondsFlag)
if err != nil {
log.Println("in command: error while parsing flag: "+common.SecondsFlag, err)
logger.Log("in command: error while parsing flag: "+common.SecondsFlag, err)
return now, common.ErrWrongFormattedIntFlag(common.SecondsFlag)
}
minutes, err := flags.GetInt(common.MinutesFlag)
if err != nil {
log.Println("in command: error while parsing flag: "+common.MinutesFlag, err)
logger.Log("in command: error while parsing flag: "+common.MinutesFlag, err)
return now, common.ErrWrongFormattedIntFlag(common.MinutesFlag)
}
hours, err := flags.GetInt(common.HoursFlag)
if err != nil {
log.Println("in command: error while parsing flag: "+common.HoursFlag, err)
logger.Log("in command: error while parsing flag: "+common.HoursFlag, err)
return now, common.ErrWrongFormattedIntFlag(common.HoursFlag)
}

if seconds == 0 && minutes == 0 && hours == 0 {
log.Println("in command: no duration flags provided")
logger.Log("in command: no duration flags provided")
return now, common.ErrInCmdDurationNotProvided
}
if seconds < 0 || minutes < 0 || hours < 0 {
log.Println("in command: negative duration flags provided")
logger.Log("in command: negative duration flags provided")
return now, common.ErrInCmdInvalidDuration
}

Expand Down
Loading

0 comments on commit 577e374

Please sign in to comment.