2023-03-24 20:41:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2024-06-09 10:44:41 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/cli"
|
2023-03-24 20:41:38 +00:00
|
|
|
"github.com/gofrs/uuid/v3"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-06-30 15:38:33 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-03-24 20:41:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2024-06-09 10:44:41 +00:00
|
|
|
cliTool.Add(cli.RegistryEntry{
|
2023-03-24 20:41:38 +00:00
|
|
|
Name: "api-token",
|
|
|
|
Description: "Generate an api-token to be entered into the config",
|
|
|
|
Params: []string{"<token-name>", "<scope>", "[...scope]"},
|
|
|
|
Run: func(args []string) error {
|
2024-06-09 10:44:41 +00:00
|
|
|
if len(args) < 3 { //nolint:mnd // Just a count of parameters
|
2023-03-24 20:41:38 +00:00
|
|
|
return errors.New("Usage: twitch-bot api-token <token name> <scope> [...scope]")
|
|
|
|
}
|
|
|
|
|
|
|
|
t := configAuthToken{
|
|
|
|
Name: args[1],
|
|
|
|
Modules: args[2:],
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fillAuthToken(&t); err != nil {
|
|
|
|
return errors.Wrap(err, "generating token")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("token", t.Token).Info("Token generated, add this to your config:")
|
|
|
|
if err := yaml.NewEncoder(os.Stdout).Encode(map[string]map[string]configAuthToken{
|
|
|
|
"auth_tokens": {
|
|
|
|
uuid.Must(uuid.NewV4()).String(): t,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrap(err, "printing token info")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|