github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/deletecmd/delete_git_token.go (about) 1 package deletecmd 2 3 import ( 4 "fmt" 5 6 "github.com/olli-ai/jx/v2/pkg/cmd/create/options" 7 8 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 9 10 "strings" 11 12 "github.com/jenkins-x/jx-logging/pkg/log" 13 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 14 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 15 "github.com/olli-ai/jx/v2/pkg/util" 16 "github.com/spf13/cobra" 17 ) 18 19 var ( 20 delete_git_token_long = templates.LongDesc(` 21 Deletes one or more git tokens from your local settings 22 `) 23 24 delete_git_token_example = templates.Examples(` 25 # Deletes a Git user token 26 jx delete git token -n local myusername 27 `) 28 ) 29 30 // DeleteGitTokenOptions the options for the create spring command 31 type DeleteGitTokenOptions struct { 32 options.CreateOptions 33 34 ServerFlags opts.ServerFlags 35 } 36 37 // NewCmdDeleteGitToken defines the command 38 func NewCmdDeleteGitToken(commonOpts *opts.CommonOptions) *cobra.Command { 39 options := &DeleteGitTokenOptions{ 40 CreateOptions: options.CreateOptions{ 41 CommonOptions: commonOpts, 42 }, 43 } 44 45 cmd := &cobra.Command{ 46 Use: "token", 47 Short: "Deletes one or more API tokens for a user on a Git server", 48 Aliases: []string{"api-token"}, 49 Long: delete_git_token_long, 50 Example: delete_git_token_example, 51 Run: func(cmd *cobra.Command, args []string) { 52 options.Cmd = cmd 53 options.Args = args 54 err := options.Run() 55 helper.CheckErr(err) 56 }, 57 } 58 options.ServerFlags.AddGitServerFlags(cmd) 59 return cmd 60 } 61 62 // Run implements the command 63 func (o *DeleteGitTokenOptions) Run() error { 64 args := o.Args 65 if len(args) == 0 { 66 return fmt.Errorf("Missing Git user name") 67 } 68 authConfigSvc, err := o.GitAuthConfigService() 69 if err != nil { 70 return err 71 } 72 config := authConfigSvc.Config() 73 74 server, err := o.FindGitServer(config, &o.ServerFlags) 75 if err != nil { 76 return err 77 } 78 for _, username := range args { 79 err = server.DeleteUser(username) 80 if err != nil { 81 return err 82 } 83 } 84 err = authConfigSvc.SaveConfig() 85 if err != nil { 86 return err 87 } 88 log.Logger().Infof("Deleted API tokens for users: %s for Git server %s at %s from local settings", 89 util.ColorInfo(strings.Join(args, ", ")), util.ColorInfo(server.Name), util.ColorInfo(server.URL)) 90 return nil 91 }