1
0
Fork 0

Move all the configuration stuff into cfg package

main
Daniele Tricoli 9 months ago
parent a1b10758cd
commit a4027c4271
  1. 78
      cfg/environ.go
  2. 10
      cfg/environ_test.go
  3. 42
      cmd/run.go
  4. 17
      cmd/run_test.go

@ -8,16 +8,42 @@ import (
)
const (
MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN"
MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS"
MASTODON_TOOT_FOOTER = "MASTODON_TOOT_FOOTER"
MASTODON_TOOT_MAX_CHARACTERS = "MASTODON_TOOT_MAX_CHARACTERS"
MASTODON_TOOT_VISIBILITY = "MASTODON_TOOT_VISIBILITY"
TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN"
TELEGRAM_CHAT_ID = "TELEGRAM_CHAT_ID"
TELEGRAM_DEBUG = "TELEGRAM_DEBUG"
mastodonAccessToken = "MASTODON_ACCESS_TOKEN"
mastodonServerAddress = "MASTODON_SERVER_ADDRESS"
mastodonTootFooter = "MASTODON_TOOT_FOOTER"
mastodonTootMaxCharacters = "MASTODON_TOOT_MAX_CHARACTERS"
mastodonTootVisibility = "MASTODON_TOOT_VISIBILITY"
telegramBotToken = "TELEGRAM_BOT_TOKEN"
telegramChatID = "TELEGRAM_CHAT_ID"
telegramDebug = "TELEGRAM_DEBUG"
)
func GetMastodonAccessToken() string {
return os.Getenv(mastodonAccessToken)
}
func GetMastodonServerAddress() string {
return os.Getenv(mastodonServerAddress)
}
func GetMastodonTootFooter() string {
return os.Getenv(mastodonTootFooter)
}
// Parse Mastodon max characters and return 500 as default in case of errors.
func parseMastodonMaxCharacters(s string) int {
if n, err := strconv.ParseUint(s, 10, 32); err == nil {
return int(n)
}
return 500
}
// Return the max characters allowed for toots.
func GetMastodonMaxCharacters() int {
return parseMastodonMaxCharacters(os.Getenv(mastodonTootMaxCharacters))
}
// Check the specified Mastodon visibility and return it if valid or return
// unlisted if it's not valid.
// The specified string will be cheched case unsensitive.
@ -35,18 +61,38 @@ func parseMastodonVisibility(s string) string {
// Return configured Mastodon visibility for toot.
func GetMastodonVisibility() string {
return parseMastodonVisibility(os.Getenv(MASTODON_TOOT_VISIBILITY))
return parseMastodonVisibility(os.Getenv(mastodonTootVisibility))
}
// Parse Mastodon max characters and return 500 as default in case of errors.
func parseMastodonMaxCharacters(s string) int {
if n, err := strconv.ParseUint(s, 10, 32); err == nil {
return int(n)
func parseBoolOrFalse(s string) bool {
r, err := strconv.ParseBool(s)
if err != nil {
return false
}
return 500
return r
}
func GetMastodonMaxCharacters() int {
return parseMastodonMaxCharacters(os.Getenv(MASTODON_TOOT_MAX_CHARACTERS))
// Return if Telegram bot will run in debug mode or not.
func GetTelegramDebug() bool {
return parseBoolOrFalse(os.Getenv(telegramDebug))
}
func GetTelegramBotToken() string {
return os.Getenv(telegramBotToken)
}
// Parse the telegram chat or return 0.
func parseTelegramChatID(s string) int64 {
r, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return r
}
// Return the configured Telegram chat ID.
func GetTelegramChatID() int64 {
return parseTelegramChatID(os.Getenv(telegramChatID))
}

@ -28,3 +28,13 @@ func TestParseMastodonMaxCharacters(t *testing.T) {
assert.Equal(t, parseMastodonMaxCharacters("-42"), 500)
assert.Equal(t, parseMastodonMaxCharacters("hello"), 500)
}
func TestParseBoolOrFalse(t *testing.T) {
assert.Equal(t, parseBoolOrFalse("True"), true)
assert.Equal(t, parseBoolOrFalse("TRUE"), true)
assert.Equal(t, parseBoolOrFalse("true"), true)
assert.Equal(t, parseBoolOrFalse(""), false)
assert.Equal(t, parseBoolOrFalse("False"), false)
assert.Equal(t, parseBoolOrFalse("FALSE"), false)
assert.Equal(t, parseBoolOrFalse("false"), false)
}

@ -5,8 +5,6 @@ import (
"io"
"log"
"net/http"
"os"
"strconv"
mastodonapi "github.com/cking/go-mastodon"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
@ -17,16 +15,6 @@ import (
"noa.mornie.org/eriol/telegram-group2mastodon/utils"
)
const (
MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN"
MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS"
MASTODON_TOOT_FOOTER = "MASTODON_TOOT_FOOTER"
MASTODON_TOOT_MAX_CHARACTERS = "MASTODON_TOOT_MAX_CHARACTERS"
TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN"
TELEGRAM_CHAT_ID = "TELEGRAM_CHAT_ID"
TELEGRAM_DEBUG = "TELEGRAM_DEBUG"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
@ -36,21 +24,21 @@ var runCmd = &cobra.Command{
Every messages posted in the Telegram groups the bot is in will be posted into
the specified Mastodon account.`,
Run: func(cmd *cobra.Command, args []string) {
mastodonInstance := os.Getenv(MASTODON_SERVER_ADDRESS)
mastodonInstance := cfg.GetMastodonServerAddress()
c := mastodonapi.NewClient(&mastodonapi.Config{
Server: mastodonInstance,
AccessToken: os.Getenv(MASTODON_ACCESS_TOKEN),
AccessToken: cfg.GetMastodonAccessToken(),
})
log.Println("Crating a new client for mastondon istance:", mastodonInstance)
allowedTelegramChat := parseTelegramChatID(os.Getenv(TELEGRAM_CHAT_ID))
allowedTelegramChat := cfg.GetTelegramChatID()
log.Println("Allowed telegram chat id:", allowedTelegramChat)
bot, err := tgbotapi.NewBotAPI(os.Getenv(TELEGRAM_BOT_TOKEN))
bot, err := tgbotapi.NewBotAPI(cfg.GetTelegramBotToken())
if err != nil {
log.Panic(err)
}
bot.Debug = parseBoolOrFalse(os.Getenv(TELEGRAM_DEBUG))
bot.Debug = cfg.GetTelegramDebug()
u := tgbotapi.NewUpdate(0)
u.Timeout = 30
@ -71,7 +59,7 @@ the specified Mastodon account.`,
messageID := update.Message.MessageID
maxChars := cfg.GetMastodonMaxCharacters()
tootVisibility := cfg.GetMastodonVisibility()
tootFooter := os.Getenv(MASTODON_TOOT_FOOTER)
tootFooter := cfg.GetMastodonTootFooter()
if update.Message.Text != "" {
log.Printf("Text message received. Message id: %d\n", messageID)
@ -118,15 +106,6 @@ func init() {
rootCmd.AddCommand(runCmd)
}
func parseBoolOrFalse(s string) bool {
r, err := strconv.ParseBool(s)
if err != nil {
return false
}
return r
}
func downloadFile(url string) (io.ReadCloser, error) {
response, err := http.Get(url)
if err != nil {
@ -139,12 +118,3 @@ func downloadFile(url string) (io.ReadCloser, error) {
return response.Body, nil
}
func parseTelegramChatID(s string) int64 {
r, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return r
}

@ -1,17 +0,0 @@
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseBoolOrFalse(t *testing.T) {
assert.Equal(t, parseBoolOrFalse("True"), true)
assert.Equal(t, parseBoolOrFalse("TRUE"), true)
assert.Equal(t, parseBoolOrFalse("true"), true)
assert.Equal(t, parseBoolOrFalse(""), false)
assert.Equal(t, parseBoolOrFalse("False"), false)
assert.Equal(t, parseBoolOrFalse("FALSE"), false)
assert.Equal(t, parseBoolOrFalse("false"), false)
}
Loading…
Cancel
Save