Skip to content

Commit

Permalink
feat: add reset command
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jun 3, 2024
1 parent e5b37c0 commit e7d1ebc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 3 deletions.
5 changes: 5 additions & 0 deletions messages/reset/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package reset

const (
ERRORLOGOUT = "Failed to log out during reset process: %s. Check your settings and try again. If the error persists, please contact Azion support."
)
9 changes: 9 additions & 0 deletions messages/reset/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package reset

const (
USAGE = "reset"
SHORTDESCRIPTION = "Resets settings to default"
LONGDESCRIPTION = "Resets your settings to default and deletes personal token if possible"
FLAGHELP = "Displays more information about the reset command"
SUCCESS = "Sucessfully reseted to default settings"
)
2 changes: 1 addition & 1 deletion pkg/cmd/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
return err
}

logger.LogSuccessBad(f.IOStreams.Out, msg.Success)
logger.LogSuccessBye(f.IOStreams.Out, msg.Success)
return nil
},
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/cmd/reset/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package reset

import (
"context"
"fmt"

"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/reset"
api "github.com/aziontech/azion-cli/pkg/api/personal_token"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/token"
"github.com/spf13/cobra"
)

func NewCmd(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: msg.USAGE,
Short: msg.SHORTDESCRIPTION,
Long: msg.LONGDESCRIPTION,
Example: heredoc.Doc(`
$ azion logout --help
`),
RunE: func(cmd *cobra.Command, args []string) error {
settings, err := token.ReadSettings()
if err != nil {
return err
}

if settings.UUID != "" {
client := api.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
err = client.Delete(context.Background(), settings.UUID)
if err != nil {
return fmt.Errorf(msg.ERRORLOGOUT, err.Error())
}
}

settings = token.Settings{}
err = token.WriteSettings(settings)
if err != nil {
return err
}

logger.LogSuccessBye(f.IOStreams.Out, msg.SUCCESS)
return nil
},
}

flags := cmd.Flags()
flags.BoolP("help", "h", false, msg.FLAGHELP)
return cmd
}
2 changes: 2 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/aziontech/azion-cli/pkg/cmd/logout"
logcmd "github.com/aziontech/azion-cli/pkg/cmd/logs"
"github.com/aziontech/azion-cli/pkg/cmd/purge"
"github.com/aziontech/azion-cli/pkg/cmd/reset"
"github.com/aziontech/azion-cli/pkg/cmd/unlink"
"github.com/aziontech/azion-cli/pkg/cmd/update"
"github.com/aziontech/azion-cli/pkg/cmd/whoami"
Expand Down Expand Up @@ -148,6 +149,7 @@ func NewCobraCmd(rootCmd *RootCmd, f *cmdutil.Factory) *cobra.Command {
cobraCmd.AddCommand(version.NewCmd(f))
cobraCmd.AddCommand(whoami.NewCmd(f))
cobraCmd.AddCommand(purge.NewCmd(f))
cobraCmd.AddCommand(reset.NewCmd(f))

return cobraCmd
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"io"

"github.com/fatih/color"
"github.com/aziontech/tablecli"
"github.com/fatih/color"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -119,7 +119,7 @@ func LogSuccess(w io.Writer, message string) {
FInfo(w, formatSuccess("🚀 %s\n", message))
}

func LogSuccessBad(w io.Writer, message string) {
func LogSuccessBye(w io.Writer, message string) {
formatSuccess := color.New(color.FgBlue).SprintfFunc()
FInfo(w, formatSuccess("👋 %s\n", message))
}
Expand Down

0 comments on commit e7d1ebc

Please sign in to comment.