mirror of
https://github.com/sstent/go-garth-cli.git
synced 2026-01-26 09:01:58 +00:00
sync
This commit is contained in:
57
python-garmin-connect/connect/state.go
Normal file
57
python-garmin-connect/connect/state.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
connect "github.com/abrander/garmin-connect"
|
||||
)
|
||||
|
||||
var (
|
||||
client = connect.NewClient(
|
||||
connect.AutoRenewSession(true),
|
||||
)
|
||||
|
||||
stateFile string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringVarP(&stateFile, "state", "s", stateFilename(), "State file to use")
|
||||
}
|
||||
|
||||
func stateFilename() string {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not detect home directory: %s", err.Error())
|
||||
}
|
||||
|
||||
return path.Join(home, ".garmin-connect.json")
|
||||
}
|
||||
|
||||
func loadState() {
|
||||
data, err := ioutil.ReadFile(stateFile)
|
||||
if err != nil {
|
||||
log.Printf("Could not open state file: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, client)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not unmarshal state: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func storeState() {
|
||||
b, err := json.MarshalIndent(client, "", " ")
|
||||
if err != nil {
|
||||
log.Fatalf("Could not marshal state: %s", err.Error())
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(stateFile, b, 0600)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not write state file: %s", err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user