autogits/bots-common/tokens.go
2024-08-28 00:45:47 +02:00

66 lines
1.3 KiB
Go

package common
import (
"errors"
"fmt"
"os"
)
const (
RabbitUserEnv = "AMQP_USERNAME"
RabbitPasswordEnv = "AMQP_PASSWORD"
)
var giteaToken string
var obsUser, obsPassword string
var rabbitUser, rabbitPassword string
func RequireGiteaSecretToken() error {
giteaToken = os.Getenv(GiteaTokenEnv)
if len(giteaToken) < 10 {
return errors.New(GiteaTokenEnv + " not provided")
}
err := os.Setenv(GiteaTokenEnv, "")
if err != nil {
return fmt.Errorf("%s: %v", "Cannot reset "+GiteaTokenEnv, err)
}
return nil
}
func GetGiteaToken() string {
return giteaToken
}
func RequireObsSecretToken() error {
obsUser = os.Getenv(ObsUserEnv)
obsPassword = os.Getenv(ObsPasswordEnv)
if len(obsPassword) < 10 || len(obsUser) < 2 {
return fmt.Errorf("Missing OBS authentication: %s %s", ObsUserEnv, ObsPasswordEnv)
}
err := os.Setenv(ObsUserEnv, "")
if err != nil {
return fmt.Errorf("Cannot reset %s: %v", ObsUserEnv, err)
}
err = os.Setenv(ObsPasswordEnv, "")
if err != nil {
return fmt.Errorf("Cannot reset %s: %v", ObsPasswordEnv, err)
}
return nil
}
func RequireRabbitSecrets() error {
rabbitUser = os.Getenv(RabbitUserEnv)
rabbitPassword = os.Getenv(RabbitPasswordEnv)
if len(rabbitUser) == 0 || len(rabbitPassword) == 0 {
return fmt.Errorf("Missing RabbitMQ env variables: %s, %s", RabbitUserEnv, RabbitPasswordEnv)
}
return nil
}