Skip to content

Commit

Permalink
feat: implement version check
Browse files Browse the repository at this point in the history
  • Loading branch information
10ko authored and thsig committed Jun 27, 2019
1 parent 398005c commit 9b07794
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
4 changes: 4 additions & 0 deletions garden-service/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
getLogLevelChoices,
parseLogLevel,
helpTextMaxWidth,
checkForUpdates,
} from "./helpers"
import { defaultEnvironments, ProjectConfig } from "../config/project"
import {
Expand Down Expand Up @@ -301,6 +302,9 @@ export class GardenCli {
const analytics = await new AnalyticsHandler(garden).init()
analytics.trackCommand(command.getFullName())

// tslint:disable-next-line: no-floating-promises
checkForUpdates(garden.globalConfigStore, headerLog)

// TODO: enforce that commands always output DeepPrimitiveMap
result = await command.action({
garden,
Expand Down
36 changes: 35 additions & 1 deletion garden-service/src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ import {
InternalError,
} from "../exceptions"
import { LogLevel } from "../logger/log-node"
import { getEnumKeys } from "../util/util"
import { getEnumKeys, getPackageVersion } from "../util/util"
import axios from "axios"
import qs = require("qs")
import { platform, release } from "os"
import { LogEntry } from "../logger/log-entry"
import { VERSION_CHECK_URL } from "../constants"
import { printHeader } from "../logger/util"
import { GlobalConfigStore, globalConfigKeys } from "../config-store"
import moment = require("moment")

// Parameter types T which map between the Parameter<T> class and the Sywac cli library.
// In case we add types that aren't supported natively by Sywac, see: http://sywac.io/docs/sync-config.html#custom
Expand Down Expand Up @@ -207,3 +215,29 @@ export function failOnInvalidOptions(argv, ctx) {
ctx.cliMessage(`Received invalid flag(s): ${invalid.join(", ")}`)
}
}

export async function checkForUpdates(config: GlobalConfigStore, logger: LogEntry) {
const query = {
gardenVersion: getPackageVersion(),
platform: platform(),
platformVersion: release(),
}
try {
const res = await axios.get(`${VERSION_CHECK_URL}?${qs.stringify(query)}`)
const configObj = await config.get()
const showMessage = (configObj.lastVersionCheck
&& moment().subtract(1, "days").isAfter(moment(configObj.lastVersionCheck.lastRun)))

if (showMessage || !configObj.lastVersionCheck) {
if (res.data.status === "OUTDATED") {
printHeader(logger, res.data.message, "warning")
} else {
printHeader(logger, res.data.message, "thumbsup")
}
await config.set([globalConfigKeys.lastVersionCheck], { lastRun: new Date() })
}
} catch (err) {
logger.verbose("Something went wrong while checking for the latest Garden version.")
logger.verbose(err)
}
}
15 changes: 13 additions & 2 deletions garden-service/src/config-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,30 @@ export interface AnalyticsGlobalConfig {
firstRun: boolean
}

export interface VersionCheckGlobalConfig {
lastRun: Date
}

export interface GlobalConfig {
analytics?: AnalyticsGlobalConfig
lastVersionCheck?: VersionCheckGlobalConfig
}

const AnalyticsGlobalConfigSchema = joi.object()
const analyticsGlobalConfigSchema = joi.object()
.keys({
userId: joiPrimitive().allow("").optional(),
optedIn: joi.boolean().optional(),
firstRun: joi.boolean().optional(),
}).meta({ internal: true })

const versionCheckGlobalConfigSchema = joi.object()
.keys({
lastRun: joi.date().optional(),
}).meta({ internal: true })

const globalConfigSchemaKeys = {
analytics: AnalyticsGlobalConfigSchema,
analytics: analyticsGlobalConfigSchema,
lastVersionCheck: versionCheckGlobalConfigSchema,
}

/* This contains a config key, key string pair to be used when setting/getting values in the store
Expand Down
2 changes: 2 additions & 0 deletions garden-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ export const SUPPORTED_PLATFORMS: SupportedPlatform[] = ["linux", "darwin", "win

export const SEGMENT_DEV_API_KEY = "D3DUZ3lBSDO3krnuIO7eYDdtlDAjooKW"
export const SEGMENT_PROD_API_KEY = "b6ovUD9A0YjQqT3ZWetWUbuZ9OmGxKMa"

export const VERSION_CHECK_URL = "https://get.garden.io/version"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@
},
"snyk": true,
"dependencies": {}
}
}

0 comments on commit 9b07794

Please sign in to comment.