mukan-ignite/ignite/internal/announcements/announcement.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

57 lines
1.2 KiB
Go

package announcements
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/cliui/icons"
)
var (
SurveyLink = "https://bit.ly/3WZS2uS"
AnnouncementURL = "https://api.ignite.com/v1/announcements"
)
type announcement struct {
ID string `json:"id"`
Text string `json:"text"`
Timestamp time.Time `json:"timestamp"`
User string `json:"user"`
}
// Fetch fetches the latest announcements from the API.
func Fetch() string {
resp, err := http.Get(AnnouncementURL) //nolint:gosec
if err != nil || resp.StatusCode != 200 {
return fallbackData()
}
defer resp.Body.Close()
type response struct {
Announcements []announcement `json:"announcements"`
}
var data response
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return fallbackData()
}
if len(data.Announcements) == 0 {
return fallbackData()
}
var out strings.Builder
fmt.Fprintf(&out, "%s\n\n", "Announcements:")
for _, msg := range data.Announcements {
fmt.Fprintf(&out, "%s %s\n", icons.Bullet, msg.Text)
}
return out.String()
}
func fallbackData() string {
return fmt.Sprintf("\n%s Survey: %s\n", icons.Survey, SurveyLink)
}