Skip to content

Commit

Permalink
feat(config): var to disable a service (#88)
Browse files Browse the repository at this point in the history
active: false
  • Loading branch information
JosephKav committed Jun 18, 2022
1 parent a24e194 commit 8fa72be
Show file tree
Hide file tree
Showing 26 changed files with 75 additions and 71 deletions.
6 changes: 3 additions & 3 deletions cmd/argus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
argus_testing.TestService(testServiceFlag, &config)

// config.Service.Init()
serviceCount := len(config.Service)
serviceCount := len(*config.Order)
if serviceCount == 0 {
jLog.Warn("No services to monitor were found.", utils.LogFrom{}, true)
os.Exit(0)
Expand All @@ -74,13 +74,13 @@ func main() {
msg := fmt.Sprintf("Found %d services to monitor:", serviceCount)
jLog.Info(msg, utils.LogFrom{}, true)

for _, key := range config.Order {
for _, key := range *config.Order {
fmt.Printf(" - %s\n", *config.Service[key].ID)
}
}

// Track all targets for changes in version and act on any found changes.
go (&config).Service.Track(&config.Order)
go (&config).Service.Track(config.Order)

// SaveHandler that listens for calls to save config changes.
go (&config).SaveHandler(configFile)
Expand Down
39 changes: 14 additions & 25 deletions config/ordering.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (c *Config) GetOrder(data []byte) {
}
c.Settings.Indentation = uint8(len(indentation))
}
} else {
afterService = false
} else if afterService {
break
}
}
if afterService && strings.HasPrefix(line, indentation) && !strings.HasPrefix(line, indentation+" ") {
Expand All @@ -60,31 +60,20 @@ func (c *Config) GetOrder(data []byte) {
}
}

