Skip to content

Commit

Permalink
fix(CLI): use moment to parse dataset backup list date parameters, al…
Browse files Browse the repository at this point in the history
…low only dates.
  • Loading branch information
j33ty committed Feb 7, 2024
1 parent cc7c4cb commit 442ea6c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
"log-symbols": "^2.2.0",
"mendoza": "^3.0.0",
"module-alias": "^2.2.2",
"moment": "^2.30.1",
"nano-pubsub": "^2.0.1",
"nanoid": "^3.1.30",
"observable-callback": "^1.0.1",
Expand Down Expand Up @@ -315,6 +316,7 @@
"@types/connect-history-api-fallback": "^1.5.2",
"@types/lodash": "^4.14.149",
"@types/log-symbols": "^2.0.0",
"@types/moment": "^2.13.0",
"@types/raf": "^3.4.0",
"@types/refractor": "^3.0.0",
"@types/resolve-from": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ const downloadBackupCommand: CliCommandDefinition = {
} catch (error) {
spinner.fail()
let msg = error.statusCode ? error.response.body.message : error.message
// eslint-disable-next-line no-warning-comments
// TODO: Pull this out in a common error handling function for reusability.
// If no message can be extracted, print the whole error.
if (msg === undefined) {
msg = String(error)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {CliCommandDefinition} from '@sanity/cli'
import {Table} from 'console-table-printer'
import {lightFormat} from 'date-fns'
import moment from 'moment'
import resolveApiClient from '../../actions/backup/resolveApiClient'
import {defaultApiVersion, validateLimit} from './backupGroup'

Expand Down Expand Up @@ -30,14 +31,14 @@ type ListBackupResponseItem = {
const helpText = `
Options
--limit <int> Maximum number of backups returned. Default 30.
--after <string> Only return backups after this timestamp (inclusive)
--before <string> Only return backups before this timestamp (exclusive). Cannot be younger than <after> if specified.
--after <string> Only return backups after this date (inclusive)
--before <string> Only return backups before this date (exclusive). Cannot be younger than <after> if specified.
Examples
sanity backup list DATASET_NAME
sanity backup list DATASET_NAME --limit 50
sanity backup list DATASET_NAME --after 2024-01-01 --limit 10
sanity backup list DATASET_NAME --after 2024-01-01T12:00:01Z --before 2024-01-10
sanity backup list DATASET_NAME --after 2024-01-31 --limit 10
sanity backup list DATASET_NAME --after 2024-01-31 --before 2024-01-10
`

const listDatasetBackupCommand: CliCommandDefinition<ListDatasetBackupFlags> = {
Expand Down Expand Up @@ -67,23 +68,23 @@ const listDatasetBackupCommand: CliCommandDefinition<ListDatasetBackupFlags> = {
}

if (flags.after) {
try {
query.start = new Date(flags.after).toISOString()
} catch (err) {
throw new Error(`Parsing --after date: ${err}`)
if (moment(flags.after, 'YYYY-MM-DD', true).isValid()) {
query.start = flags.after
} else {
throw new Error('Invalid after date format. Use YYYY-MM-DD')
}
}

if (flags.before) {
try {
query.end = new Date(flags.before).toISOString()
} catch (err) {
throw new Error(`Parsing --before date: ${err}`)
if (moment(flags.before, 'YYYY-MM-DD', true).isValid()) {
query.end = flags.before
} else {
throw new Error('Invalid before date format. Use YYYY-MM-DD')
}
}

if (query.start && query.end && query.start >= query.end) {
throw new Error('--after timestamp must be before --before')
if (query.start && query.end && moment(query.start).isAfter(query.end)) {
throw new Error('--after date must be before --before')
}

let response
Expand Down

0 comments on commit 442ea6c

Please sign in to comment.