-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16345fd
commit 1ecfa71
Showing
9 changed files
with
360 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/api" | ||
"github.com/CircleCI-Public/circleci-cli/api/rest" | ||
"github.com/CircleCI-Public/circleci-cli/prompt" | ||
"github.com/CircleCI-Public/circleci-cli/settings" | ||
"github.com/CircleCI-Public/circleci-cli/telemetry" | ||
"github.com/google/uuid" | ||
"github.com/pkg/errors" | ||
"golang.org/x/term" | ||
) | ||
|
||
const ( | ||
TelemetryIsActive = "yes" | ||
TelemetryIsInactive = "no" | ||
|
||
// This value means telemetry is disabled because we have no access to stdin and so can't ask user approval | ||
TelemetryDefaultDisabled = "default-disabled" | ||
) | ||
|
||
type telemetryUI interface { | ||
AskUserToApproveTelemetry(message string) bool | ||
} | ||
|
||
type telemetryInteractiveUI struct{} | ||
|
||
func (telemetryInteractiveUI) AskUserToApproveTelemetry(message string) bool { | ||
return prompt.AskUserToConfirmWithDefault(message, true) | ||
} | ||
|
||
type telemetryTestUI struct { | ||
Approved bool | ||
} | ||
|
||
func (ui telemetryTestUI) AskUserToApproveTelemetry(message string) bool { | ||
return ui.Approved | ||
} | ||
|
||
// Make sure the user gave their approval for the telemetry and | ||
func checkTelemetry(config *settings.Config, ui telemetryUI) error { | ||
config.Telemetry.Load() | ||
|
||
if err := askForTelemetryApproval(config, ui); err != nil { | ||
config.Telemetry.Client = telemetry.CreateClient(telemetry.User{}, false) | ||
return err | ||
} | ||
|
||
config.Telemetry.Client = telemetry.CreateClient(telemetry.User{ | ||
UniqueID: config.Telemetry.UniqueID, | ||
UserID: config.Telemetry.UserID, | ||
}, config.Telemetry.IsActive) | ||
return nil | ||
} | ||
|
||
func askForTelemetryApproval(config *settings.Config, ui telemetryUI) error { | ||
// If we already have telemetry information or that telemetry is explicitly disabled, skip | ||
if config.Telemetry.HasAnsweredPrompt || config.Telemetry.DisabledFromParams { | ||
return nil | ||
} | ||
|
||
// If stdin is not available, send telemetry event, disactive telemetry and return | ||
if !term.IsTerminal(int(os.Stdin.Fd())) { | ||
telemetry.SendTelemetryApproval(telemetry.User{}, telemetry.NoStdin) | ||
config.Telemetry.IsActive = false | ||
return nil | ||
} | ||
|
||
// Else ask user for telemetry approval | ||
fmt.Println("CircleCI would like to collect CLI usage data for product improvement purposes.") | ||
fmt.Println("") | ||
fmt.Println("Participation is voluntary, and your choice can be changed at any time through the command `cli telemetry enable` and `cli telemetry disable`.") | ||
fmt.Println("For more information, please see our privacy policy at https://circleci.com/legal/privacy/.") | ||
fmt.Println("") | ||
config.Telemetry.IsActive = ui.AskUserToApproveTelemetry("Enable telemetry?") | ||
config.Telemetry.HasAnsweredPrompt = true | ||
|
||
// If user allows telemetry, create a telemetry user | ||
user := telemetry.User{} | ||
if config.Telemetry.UniqueID == "" { | ||
user.UniqueID = uuid.New().String() | ||
} | ||
|
||
if config.Telemetry.IsActive && config.Token != "" { | ||
me, err := api.GetMe(rest.NewFromConfig(config.Host, config)) | ||
if err != nil { | ||
user.UserID = me.ID | ||
} | ||
} | ||
config.Telemetry.UniqueID = user.UniqueID | ||
config.Telemetry.UserID = user.UserID | ||
|
||
// Send telemetry approval event | ||
approval := telemetry.Enabled | ||
if !config.Telemetry.IsActive { | ||
approval = telemetry.Disabled | ||
} | ||
if err := telemetry.SendTelemetryApproval(telemetry.User{}, approval); err != nil { | ||
return err | ||
} | ||
|
||
// Write telemetry | ||
if err := config.Telemetry.Write(); err != nil { | ||
return errors.Wrap(err, "Writing telemetry to disk") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.