|
| 1 | +import { Setup } from '..' |
| 2 | +import { Flag, SubCommand } from '../../cli/dvc/constants' |
| 3 | +import { AvailableCommands, InternalCommands } from '../../commands/internal' |
| 4 | +import { definedAndNonEmpty } from '../../util/array' |
| 5 | +import { getInput } from '../../vscode/inputBox' |
| 6 | +import { quickPickYesOrNo } from '../../vscode/quickPick' |
| 7 | +import { Title } from '../../vscode/title' |
| 8 | +import { Toast } from '../../vscode/toast' |
| 9 | +import { getOnlyOrPickProject } from '../../workspace/util' |
| 10 | + |
| 11 | +const noExistingOrUserConfirms = async ( |
| 12 | + internalCommands: InternalCommands, |
| 13 | + dvcRoot: string |
| 14 | +): Promise<boolean | undefined> => { |
| 15 | + const remoteList = await internalCommands.executeCommand( |
| 16 | + AvailableCommands.REMOTE, |
| 17 | + dvcRoot, |
| 18 | + SubCommand.LIST |
| 19 | + ) |
| 20 | + |
| 21 | + if (!remoteList) { |
| 22 | + return true |
| 23 | + } |
| 24 | + |
| 25 | + return await quickPickYesOrNo( |
| 26 | + 'make this new remote the default', |
| 27 | + 'keep the current default', |
| 28 | + { |
| 29 | + placeHolder: 'Would you like to set this new remote as the default?', |
| 30 | + title: Title.SET_REMOTE_AS_DEFAULT |
| 31 | + } |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +const addRemoteToProject = async ( |
| 36 | + internalCommands: InternalCommands, |
| 37 | + dvcRoot: string |
| 38 | +): Promise<void> => { |
| 39 | + const name = await getInput(Title.ENTER_REMOTE_NAME) |
| 40 | + if (!name) { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + const url = await getInput(Title.ENTER_REMOTE_URL) |
| 45 | + if (!url) { |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + const args = [Flag.PROJECT, name, url] |
| 50 | + |
| 51 | + const shouldSetAsDefault = await noExistingOrUserConfirms( |
| 52 | + internalCommands, |
| 53 | + dvcRoot |
| 54 | + ) |
| 55 | + if (shouldSetAsDefault === undefined) { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if (shouldSetAsDefault) { |
| 60 | + args.unshift(Flag.DEFAULT) |
| 61 | + } |
| 62 | + |
| 63 | + return await Toast.showOutput( |
| 64 | + internalCommands.executeCommand( |
| 65 | + AvailableCommands.REMOTE, |
| 66 | + dvcRoot, |
| 67 | + SubCommand.ADD, |
| 68 | + ...args |
| 69 | + ) |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +export const getAddRemoteCommand = |
| 74 | + (setup: Setup, internalCommands: InternalCommands) => |
| 75 | + async (): Promise<void> => { |
| 76 | + const dvcRoots = setup.getRoots() |
| 77 | + if (!definedAndNonEmpty(dvcRoots)) { |
| 78 | + return Toast.showError('Cannot add a remote without a DVC project') |
| 79 | + } |
| 80 | + const dvcRoot = await getOnlyOrPickProject(dvcRoots) |
| 81 | + |
| 82 | + if (!dvcRoot) { |
| 83 | + return |
| 84 | + } |
| 85 | + return addRemoteToProject(internalCommands, dvcRoot) |
| 86 | + } |
0 commit comments