Skip to content

Commit

Permalink
feat: notify user if old configuration version
Browse files Browse the repository at this point in the history
  • Loading branch information
edosrecki committed Jul 24, 2023
1 parent e814d7e commit 12c1507
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/lib/configurations/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import boxen from 'boxen'
import { blue } from 'chalk'
import exitHook from 'exit-hook'
import { kebabCase, omit } from 'lodash'
import { printGoogleCloudRedisAuthString } from '../gcloud/redis'
Expand All @@ -10,7 +12,7 @@ import {
import { Configuration, ConfigurationCreateAnswers } from '../types'
import { appendOrReplaceByKey, deleteByKey, findByKey } from '../util/array'
import { randomString } from '../util/string'
import { store } from './store'
import { currentConfigurationVersion, store } from './store'

const storeKey = 'configurations' as const
const searchKey = 'configurationName' as const
Expand All @@ -26,7 +28,10 @@ export const getConfiguration = (name: string): Configuration | undefined => {
}

export const saveConfiguration = (answers: ConfigurationCreateAnswers): void => {
const configuration = omit(answers, excludeProperties)
const configuration = {
configurationVersion: currentConfigurationVersion,
...omit(answers, excludeProperties),
}

const configurations = store.get(storeKey)
appendOrReplaceByKey(configurations, configuration, searchKey)
Expand All @@ -39,7 +44,30 @@ export const deleteConfiguration = (configuratioName: string): void => {
store.set(storeKey, configurations)
}

const checkConfigurationVersion = ({ configurationVersion }: Configuration): void => {
if (configurationVersion && configurationVersion >= currentConfigurationVersion) {
return
}

const text =
`Your configuration was created with older version of the app.\n` +
`Recreate it to use latest features:\n\n` +
`> ${blue('google-cloud-redis')} configurations create`

const box = boxen(text, {
title: 'Warning',
titleAlignment: 'center',
borderColor: 'yellow',
margin: { top: 1, right: 0, bottom: 1, left: 0 },
padding: { top: 0, right: 1, bottom: 0, left: 1 },
})

console.log(box)
}

export const execConfiguration = (configuration: Configuration) => {
checkConfigurationVersion(configuration)

const pod = {
name: `redis-proxy-${kebabCase(configuration.configurationName)}-${randomString()}`,
context: configuration.kubernetesContext,
Expand Down
3 changes: 3 additions & 0 deletions src/lib/configurations/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type Schema = {
configurations: Configuration[]
}

export const currentConfigurationVersion = 2

export const store = new Conf<Schema>({
configName: 'configurations',
projectSuffix: '',
Expand All @@ -15,6 +17,7 @@ export const store = new Conf<Schema>({
items: {
type: 'object',
properties: {
configurationVersion: { type: 'number' },
configurationName: { type: 'string' },
googleCloudProject: { type: 'string' },
googleCloudRedisInstance: {
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GoogleCloudRedisInstance } from './gcloud/redis'

// configurations
export type Configuration = {
configurationVersion?: number
configurationName: string
googleCloudProject: string
googleCloudRedisInstance: GoogleCloudRedisInstance
Expand Down

0 comments on commit 12c1507

Please sign in to comment.