Skip to content

Commit

Permalink
Monitors in Separated Files
Browse files Browse the repository at this point in the history
  • Loading branch information
RaJiska committed Dec 30, 2020
1 parent d57d71f commit 3e765b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
20 changes: 11 additions & 9 deletions commands/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"

"github.com/kennygrant/sanitize"
"github.com/mihirsoni/odfe-monitor-cli/destination"
"github.com/mihirsoni/odfe-monitor-cli/monitor"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -72,15 +73,16 @@ func writeDestinations(destinations map[string]destination.Destination) {
}

func writeMonitors(monitors map[string]monitor.Monitor) {
destinationsPath := filepath.Join(rootDir, "monitors.yaml")
file, err := os.OpenFile(destinationsPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
check(err)
defer file.Close()
var monitorsList []monitor.Monitor
monitorsPath := filepath.Join(rootDir, "monitors/")
if _, err := os.Stat(monitorsPath); os.IsNotExist(err) {
os.Mkdir(monitorsPath, 0755)
}
for name := range monitors {
monitorsList = append(monitorsList, monitors[name])
monitorFile := filepath.Join(monitorsPath, sanitize.BaseName(name)+".yaml")
file, err := os.OpenFile(monitorFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
check(err)
data, err := yaml.Marshal(monitors[name])
check(err)
file.Write(data)
}
data, err := yaml.Marshal(monitorsList)
check(err)
file.Write(data)
}
14 changes: 6 additions & 8 deletions monitor/localOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,20 @@ func GetAllLocal(rootDir string) (map[string]Monitor, mapset.Set, error) {
monitorsSet := mapset.NewSet()
monitorsMap := make(map[string]Monitor)
for _, file := range files {
var allLocalMonitors []Monitor
var monitor Monitor
yamlFile, err := ioutil.ReadFile(file)
if err != nil {
return nil, nil, errors.Wrap(err, "Unable to read "+file)
}
err = yaml.Unmarshal(yamlFile, &allLocalMonitors)
err = yaml.Unmarshal(yamlFile, &monitor)
if err != nil {
return nil, nil, errors.Wrap(err, "Unable to parse "+file)
}
for _, localMonitor := range allLocalMonitors {
if monitorsSet.Contains(localMonitor.Name) {
return nil, nil, errors.New("Duplicate monitor found. " + localMonitor.Name + " already exists")
}
monitorsSet.Add(localMonitor.Name)
monitorsMap[localMonitor.Name] = localMonitor
if monitorsSet.Contains(monitor.Name) {
return nil, nil, errors.New("Duplicate monitor found. " + monitor.Name + " already exists")
}
monitorsSet.Add(monitor.Name)
monitorsMap[monitor.Name] = monitor
}
return monitorsMap, monitorsSet, nil
}

0 comments on commit 3e765b0

Please sign in to comment.