@@ -11,7 +11,6 @@ import (
1111 "github.com/ActiveState/cli/internal/config"
1212 "github.com/ActiveState/cli/internal/constants"
1313 "github.com/ActiveState/cli/internal/errs"
14- "github.com/ActiveState/cli/internal/fileutils"
1514 "github.com/ActiveState/cli/internal/graph"
1615 "github.com/ActiveState/cli/internal/httputil"
1716 "github.com/ActiveState/cli/internal/logging"
@@ -21,8 +20,14 @@ import (
2120 auth "github.com/ActiveState/cli/pkg/platform/authentication"
2221 "github.com/ActiveState/cli/pkg/sysinfo"
2322 "github.com/blang/semver"
23+
24+ configMediator "github.com/ActiveState/cli/internal/mediators/config"
2425)
2526
27+ func init () {
28+ configMediator .RegisterOption (constants .NotificationsURLConfig , configMediator .String , "" )
29+ }
30+
2631const ConfigKeyLastReport = "notifications.last_reported"
2732
2833type Notifications struct {
@@ -48,11 +53,11 @@ func New(cfg *config.Instance, auth *auth.Auth) (*Notifications, error) {
4853 defer func () {
4954 panics .LogAndPanic (recover (), debug .Stack ())
5055 }()
51- resp , err := fetch ()
56+ resp , err := fetch (cfg )
5257 return resp , err
5358 })
5459
55- return & Notifications {
60+ notifications := & Notifications {
5661 baseParams : & ConditionParams {
5762 OS : sysinfo .OS ().String (),
5863 OSVersion : NewVersionFromSysinfo (osVersion ),
@@ -62,7 +67,20 @@ func New(cfg *config.Instance, auth *auth.Auth) (*Notifications, error) {
6267 cfg : cfg ,
6368 auth : auth ,
6469 poll : poll ,
65- }, nil
70+ }
71+
72+ configMediator .AddListener (constants .NotificationsURLConfig , func () {
73+ notifications .poll .Close ()
74+ notifications .poll = poller .New (10 * time .Minute , func () (interface {}, error ) {
75+ defer func () {
76+ panics .LogAndPanic (recover (), debug .Stack ())
77+ }()
78+ resp , err := fetch (cfg )
79+ return resp , err
80+ })
81+ })
82+
83+ return notifications , nil
6684}
6785
6886func (m * Notifications ) Close () error {
@@ -168,8 +186,7 @@ func check(params *ConditionParams, notifications []*graph.NotificationInfo, las
168186 // Check if message is within date range
169187 inRange , err := notificationInDateRange (notification , baseTime )
170188 if err != nil {
171- logging .Warning ("Could not check if notification %s is in date range: %v" , notification .ID , err )
172- continue
189+ return nil , errs .Wrap (err , "Could not check if notification %s is in date range" , notification .ID )
173190 }
174191 if ! inRange {
175192 logging .Debug ("Skipping notification %s as it is outside of its date range" , notification .ID )
@@ -198,17 +215,36 @@ func check(params *ConditionParams, notifications []*graph.NotificationInfo, las
198215 return filteredNotifications , nil
199216}
200217
201- func fetch () ([]* graph.NotificationInfo , error ) {
218+ func fetch (cfg * config. Instance ) ([]* graph.NotificationInfo , error ) {
202219 var body []byte
203220 var err error
204221
205- if v := os .Getenv (constants .NotificationsOverrideEnvVarName ); v != "" {
206- body , err = fileutils .ReadFile (v )
222+ var (
223+ notificationsURL string
224+
225+ envURL = os .Getenv (constants .NotificationsOverrideEnvVarName )
226+ configURL = cfg .GetString (constants .NotificationsURLConfig )
227+ )
228+
229+ switch {
230+ case envURL != "" :
231+ notificationsURL = envURL
232+ case configURL != "" :
233+ notificationsURL = configURL
234+ default :
235+ notificationsURL = constants .NotificationsInfoURL
236+ }
237+
238+ logging .Debug ("Fetching notifications from %s" , notificationsURL )
239+ // Check if this is a local file path (when using environment override)
240+ if envURL != "" {
241+ body , err = os .ReadFile (notificationsURL )
207242 if err != nil {
208- return nil , errs .Wrap (err , "Could not read notifications override file" )
243+ return nil , errs .Wrap (err , "Could not read notifications file" )
209244 }
210245 } else {
211- body , err = httputil .Get (constants .NotificationsInfoURL )
246+ // Use HTTP client for remote URLs
247+ body , err = httputil .Get (notificationsURL )
212248 if err != nil {
213249 return nil , errs .Wrap (err , "Could not fetch notifications information" )
214250 }
0 commit comments