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

Only read extra heartbeats from stdin once #1073

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
35 changes: 20 additions & 15 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"

"github.com/wakatime/wakatime-cli/pkg/api"
Expand Down Expand Up @@ -398,10 +399,7 @@ func LoadHeartbeatParams(v *viper.Viper) (Heartbeat, error) {
var extraHeartbeats []heartbeat.Heartbeat

if v.GetBool("extra-heartbeats") {
extraHeartbeats, err = readExtraHeartbeats()
if err != nil {
log.Errorf("failed to read extra heartbeats: %s", err)
}
extraHeartbeats = readExtraHeartbeats()
}

var isWrite *bool
Expand Down Expand Up @@ -752,20 +750,27 @@ func readAPIKeyFromCommand(cmdStr string) (string, error) {
return strings.TrimSpace(string(out)), nil
}

func readExtraHeartbeats() ([]heartbeat.Heartbeat, error) {
in := bufio.NewReader(os.Stdin)
var extraHeartbeatsCache *[]heartbeat.Heartbeat // nolint:gochecknoglobals
var once sync.Once // nolint:gochecknoglobals

input, err := in.ReadString('\n')
if err != nil && err != io.EOF {
log.Debugf("failed to read data from stdin: %s", err)
}
func readExtraHeartbeats() []heartbeat.Heartbeat {
once.Do(func() {
in := bufio.NewReader(os.Stdin)

heartbeats, err := parseExtraHeartbeats(input)
if err != nil {
return nil, fmt.Errorf("failed parsing: %s", err)
}
input, err := in.ReadString('\n')
if err != nil && err != io.EOF {
log.Debugf("failed to read data from stdin: %s", err)
}

return heartbeats, nil
heartbeats, err := parseExtraHeartbeats(input)
if err != nil {
log.Errorf("failed parsing: %s", err)
}

extraHeartbeatsCache = &heartbeats
})

return *extraHeartbeatsCache
}

func parseExtraHeartbeats(data string) ([]heartbeat.Heartbeat, error) {
Expand Down
Loading