1
0
Fork 0

Move all the configuration stuff into cfg package
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Daniele Tricoli 2022-03-13 03:09:51 +01:00
parent a1b10758cd
commit a4027c4271
4 changed files with 78 additions and 69 deletions

View File

@ -8,16 +8,42 @@ import (
) )
const ( const (
MASTODON_ACCESS_TOKEN = "MASTODON_ACCESS_TOKEN" mastodonAccessToken = "MASTODON_ACCESS_TOKEN"
MASTODON_SERVER_ADDRESS = "MASTODON_SERVER_ADDRESS" mastodonServerAddress = "MASTODON_SERVER_ADDRESS"
MASTODON_TOOT_FOOTER = "MASTODON_TOOT_FOOTER" mastodonTootFooter = "MASTODON_TOOT_FOOTER"
MASTODON_TOOT_MAX_CHARACTERS = "MASTODON_TOOT_MAX_CHARACTERS" mastodonTootMaxCharacters = "MASTODON_TOOT_MAX_CHARACTERS"
MASTODON_TOOT_VISIBILITY = "MASTODON_TOOT_VISIBILITY" mastodonTootVisibility = "MASTODON_TOOT_VISIBILITY"
TELEGRAM_BOT_TOKEN = "TELEGRAM_BOT_TOKEN" telegramBotToken = "TELEGRAM_BOT_TOKEN"
TELEGRAM_CHAT_ID = "TELEGRAM_CHAT_ID" telegramChatID = "TELEGRAM_CHAT_ID"
TELEGRAM_DEBUG = "TELEGRAM_DEBUG" 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 // Check the specified Mastodon visibility and return it if valid or return
// unlisted if it's not valid. // unlisted if it's not valid.
// The specified string will be cheched case unsensitive. // The specified string will be cheched case unsensitive.
@ -35,18 +61,38 @@ func parseMastodonVisibility(s string) string {
// Return configured Mastodon visibility for toot. // Return configured Mastodon visibility for toot.
func GetMastodonVisibility() string { 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 parseBoolOrFalse(s string) bool {
func parseMastodonMaxCharacters(s string) int { r, err := strconv.ParseBool(s)
if n, err := strconv.ParseUint(s, 10, 32); err == nil { if err != nil {
return int(n) return false
} }
return 500 return r
} }
func GetMastodonMaxCharacters() int { // Return if Telegram bot will run in debug mode or not.
return parseMastodonMaxCharacters(os.Getenv(MASTODON_TOOT_MAX_CHARACTERS)) 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))
} }

View File

@ -28,3 +28,13 @@ func TestParseMastodonMaxCharacters(t *testing.T) {
assert.Equal(t, parseMastodonMaxCharacters("-42"), 500) assert.Equal(t, parseMastodonMaxCharacters("-42"), 500)
assert.Equal(t, parseMastodonMaxCharacters("hello"), 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)
}

View File

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

View File

@ -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)
}