-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Telemetry Opt-Out and Settings Command (#94)
* Add settings command and TelemetryEventListener * setup settings command * Refactor UserSettings class * add welcome message * finish settings command * add settings command documentation * add unit tests for settings command * clean up * Fix telemetry disable logic * make eventsourcename accessible * fix bugs and modify schema to add schema header * address PR comments Co-authored-by: Ryan Fu <[email protected]>
- Loading branch information
Showing
14 changed files
with
619 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
# Winget-Create CLI Settings | ||
|
||
You can configure Winget-Create by editing the `settings.json` file. Running `wingetcreate settings` will open the file in the default json editor; if no editor is configured, Windows will prompt for you to select an editor, and Notepad is sensible option if you have no other preference. | ||
|
||
## Telemetry | ||
|
||
The `telemetry` settings control whether Winget-Create writes ETW events that may be sent to Microsoft on a default installation of Windows. | ||
|
||
See [details on telemetry](https://github.com/microsoft/winget-create#datatelemetry), and our [primary privacy statement](../PRIVACY.md). | ||
|
||
### disable | ||
|
||
```json | ||
"telemetry": { | ||
"disable": true | ||
}, | ||
``` | ||
|
||
If set to true, the `telemetry.disable` setting will prevent any event from being written by the program. |
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,105 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
namespace Microsoft.WingetCreateCLI.Commands | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using CommandLine; | ||
using Microsoft.WingetCreateCLI.Logging; | ||
using Microsoft.WingetCreateCLI.Models.Settings; | ||
using Microsoft.WingetCreateCLI.Properties; | ||
using Microsoft.WingetCreateCLI.Telemetry; | ||
using Microsoft.WingetCreateCLI.Telemetry.Events; | ||
|
||
/// <summary> | ||
/// Command to either update or delete the cached GitHub Oauth token. | ||
/// </summary> | ||
[Verb("settings", HelpText = "SettingsCommand_HelpText", ResourceType = typeof(Resources))] | ||
public class SettingsCommand : BaseCommand | ||
{ | ||
/// <summary> | ||
/// Executes the token command flow. | ||
/// </summary> | ||
/// <returns>Boolean representing success or fail of the command.</returns> | ||
public override Task<bool> Execute() | ||
{ | ||
CommandExecutedEvent commandEvent = new CommandExecutedEvent | ||
{ | ||
Command = nameof(SettingsCommand), | ||
}; | ||
|
||
try | ||
{ | ||
if (!File.Exists(UserSettings.SettingsJsonPath)) | ||
{ | ||
Logger.WarnLocalized(nameof(Resources.GenerateNewSettingsFile_Message)); | ||
UserSettings.SaveSettings(); | ||
} | ||
else | ||
{ | ||
(bool isSettingsValid, List<string> settingsFileErrors) = UserSettings.ParseJsonFile(UserSettings.SettingsJsonPath, out SettingsManifest manifest); | ||
|
||
if (isSettingsValid) | ||
{ | ||
File.Copy(UserSettings.SettingsJsonPath, UserSettings.SettingsBackupJsonPath, true); | ||
} | ||
else | ||
{ | ||
this.DisplayParsingErrors(settingsFileErrors, UserSettings.SettingsJsonPath); | ||
} | ||
} | ||
|
||
if (File.Exists(UserSettings.SettingsBackupJsonPath)) | ||
{ | ||
(bool isBackupValid, List<string> backupFileErrors) = UserSettings.ParseJsonFile(UserSettings.SettingsBackupJsonPath, out SettingsManifest manifest); | ||
|
||
if (!isBackupValid) | ||
{ | ||
this.DisplayParsingErrors(backupFileErrors, UserSettings.SettingsBackupJsonPath); | ||
} | ||
} | ||
|
||
return Task.FromResult(commandEvent.IsSuccessful = this.OpenJsonFile(UserSettings.SettingsJsonPath)); | ||
} | ||
finally | ||
{ | ||
TelemetryManager.Log.WriteEvent(commandEvent); | ||
} | ||
} | ||
|
||
private void DisplayParsingErrors(List<string> errors, string path) | ||
{ | ||
Logger.WarnLocalized(nameof(Resources.ErrorParsingSettingsFile_Message), Path.GetFileName(path)); | ||
|
||
Console.WriteLine(); | ||
foreach (string e in errors) | ||
{ | ||
Logger.Warn(e); | ||
} | ||
} | ||
|
||
private bool OpenJsonFile(string path) | ||
{ | ||
if (!File.Exists(path)) | ||
{ | ||
return false; | ||
} | ||
|
||
try | ||
{ | ||
Process.Start(new ProcessStartInfo { UseShellExecute = true, FileName = path }); | ||
return true; | ||
} | ||
catch (Win32Exception e) | ||
{ | ||
Logger.ErrorLocalized(nameof(Resources.Error_Prefix), e.Message); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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,34 @@ | ||
//---------------------- | ||
// <auto-generated> | ||
// Generated using the NJsonSchema v10.4.3.0 (Newtonsoft.Json v11.0.0.0) (http://NJsonSchema.org) | ||
// </auto-generated> | ||
//---------------------- | ||
|
||
namespace Microsoft.WingetCreateCLI.Models.Settings | ||
{ | ||
#pragma warning disable // Disable all warnings | ||
|
||
/// <summary>Telemetry settings</summary> | ||
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.4.3.0 (Newtonsoft.Json v11.0.0.0)")] | ||
public partial class Telemetry | ||
{ | ||
/// <summary>Controls whether telemetry events are written</summary> | ||
[Newtonsoft.Json.JsonProperty("disable", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] | ||
public bool Disable { get; set; } = false; | ||
|
||
|
||
} | ||
|
||
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.4.3.0 (Newtonsoft.Json v11.0.0.0)")] | ||
public partial class SettingsManifest | ||
{ | ||
/// <summary>The settings json schema</summary> | ||
[Newtonsoft.Json.JsonProperty("$schema", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] | ||
public string Schema { get; set; } = "https://aka.ms/wingetcreate-settings.schema.json"; | ||
|
||
[Newtonsoft.Json.JsonProperty("telemetry", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] | ||
public Telemetry Telemetry { get; set; } | ||
|
||
|
||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.