MCPcopy
hub / github.com/cli/cli / getOrCreateDeviceID

Function getOrCreateDeviceID

internal/telemetry/telemetry.go:38–88  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

36var deviceIDFunc = getOrCreateDeviceID
37
38func getOrCreateDeviceID() (string, error) {
39 stateDir := stateDirFunc()
40 idPath := filepath.Join(stateDir, deviceIDFileName)
41
42 data, err := os.ReadFile(idPath)
43 if err == nil {
44 return strings.TrimSpace(string(data)), nil
45 }
46 if !errors.Is(err, os.ErrNotExist) {
47 return "", err
48 }
49
50 id := uuid.New().String()
51 if err := os.MkdirAll(stateDir, 0o755); err != nil {
52 return "", err
53 }
54
55 // Write the ID to a temp file in the same directory, then hard-link it
56 // to the target path. os.Link fails atomically if the target already
57 // exists, so exactly one concurrent caller wins. Losers read the
58 // winner's ID. The temp file is always cleaned up.
59 tmpFile, err := os.CreateTemp(stateDir, deviceIDFileName+".tmp.*")
60 if err != nil {
61 return "", err
62 }
63 tmpPath := tmpFile.Name()
64
65 if _, err := tmpFile.WriteString(id); err != nil {
66 tmpFile.Close()
67 os.Remove(tmpPath)
68 return "", err
69 }
70 if err := tmpFile.Close(); err != nil {
71 os.Remove(tmpPath)
72 return "", err
73 }
74
75 linkErr := os.Link(tmpPath, idPath)
76 os.Remove(tmpPath)
77
78 if linkErr != nil {
79 // Another caller won — read their ID.
80 data, readErr := os.ReadFile(idPath)
81 if readErr != nil {
82 return "", linkErr
83 }
84 return strings.TrimSpace(string(data)), nil
85 }
86
87 return id, nil
88}
89
90var falseyValues = []string{"", "0", "false", "no", "disabled", "off"}
91

Callers 1

TestGetOrCreateDeviceIDFunction · 0.85

Calls 6

JoinMethod · 0.80
NameMethod · 0.65
CloseMethod · 0.65
RemoveMethod · 0.65
LinkMethod · 0.65
StringMethod · 0.45

Tested by 1

TestGetOrCreateDeviceIDFunction · 0.68