if len(c.Order) != 0 {
// Add Services not in the existing Order.
for _, serviceID := range order {
if !utils.Contains(c.Order, serviceID) {
c.Order = append(c.Order, serviceID)
}
}
c.All = order
c.Order = &c.All

// Remove Services in the existing Order that have been removed.
if len(order) != len(c.Order) {
deleted := 0
services := len(c.Order)
for i := 0; i < services; i++ {
if !utils.Contains(order, c.Order[i-deleted]) {
if i == len(c.Order) {
c.Order = c.Order[:deleted]
} else {
c.Order = append(c.Order[:i-deleted], c.Order[i-deleted+1:]...)
}
deleted++
}
// Filter out Services that aren't Active
removed := 0
for index, id := range c.All {
if !utils.EvalNilPtr(c.Service[id].Active, true) {
if removed == 0 {
order = make([]string, len(order))
copy(order, c.All)
c.Order = &order
}

utils.RemoveIndex(c.Order, index-removed)
removed++
}
} else {
c.Order = order
}
}
6 changes: 3 additions & 3 deletions config/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ func (c *Config) Save() {

// Check if `i-1` should be before `i-2`
swap := false
for j := range c.Order {
for j := range c.All {
// Found i-1 (current item)
if c.Order[j] == currentOrder[i-1] {
if c.All[j] == currentOrder[i-1] {
swap = true
break
}
// Found i-2 (previous item)
if c.Order[j] == currentOrder[i-2] {
if c.All[j] == currentOrder[i-2] {
break
}
}
Expand Down
3 changes: 2 additions & 1 deletion config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type Config struct {
Notify shoutrrr.Slice `yaml:"notify,omitempty"` // Shoutrrr message(s) to send on a new release.
WebHook webhook.Slice `yaml:"webhook,omitempty"` // WebHook(s) to send on a new release.
Service service.Slice `yaml:"service,omitempty"` // The service(s) to monitor.
Order []string `yaml:"-"` // Ordering for the Service(s) in the WebUI.
All []string `yaml:"-"` // Ordered list of all Service(s).
Order *[]string `yaml:"-"` // Ordered list of all enabled Service(s).
SaveChannel *chan bool `yaml:"-"` // Channel for triggering a save of the config.
// TODO: Remove deprecated V
Gotify *conversions.GotifySlice `yaml:"gotify,omitempty"` // Gotify message(s) to send on a new release.
Expand Down
2 changes: 1 addition & 1 deletion config/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *Config) Print(flag *bool) {
return
}

c.Service.Print("", c.Order)
c.Service.Print("", *c.Order)
fmt.Println()
c.Notify.Print("")
fmt.Println()
Expand Down
8 changes: 4 additions & 4 deletions service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (s *Service) UpdatedVersion() {
if s.WebHook != nil {
for key := range *s.WebHook {
// Default nil to true = failed
if utils.EvalBoolPtr((*s.WebHook)[key].Failed, true) {
if utils.EvalNilPtr((*s.WebHook)[key].Failed, true) {
return
}
}
Expand All @@ -38,7 +38,7 @@ func (s *Service) UpdatedVersion() {
if s.Command != nil {
for key := range *s.Command {
// Default nil to true = failed
if utils.EvalBoolPtr(s.CommandController.Failed[key], true) {
if utils.EvalNilPtr(s.CommandController.Failed[key], true) {
return
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func (s *Service) HandleFailedActions() {
if s.WebHook != nil {
potentialErrors += len(*s.WebHook)
for key := range *s.WebHook {
if utils.EvalBoolPtr((*s.WebHook)[key].Failed, true) {
if utils.EvalNilPtr((*s.WebHook)[key].Failed, true) {
go func(key string) {
err := (*s.WebHook)[key].Send(s.GetServiceInfo(), false)
errs <- err
Expand All @@ -129,7 +129,7 @@ func (s *Service) HandleFailedActions() {
potentialErrors += len(*s.Command)
logFrom := utils.LogFrom{Primary: "Command", Secondary: *s.ID}
for key := range *s.Command {
if utils.EvalBoolPtr(s.CommandController.Failed[key], true) {
if utils.EvalNilPtr(s.CommandController.Failed[key], true) {
go func(key int) {
err := s.CommandController.ExecIndex(&logFrom, key)
errs <- err
Expand Down
5 changes: 5 additions & 0 deletions service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (s *Service) GetAccessToken() string {
return utils.DefaultIfNil(utils.GetFirstNonNilPtr(s.AccessToken, s.Defaults.AccessToken, s.HardDefaults.AccessToken))
}

// GetActive will return whether the Service is Active or not (default = true)
func (s *Service) GetActive() bool {
return utils.EvalNilPtr(s.Active, true)
}

// GetAllowInvalidCerts returns whether invalid HTTPS certs are allowed.
func (s *Service) GetAllowInvalidCerts() bool {
return *utils.GetFirstNonNilPtr(s.AllowInvalidCerts, s.Defaults.AllowInvalidCerts, s.HardDefaults.AllowInvalidCerts)
Expand Down
6 changes: 6 additions & 0 deletions service/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (
// Track will call Track on all Services in this Slice.
func (s *Slice) Track(ordering *[]string) {
for _, key := range *ordering {
// Skip disabled Services
if !(*s)[key].GetActive() {
continue
}
(*s)[key].Active = nil

jLog.Verbose(
fmt.Sprintf("Tracking %s at %s every %s", *(*s)[key].ID, (*s)[key].GetServiceURL(true), (*s)[key].GetInterval()),
utils.LogFrom{Primary: *(*s)[key].ID},
Expand Down
1 change: 1 addition & 0 deletions service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Slice map[string]*Service
// the latest version from the URL provided.
type Service struct {
ID *string `yaml:"-"` // service_name.
Active *bool `yaml:"active,omitempty"` // Disable the service
Type *string `yaml:"type,omitempty"` // "github"/"URL"
URL *string `yaml:"url,omitempty"` // type:URL - "https://example.com", type:github - "owner/repo" or "https://github.com/owner/repo".
AllowInvalidCerts *bool `yaml:"allow_invalid_certs,omitempty"` // default - false = Disallows invalid HTTPS certificates.
Expand Down
8 changes: 4 additions & 4 deletions service/urlcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ func (c *URLCommand) regex(text string, logFrom utils.LogFrom) (string, error) {

if len(texts) == 0 {
err := fmt.Errorf("%s (%s) didn't return any matches", c.Type, *c.Regex)
jLog.Warn(err, logFrom, !utils.EvalBoolPtr(c.GetIgnoreMisses(), false))
jLog.Warn(err, logFrom, !utils.EvalNilPtr(c.GetIgnoreMisses(), false))

return text, err
}

if (len(texts) - index) < 1 {
err := fmt.Errorf("%s (%s) returned %d elements but the index wants element number %d", c.Type, *c.Regex, len(texts), (index + 1))
jLog.Warn(err, logFrom, !utils.EvalBoolPtr(c.GetIgnoreMisses(), false))
jLog.Warn(err, logFrom, !utils.EvalNilPtr(c.GetIgnoreMisses(), false))

return text, err
}
Expand All @@ -180,7 +180,7 @@ func (c *URLCommand) split(text string, logFrom utils.LogFrom) (string, error) {

if len(texts) == 1 {
err := fmt.Errorf("%s didn't find any %q to split on", c.Type, *c.Text)
jLog.Warn(err, logFrom, !utils.EvalBoolPtr(c.GetIgnoreMisses(), false))
jLog.Warn(err, logFrom, !utils.EvalNilPtr(c.GetIgnoreMisses(), false))

return text, err
}
Expand All @@ -193,7 +193,7 @@ func (c *URLCommand) split(text string, logFrom utils.LogFrom) (string, error) {

if (len(texts) - index) < 1 {
err := fmt.Errorf("%s (%s) returned %d elements but the index wants element number %d", c.Type, *c.Text, len(texts), (index + 1))
jLog.Warn(err, logFrom, !utils.EvalBoolPtr(c.GetIgnoreMisses(), false))
jLog.Warn(err, logFrom, !utils.EvalNilPtr(c.GetIgnoreMisses(), false))

return text, err
}
Expand Down
8 changes: 4 additions & 4 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func Contains[T comparable](s []T, e T) bool {
return false
}

// EvalBoolPtr.
func EvalBoolPtr(boolean *bool, nilValue bool) bool {
if boolean == nil {
// EvalNilPtr - Return the value of pointer if it's non-nil, otherwise nilValue.
func EvalNilPtr[T comparable](pointer *T, nilValue T) T {
if pointer == nil {
return nilValue
}
return *boolean
return *pointer
}

// PtrOrValueToPtr will take the pointer `a` and the value `b`, returning
Expand Down
2 changes: 2 additions & 0 deletions web/api/types/argus.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

// ServiceSummary is the Summary of a Service.
type ServiceSummary struct {
Active *bool `json:"active,omitempty"` // Active Service?
ID *string `json:"id"`
Type *string `json:"type,omitempty"` // "github"/"URL"
URL *string `json:"url,omitempty"` // type:URL - "https://example.com", type:github - "owner/repo" or "https://github.com/owner/repo".
Expand Down Expand Up @@ -240,6 +241,7 @@ type ServiceSlice map[string]*Service
// Service is a source to be serviceed and provides everything needed to extract
// the latest version from the URL provided.
type Service struct {
Active *bool `json:"active,omitempty"` // Active Service?
Type *string `json:"type,omitempty"` // "github"/"URL"
URL *string `json:"url,omitempty"` // type:URL - "https://example.com", type:github - "owner/repo" or "https://github.com/owner/repo".
WebURL *string `json:"web_url,omitempty"` // URL to provide on the Web UI
Expand Down
12 changes: 7 additions & 5 deletions web/api/v1/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (api *API) wsService(client *Client) {
Page: &responsePage,
Type: &responseType,
SubType: &responseSubType,
Order: &api.Config.Order,
Order: api.Config.Order,
}
if err := client.conn.WriteJSON(msg); err != nil {
api.Log.Error(err, logFrom, true)
Expand All @@ -46,7 +46,7 @@ func (api *API) wsService(client *Client) {

// Initialise the services
responseSubType = "INIT"
for _, key := range api.Config.Order {
for _, key := range *api.Config.Order {
// Check Service still exists in this ordering
if api.Config.Service[key] == nil {
continue
Expand All @@ -65,10 +65,11 @@ func (api *API) wsService(client *Client) {
hasDeployedVersionLookup := service.DeployedVersionLookup != nil

serviceSummary := api_types.ServiceSummary{
Active: service.Active,
ID: service.ID,
Type: service.Type,
URL: &url,
Icon: (*service).GetIconURL(),
Icon: service.GetIconURL(),
HasDeployedVersionLookup: &hasDeployedVersionLookup,
Command: commandCount,
WebHook: webhookCount,
Expand Down Expand Up @@ -506,10 +507,11 @@ func (api *API) wsConfigService(client *Client) {

serviceConfig := make(api_types.ServiceSlice)
if api.Config.Service != nil {
for _, key := range api.Config.Order {
for _, key := range api.Config.All {
service := api.Config.Service[key]

serviceConfig[key] = &api_types.Service{
Active: service.Active,
Type: service.Type,
URL: service.URL,
WebURL: service.WebURL,
Expand Down Expand Up @@ -636,7 +638,7 @@ func (api *API) wsConfigService(client *Client) {
SubType: &responseSubType,
ConfigData: &api_types.Config{
Service: &serviceConfig,
Order: api.Config.Order,
Order: api.Config.All,
},
}
if err := client.conn.WriteJSON(msg); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion web/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/ui/react-app/src/components/approvals/service-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export const ServiceInfo = ({
>
{service?.status?.last_queried ? (
<>
Queried{" "}
queried{" "}
{formatRelative(
new Date(service.status.last_queried),
new Date()
Expand Down
6 changes: 1 addition & 5 deletions web/ui/react-app/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,6 @@ body {
.same-color-as-background {
color: var(--background);
}
.same-background-color {
color: var(--dark-text);
background-color: var(--background);
}

.card-footer {
border-top: none;
Expand All @@ -340,7 +336,7 @@ body {
}

.list-group-item-secondary {
background-color: var(--background);
background-color: transparent;
color: var(--dark-text);
}
.list-group-item-warning {
Expand Down
1 change: 1 addition & 0 deletions web/ui/react-app/src/types/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface ServiceListType {
}

export interface ServiceType {
active?: boolean;
type: string;
url?: string;
allow_invalid_certs?: boolean;
Expand Down
1 change: 1 addition & 0 deletions web/ui/react-app/src/types/summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ServiceSummaryListType {
}

export interface ServiceSummaryType {
active?: boolean;
id: string;
loading: boolean;
type?: string;
Expand Down
12 changes: 6 additions & 6 deletions web/ui/static/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"files": {
"main.css": "./static/css/main.93d93d0c.css",
"main.js": "./static/js/main.7137ee59.js",
"main.css": "./static/css/main.f180f0e2.css",
"main.js": "./static/js/main.cdabf607.js",
"index.html": "./index.html",
"main.93d93d0c.css.map": "./static/css/main.93d93d0c.css.map",
"main.7137ee59.js.map": "./static/js/main.7137ee59.js.map"
"main.f180f0e2.css.map": "./static/css/main.f180f0e2.css.map",
"main.cdabf607.js.map": "./static/js/main.cdabf607.js.map"
},
"entrypoints": [
"static/css/main.93d93d0c.css",
"static/js/main.7137ee59.js"
"static/css/main.f180f0e2.css",
"static/js/main.cdabf607.js"
]
}
2 changes: 1 addition & 1 deletion web/ui/static/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.svg" type="image/svg+xml"/><link rel="apple-touch-icon" href="./apple-touch-icon.png"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="Argus" content="Monitor new releases"/><link rel="apple-touch-icon" href="./favicon.png"/><link rel="manifest" href="./manifest.json"/><title>Argus</title><script defer="defer" src="./static/js/main.7137ee59.js"></script><link href="./static/css/main.93d93d0c.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="./favicon.svg" type="image/svg+xml"/><link rel="apple-touch-icon" href="./apple-touch-icon.png"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="Argus" content="Monitor new releases"/><link rel="apple-touch-icon" href="./favicon.png"/><link rel="manifest" href="./manifest.json"/><title>Argus</title><script defer="defer" src="./static/js/main.cdabf607.js"></script><link href="./static/css/main.f180f0e2.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
1 change: 0 additions & 1 deletion web/ui/static/static/css/main.93d93d0c.css.map

This file was deleted.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions web/ui/static/static/css/main.f180f0e2.css.map

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 8fa72be

Please sign in to comment.