From d1d895e57b686446182cddd8222f279858723579 Mon Sep 17 00:00:00 2001 From: Tony Worm Date: Fri, 8 May 2020 18:48:10 -0600 Subject: [PATCH] bug fix --- .hof/Cli/cmd/mvs/ga/ga.go | 72 +++++++++++++++++++++++++++++++++++++-- cli.cue | 1 + cmd/mvs/ga/ga.go | 72 +++++++++++++++++++++++++++++++++++++-- cue.mods | 2 +- cue.sums | 2 ++ go.mod | 1 + go.sum | 2 ++ 7 files changed, 147 insertions(+), 5 deletions(-) diff --git a/.hof/Cli/cmd/mvs/ga/ga.go b/.hof/Cli/cmd/mvs/ga/ga.go index 1936f80..d817452 100644 --- a/.hof/Cli/cmd/mvs/ga/ga.go +++ b/.hof/Cli/cmd/mvs/ga/ga.go @@ -1,6 +1,12 @@ package ga import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + + "github.com/google/uuid" "github.com/hofstadter-io/yagu" "github.com/hofstadter-io/mvs/cmd/mvs/verinfo" @@ -8,14 +14,22 @@ import ( func SendGaEvent(action, label string, value int) { // Do something here to lookup / create - cid := "13b3ad64-9154-11ea-9eba-47f617ab74f7" + // cid := "13b3ad64-9154-11ea-9eba-47f617ab74f7" + cid, err := readGaId() + if err != nil { + fmt.Println("Error", err) + cid = "unknown" + } + + fmt.Println("CID:", cid) cfg := yagu.GaConfig{ TID: "UA-103579574-5", CID: cid, UA: "mvs/" + verinfo.Version, + CN: "mvs", CS: "mvs", - CN: verinfo.Version, + CM: verinfo.Version, } evt := yagu.GaEvent{ @@ -31,3 +45,57 @@ func SendGaEvent(action, label string, value int) { yagu.SendGaEvent(cfg, evt) } + +func readGaId() (string, error) { + ucd := yagu.UserHomeDir() + dir := filepath.Join(ucd, ".hof") + fn := filepath.Join(dir, ".uuid") + + _, err := os.Lstat(fn) + if err != nil { + // make sure we check err for something actually bad + if _, ok := err.(*os.PathError); !ok && err.Error() != "file does not exist" { + // return "", err + } + // file does not exist + return writeGaId() + } + + content, err := ioutil.ReadFile(fn) + if err != nil { + fmt.Println("ERROR:", err) + return writeGaId() + // return "", err + } + + return string(content), nil +} + +func writeGaId() (string, error) { + fmt.Println("writeGaId") + ucd := yagu.UserHomeDir() + + dir := filepath.Join(ucd, ".hof") + err := yagu.Mkdir(dir) + if err != nil { + return "", err + } + + fmt.Println("Mkdir:", dir) + + fn := filepath.Join(dir, ".uuid") + + id, err := uuid.NewUUID() + if err != nil { + return id.String(), err + } + + fmt.Println("writeGaId: ", id.String()) + + err = ioutil.WriteFile(fn, []byte(id.String()), 0644) + if err != nil { + return id.String(), err + } + + return id.String(), nil +} diff --git a/cli.cue b/cli.cue index 4c73c47..17d5f90 100644 --- a/cli.cue +++ b/cli.cue @@ -58,6 +58,7 @@ HofGenCli: gen.#HofGenerator & { } Telemetry: "UA-103579574-5" + TelemetryIdDir: ".hof" OmitRun: true diff --git a/cmd/mvs/ga/ga.go b/cmd/mvs/ga/ga.go index 1936f80..d817452 100644 --- a/cmd/mvs/ga/ga.go +++ b/cmd/mvs/ga/ga.go @@ -1,6 +1,12 @@ package ga import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + + "github.com/google/uuid" "github.com/hofstadter-io/yagu" "github.com/hofstadter-io/mvs/cmd/mvs/verinfo" @@ -8,14 +14,22 @@ import ( func SendGaEvent(action, label string, value int) { // Do something here to lookup / create - cid := "13b3ad64-9154-11ea-9eba-47f617ab74f7" + // cid := "13b3ad64-9154-11ea-9eba-47f617ab74f7" + cid, err := readGaId() + if err != nil { + fmt.Println("Error", err) + cid = "unknown" + } + + fmt.Println("CID:", cid) cfg := yagu.GaConfig{ TID: "UA-103579574-5", CID: cid, UA: "mvs/" + verinfo.Version, + CN: "mvs", CS: "mvs", - CN: verinfo.Version, + CM: verinfo.Version, } evt := yagu.GaEvent{ @@ -31,3 +45,57 @@ func SendGaEvent(action, label string, value int) { yagu.SendGaEvent(cfg, evt) } + +func readGaId() (string, error) { + ucd := yagu.UserHomeDir() + dir := filepath.Join(ucd, ".hof") + fn := filepath.Join(dir, ".uuid") + + _, err := os.Lstat(fn) + if err != nil { + // make sure we check err for something actually bad + if _, ok := err.(*os.PathError); !ok && err.Error() != "file does not exist" { + // return "", err + } + // file does not exist + return writeGaId() + } + + content, err := ioutil.ReadFile(fn) + if err != nil { + fmt.Println("ERROR:", err) + return writeGaId() + // return "", err + } + + return string(content), nil +} + +func writeGaId() (string, error) { + fmt.Println("writeGaId") + ucd := yagu.UserHomeDir() + + dir := filepath.Join(ucd, ".hof") + err := yagu.Mkdir(dir) + if err != nil { + return "", err + } + + fmt.Println("Mkdir:", dir) + + fn := filepath.Join(dir, ".uuid") + + id, err := uuid.NewUUID() + if err != nil { + return id.String(), err + } + + fmt.Println("writeGaId: ", id.String()) + + err = ioutil.WriteFile(fn, []byte(id.String()), 0644) + if err != nil { + return id.String(), err + } + + return id.String(), nil +} diff --git a/cue.mods b/cue.mods index 0674cbb..0cb9fa9 100644 --- a/cue.mods +++ b/cue.mods @@ -3,5 +3,5 @@ module github.com/hofstadter-io/mvs cue master require ( - github.com/hofstadter-io/hofmod-cli v0.5.0 + github.com/hofstadter-io/hofmod-cli v0.5.1 ) diff --git a/cue.sums b/cue.sums index c6b17bf..d6ec124 100644 --- a/cue.sums +++ b/cue.sums @@ -24,3 +24,5 @@ github.com/hofstadter-io/hofmod-cli v0.4.0 h1:M9qmHyUW9AxmOAgeAwbfa24L3+GBAuiBhb github.com/hofstadter-io/hofmod-cli v0.4.0/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU= github.com/hofstadter-io/hofmod-cli v0.5.0 h1:hZT2BUWJftP9jq32bcOElXg1N9oXEFJCK9MxBmfS+24= github.com/hofstadter-io/hofmod-cli v0.5.0/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU= +github.com/hofstadter-io/hofmod-cli v0.5.1 h1:a8Zworm73OyAUxtVPEzibspP5kImka/3D+CwHQ9cz+E= +github.com/hofstadter-io/hofmod-cli v0.5.1/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU= diff --git a/go.mod b/go.mod index a4d281c..0cf3851 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-git/go-billy/v5 v5.0.0 github.com/go-git/go-git/v5 v5.0.0 github.com/google/go-github/v30 v30.1.0 + github.com/google/uuid v1.1.1 github.com/hofstadter-io/yagu v0.0.1 github.com/parnurzeal/gorequest v0.2.16 github.com/spf13/cobra v1.0.0 diff --git a/go.sum b/go.sum index 1375cd8..9640977 100644 --- a/go.sum +++ b/go.sum @@ -101,6 +101,8 @@ github.com/google/go-github/v30 v30.1.0 h1:VLDx+UolQICEOKu2m4uAoMti1SxuEBAl7RSEG github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQFEufcolZ95JfU8= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=