@remote.it/cli
Version:
remote.it cross-platform command line
148 lines (132 loc) • 4.52 kB
text/typescript
import { GluegunToolbox } from 'gluegun'
import {
ServiceTypes,
User,
ConfigFile,
Device,
ServiceType,
} from '@remote.it/core'
export default {
name: 'create',
alias: 'c',
description: 'Create a new service connection',
async run(toolbox: GluegunToolbox) {
const { bootstrap, parameters, print, prompt } = toolbox
// Make sure everything is setup before continuing
await bootstrap()
const config = new ConfigFile()
//-------------------------------------------------------
// Make sure pre-requisites exist before continuing
//-------------------------------------------------------
if (!config || !config.exists) {
print.error(`Config file "${config.location}" is invalid or missing`)
process.exit(1)
return
}
if (!config.target || !config.target.secret) {
print.error(
`No target device configured in "${config.location}". Please create a target device using "remoteit target create".`
)
process.exit(1)
return
}
if (!config.user || !config.user.username || !config.user.authHash) {
print.error(
`No user found in config file "${config.location}". Please sign in using "remoteit signin".`
)
process.exit(1)
return
}
//-------------------------------------------------------
// Prompt for missing values
//-------------------------------------------------------
const defaultService = ServiceTypes.tcp.id
const defaultName = ServiceTypes.tcp.name
const defaultHostname = '127.0.0.1'
let port = Number(parameters.first)
let type = Number(parameters.second)
let name = parameters.third
let hostname =
parameters.array && parameters.array.length >= 4
? parameters.array[3]
: defaultHostname
// If the passes in parameters, we assume they want to create
// a service with default values
if (parameters.array && parameters.array.length) {
type = type || defaultService
name = name || defaultName
hostname = hostname || defaultHostname
}
if (!port) {
const result = await prompt.ask({
type: 'input',
name: 'port',
message: 'What is the port you want to target?',
})
if (result && result.port) port = Number(result.port)
}
if (!type) {
print.table([
['Name', 'ID'],
...Object.values(ServiceTypes.all).map((t: ServiceType) => [
t.name,
t.id,
]),
])
const result = await prompt.ask({
type: 'input',
name: 'type',
message: 'What is the ID of the type of service you want to register?',
initial: 1,
})
if (result && result.type) type = Number(result.type)
}
if (!name) {
const result = await prompt.ask({
type: 'input',
name: 'name',
message: 'What is the name of your service?',
initial: ServiceTypes.findByID(type).name,
})
if (result && result.name) name = result.name
}
//-------------------------------------------------------
// Get info from config file
//-------------------------------------------------------
const username = config.user.username
const authHash = config.user.authHash
//-------------------------------------------------------
// Get an API token
//-------------------------------------------------------
const signInSpinner = toolbox.print.spin(
`Authenticating user "${username}"`
)
await User.authHashSignIn(username, authHash)
signInSpinner.succeed(`User "${username}" authenticated`)
//-------------------------------------------------------
// Create the service via the API
//-------------------------------------------------------
const registerSpinner = toolbox.print.spin(`Creating service "${name}"`)
const device = new Device(config.target)
const service = await device.addService({ port, type, name })
if (!service.uid || !service.secret) {
print.error('No UID or secret created for service!')
process.exit(1)
return
}
const services = config.services
services.push({
name: service.name,
port,
hardwareID: service.hardwareID,
hostname,
secret: service.secret,
type: service.type,
uid: service.uid,
})
config.services = services
registerSpinner.succeed(
`Created service "${name}" (${service.uid}) running on port ${port}`
)
},
}