2020-02-14 21:46:02 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-07 03:09:31 +02:00
|
|
|
|
2020-02-17 21:50:02 +01:00
|
|
|
"gitea.com/jolheiser/sip/modules/config"
|
2020-05-07 03:09:31 +02:00
|
|
|
|
2020-02-14 21:46:02 +01:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-27 03:53:11 +01:00
|
|
|
"go.jolheiser.com/beaver"
|
2020-02-14 21:46:02 +01:00
|
|
|
)
|
|
|
|
|
2020-02-18 03:57:51 +01:00
|
|
|
var TokensRemove = cli.Command{
|
2020-02-18 06:27:52 +01:00
|
|
|
Name: "remove",
|
|
|
|
Aliases: []string{"delete"},
|
|
|
|
Usage: "Remove access tokens",
|
|
|
|
Action: doTokenRemove,
|
2020-02-18 03:57:51 +01:00
|
|
|
}
|
2020-02-14 21:46:02 +01:00
|
|
|
|
2020-02-18 03:57:51 +01:00
|
|
|
func doTokenRemove(ctx *cli.Context) error {
|
|
|
|
opts := make([]string, len(config.Tokens))
|
|
|
|
for idx, token := range config.Tokens {
|
|
|
|
opts[idx] = fmt.Sprintf("%s (%s)", token.Name, token.URL)
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
question := &survey.MultiSelect{
|
|
|
|
Message: "Which would you like to remove?",
|
|
|
|
Options: opts,
|
|
|
|
PageSize: 10,
|
|
|
|
}
|
|
|
|
answers := make([]string, 0)
|
|
|
|
|
|
|
|
if err := survey.AskOne(question, &answers); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
idxs := make([]int, len(answers))
|
|
|
|
for idx, answer := range answers {
|
2020-02-18 03:57:51 +01:00
|
|
|
for idy, token := range config.Tokens {
|
|
|
|
if answer == fmt.Sprintf("%s (%s)", token.Name, token.URL) {
|
2020-02-14 21:46:02 +01:00
|
|
|
idxs[len(answers)-idx-1] = idy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, idx := range idxs {
|
2020-02-18 03:57:51 +01:00
|
|
|
config.Tokens = append(config.Tokens[:idx], config.Tokens[idx+1:]...)
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-18 03:57:51 +01:00
|
|
|
beaver.Infof("Removed %d token(s)! Remember to clean up in Gitea if necessary!", len(answers))
|
2020-02-14 21:46:02 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|