Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from Microsoft/jeyou/account-settings
Browse files Browse the repository at this point in the history
Refactoring original Settings into Account Settings and Settings
  • Loading branch information
Jeff Young committed Apr 20, 2016
2 parents 772ea8c + dd1b1ed commit cdc81a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 37 deletions.
72 changes: 41 additions & 31 deletions src/helpers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@ import { workspace } from "vscode";
import { SettingNames } from "./constants";
import { Logger } from "../helpers/logger";

export class Settings {
private _appInsightsEnabled: boolean;
private _appInsightsKey: string;
private _loggingLevel: string;
private _pollingInterval: number;
abstract class BaseSettings {
protected readSetting<T>(name: string, defaultValue:T): T {
let configuration = workspace.getConfiguration();
let value = configuration.get<T>(name, undefined);

// If user specified a value, use it
if (value !== undefined && value !== null) {
return value;
}
return defaultValue;
}
}

export class AccountSettings extends BaseSettings {
private _teamServicesPersonalAccessToken: string;

constructor(account: string) {
let loggingLevel = SettingNames.LoggingLevel;
this._loggingLevel = this.readSetting<string>(loggingLevel, undefined);

super();
// Storing PATs by account in the configuration settings to make switching between accounts easier
this._teamServicesPersonalAccessToken = this.getAccessToken(account);

let pollingInterval = SettingNames.PollingInterval;
this._pollingInterval = this.readSetting<number>(pollingInterval, 5);
Logger.LogDebug("Polling interval value (minutes): " + this._pollingInterval.toString());
// Ensure a minimum value when an invalid value is set
if (this._pollingInterval <= 0) {
Logger.LogDebug("Negative polling interval provided. Setting to default.");
this._pollingInterval = 5;
}

this._appInsightsEnabled = this.readSetting<boolean>(SettingNames.AppInsightsEnabled, true);
this._appInsightsKey = this.readSetting<string>(SettingNames.AppInsightsKey, undefined);
}

private getAccessToken(account: string) : string {
Expand All @@ -58,15 +53,34 @@ export class Settings {
return undefined;
}

private readSetting<T>(name: string, defaultValue:T): T {
let configuration = workspace.getConfiguration();
let value = configuration.get<T>(name, undefined);
public get TeamServicesPersonalAccessToken() : string {
return this._teamServicesPersonalAccessToken;
}
}

// If user specified a value, use it
if (value !== undefined && value !== null) {
return value;
export class Settings extends BaseSettings {
private _appInsightsEnabled: boolean;
private _appInsightsKey: string;
private _loggingLevel: string;
private _pollingInterval: number;

constructor() {
super();

let loggingLevel = SettingNames.LoggingLevel;
this._loggingLevel = this.readSetting<string>(loggingLevel, undefined);

let pollingInterval = SettingNames.PollingInterval;
this._pollingInterval = this.readSetting<number>(pollingInterval, 5);
Logger.LogDebug("Polling interval value (minutes): " + this._pollingInterval.toString());
// Ensure a minimum value when an invalid value is set
if (this._pollingInterval <= 0) {
Logger.LogDebug("Negative polling interval provided. Setting to default.");
this._pollingInterval = 5;
}
return defaultValue;

this._appInsightsEnabled = this.readSetting<boolean>(SettingNames.AppInsightsEnabled, true);
this._appInsightsKey = this.readSetting<string>(SettingNames.AppInsightsKey, undefined);
}

public get AppInsightsEnabled(): boolean {
Expand All @@ -84,8 +98,4 @@ export class Settings {
public get PollingInterval(): number {
return this._pollingInterval;
}

public get TeamServicesPersonalAccessToken() : string {
return this._teamServicesPersonalAccessToken;
}
}
12 changes: 6 additions & 6 deletions src/team-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"use strict";

import { FileSystemWatcher, StatusBarAlignment, StatusBarItem, window, workspace } from "vscode";
import { Settings } from "./helpers/settings";
import { AccountSettings, Settings } from "./helpers/settings";
import { CommandNames, TelemetryEvents, WitTypes } from "./helpers/constants";
import { Logger } from "./helpers/logger";
import { Strings } from "./helpers/strings";
Expand Down Expand Up @@ -37,6 +37,7 @@ export class TeamExtension {
private _serverContext: TeamServerContext;
private _gitContext: GitContext;
private _settings: Settings;
private _accountSettings: AccountSettings;

constructor() {
this.initializeExtension();
Expand Down Expand Up @@ -224,19 +225,18 @@ export class TeamExtension {
this._gitContext = new GitContext(workspace.rootPath);
if (this._gitContext !== undefined && this._gitContext.RemoteUrl !== undefined && this._gitContext.IsTeamServices) {
this.setupFileSystemWatcherOnHead();

this._serverContext = new TeamServerContext(this._gitContext.RemoteUrl);
this._settings = new Settings(this._serverContext.RepoInfo.Account);
this._settings = new Settings();
this.logStart(this._settings.LoggingLevel, workspace.rootPath);
if (this._settings.TeamServicesPersonalAccessToken === undefined) {
this._accountSettings = new AccountSettings(this._serverContext.RepoInfo.Account);
if (this._accountSettings.TeamServicesPersonalAccessToken === undefined) {
Logger.LogError(Strings.NoAccessTokenFound);
this._errorMessage = Strings.NoAccessTokenFound;
VsCodeUtils.ShowErrorMessage(Strings.NoAccessTokenFound);
return;
}

this._serverContext.CredentialHandler = new CredentialInfo(this._accountSettings.TeamServicesPersonalAccessToken).CredentialHandler;
this._teamServicesStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 100);
this._serverContext.CredentialHandler = new CredentialInfo(this._settings.TeamServicesPersonalAccessToken).CredentialHandler;

this._telemetry = new TelemetryService(this._serverContext, this._settings);
Logger.LogDebug("Started ApplicationInsights telemetry");
Expand Down

0 comments on commit cdc81a3

Please sign in to comment.