Skip to content

Commit

Permalink
Add logout command
Browse files Browse the repository at this point in the history
  • Loading branch information
viovanov committed Oct 7, 2021
1 parent dbfa328 commit 68ff06e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/policy/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

type LogoutCmd struct {
Server string `name:"server" short:"s" help:"Server to logout from." default:"opcr.io"`
}

func (c *LogoutCmd) Run(g *Globals) error {
g.App.UI.Normal().
WithStringValue("server", c.Server).
Msg("Logging out.")

err := g.App.RemoveServerCreds(c.Server)
if err != nil {
return err
}

g.App.UI.Normal().Msg("OK.")

<-g.App.Context.Done()
return nil
}
1 change: 1 addition & 0 deletions cmd/policy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var PolicyCLI struct {
Push PushCmd `cmd:"" help:"Push policies to a registry."`
Pull PullCmd `cmd:"" help:"Pull policies from a registry."`
Login LoginCmd `cmd:"" help:"Login to a registry."`
Logout LogoutCmd `cmd:"" help:"Logout from a registry."`
Save SaveCmd `cmd:"" help:"Save a policy to a local bundle tarball."`
Tag TagCmd `cmd:"" help:"Create a new tag for an existing policy."`
Rm RmCmd `cmd:"" help:"Removes a policy from the local registry."`
Expand Down
20 changes: 20 additions & 0 deletions pkg/app/policy_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,23 @@ func (c *PolicyApp) SaveServerCreds(server string, creds config.ServerCredential

return nil
}

func (c *PolicyApp) RemoveServerCreds(server string) error {
defer c.Cancel()
if server == "" {
server = c.Configuration.DefaultDomain
}

if c.Configuration.Servers == nil {
c.Configuration.Servers = map[string]config.ServerCredentials{}
}

c.Configuration.Servers[server] = config.ServerCredentials{}

err := c.Configuration.SaveCreds()
if err != nil {
return errors.Wrap(err, "failed to save server credentials")
}

return nil
}

0 comments on commit 68ff06e

Please sign in to comment.