|
15 | 15 | package internal |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "encoding/json" |
19 | 18 | "fmt" |
20 | | - "io/ioutil" |
21 | | - "os" |
22 | | - "os/user" |
23 | | - "path/filepath" |
24 | | - "runtime" |
25 | 19 | "strings" |
26 | 20 | "time" |
27 | 21 |
|
28 | 22 | "github.com/golang/protobuf/ptypes/timestamp" |
29 | 23 | ) |
30 | 24 |
|
31 | | -// Service contains metadata about the exporter service. |
32 | | -type Service struct { |
33 | | - Endpoint string `json:"endpoint"` |
34 | | -} |
35 | | - |
36 | | -// WriteToEndpointFile writes service metadata to |
37 | | -// canonical endpoint file. |
38 | | -func (s *Service) WriteToEndpointFile() (path string, err error) { |
39 | | - data, err := json.Marshal(s) |
40 | | - if err != nil { |
41 | | - return "", err |
42 | | - } |
43 | | - path = defaultEndpointFile() |
44 | | - if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { |
45 | | - return "", err |
46 | | - } |
47 | | - if err := ioutil.WriteFile(path, data, 0755); err != nil { |
48 | | - return "", err |
49 | | - } |
50 | | - return path, nil |
51 | | -} |
52 | | - |
53 | | -// ParseEndpointFile reads and parses the canonical endpoint |
54 | | -// file for metadata. |
55 | | -func ParseEndpointFile() (*Service, error) { |
56 | | - file := defaultEndpointFile() |
57 | | - data, err := ioutil.ReadFile(file) |
58 | | - if err != nil { |
59 | | - return nil, err |
60 | | - } |
61 | | - var s Service |
62 | | - if err := json.Unmarshal(data, &s); err != nil { |
63 | | - return nil, err |
64 | | - } |
65 | | - return &s, nil |
66 | | -} |
67 | | - |
68 | | -// defaultEndpointFile is the location where the |
69 | | -// endpoint file is at on the current platform. |
70 | | -func defaultEndpointFile() string { |
71 | | - const f = "opencensus.endpoint" |
72 | | - if runtime.GOOS == "windows" { |
73 | | - return filepath.Join(os.Getenv("APPDATA"), "opencensus", f) |
74 | | - } |
75 | | - return filepath.Join(guessUnixHomeDir(), ".config", f) |
76 | | -} |
77 | | - |
78 | | -func guessUnixHomeDir() string { |
79 | | - // Prefer $HOME over user.Current due to glibc bug: golang.org/issue/13470 |
80 | | - if v := os.Getenv("HOME"); v != "" { |
81 | | - return v |
82 | | - } |
83 | | - // Else, fall back to user.Current: |
84 | | - if u, err := user.Current(); err == nil { |
85 | | - return u.HomeDir |
86 | | - } |
87 | | - return "" |
88 | | -} |
89 | | - |
90 | 25 | // TimeToTimestamp converts a time.Time to a timestamp.Timestamp pointer. |
91 | 26 | func TimeToTimestamp(t time.Time) *timestamp.Timestamp { |
92 | 27 | if t.IsZero() { |
|
0 commit comments