commit bb3687e36df819100055904a2fc9e462d6756946
parent 2c800fa98e17e5f792f7620d7bd4a404a56e61c0
Author: Natasha Kerensikova <natacha@instinctive.eu>
Date: Sun, 26 May 2024 10:25:38 +0200
feat: dry-run option
Diffstat:
M | main.go | | | 26 | ++++++++++++++++++-------- |
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/main.go b/main.go
@@ -27,6 +27,15 @@ type News struct {
var newsList []News
+// Send an IRC ircMessage, unless in dry-run mode
+func ircMessage(client *girc.Client, target, message string) {
+ if viper.GetBool("irc.dry") {
+ log.Printf("ircMessage to %s: %s", target, message)
+ } else {
+ client.Cmd.Message(target, message)
+ }
+}
+
// Create a hash out of link
func mkHash(s string) string {
h := sha256.Sum256([]byte(s))
@@ -142,7 +151,7 @@ func newsFetch(client *girc.Client, channel string) {
break
}
- client.Cmd.Message(channel, fmtNews(news))
+ ircMessage(client, channel, fmtNews(news))
time.Sleep(viper.GetDuration("irc.delay"))
// Mark item as posted
@@ -162,7 +171,7 @@ func newsFetch(client *girc.Client, channel string) {
}
encoder := json.NewEncoder(f)
if err = encoder.Encode(newsList); err != nil {
- client.Cmd.Message(channel, "could not write newsList")
+ ircMessage(client, channel, "could not write newsList")
}
time.Sleep(viper.GetDuration("feeds.frequency"))
}
@@ -175,6 +184,7 @@ func confDefault() {
"irc.channel": "goaste",
"irc.xchannels": []string{"goaste2"},
"irc.debug": false,
+ "irc.dry": false,
"irc.port": 6667,
"irc.delay": "2s",
"irc.colors.origin": "pink",
@@ -273,7 +283,7 @@ func main() {
if strings.HasPrefix(e.Last(), "!lsfeeds") {
for i, f := range viper.GetStringSlice("feeds.urls") {
n := strconv.Itoa(i + 1)
- c.Cmd.Message(dest, n+". "+f)
+ ircMessage(c, dest, n+". "+f)
time.Sleep(viper.GetDuration("irc.delay"))
}
}
@@ -286,7 +296,7 @@ func main() {
post := fmt.Sprintf(" {r}(from %s on %s)", e.Source.Name, channel)
message := fmtNews(news) + girc.Fmt(post)
for _, xchan := range viper.GetStringSlice("irc.xchannels") {
- c.Cmd.Message(xchan, message)
+ ircMessage(c, xchan, message)
time.Sleep(viper.GetDuration("irc.delay"))
}
}
@@ -294,7 +304,7 @@ func main() {
if strings.HasPrefix(e.Last(), "!latest") && e.Params[0] != channel {
args := strings.SplitN(e.Last(), " ", 3)
if len(args) < 2 {
- c.Cmd.Message(dest, "usage: !latest <number> [origin]")
+ ircMessage(c, dest, "usage: !latest <number> [origin]")
time.Sleep(viper.GetDuration("irc.delay"))
return
}
@@ -302,7 +312,7 @@ func main() {
// n == number of news to show
n, err := strconv.Atoi(args[1])
if err != nil || n <= 0 {
- c.Cmd.Message(dest, "conversion error")
+ ircMessage(c, dest, "conversion error")
time.Sleep(viper.GetDuration("irc.delay"))
return
}
@@ -319,7 +329,7 @@ func main() {
// check if some news are available
if numNews < 1 {
- c.Cmd.Message(dest, "no news available")
+ ircMessage(c, dest, "no news available")
time.Sleep(viper.GetDuration("irc.delay"))
return
}
@@ -331,7 +341,7 @@ func main() {
numNews--
for i := 0; i < n; i++ {
fmt.Println(i)
- c.Cmd.Message(dest, fmtNews(showNews[numNews-i]))
+ ircMessage(c, dest, fmtNews(showNews[numNews-i]))
time.Sleep(viper.GetDuration("irc.delay"))
}
}