Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomically write the json file #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
package main

import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"hash/fnv"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"sync"
Expand All @@ -30,6 +30,7 @@ import (
"github.com/prometheus/common/model"

"github.com/hashicorp/mdns"
"github.com/natefinch/atomic"
)

type TargetGroup struct {
Expand Down Expand Up @@ -102,12 +103,10 @@ func main() {
if *output == "-" {
fmt.Println(string(y))
} else {
file, err := os.Create(*output) // For read access.
if err != nil {
buf := bytes.NewBuffer(y)
if err := atomic.WriteFile(*output, buf); err != nil {
log.Fatal(err)
}
file.Write(y)
file.Close()
}
}
}()
Expand Down