Skip to content

Commit

Permalink
(feature) replace credentials file if user asks
Browse files Browse the repository at this point in the history
  • Loading branch information
AHarmlessPyro committed Sep 9, 2022
1 parent 4432014 commit f2fe177
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
4 changes: 3 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
},
"dependencies": {
"@metlo/testing": "^0.0.1",
"@types/valid-url": "^1.0.3",
"axios": "^0.27.2",
"commander": "^9.4.0",
"dotenv": "^16.0.2",
"enquirer": "^2.3.6",
"nodemon": "^2.0.19",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.0"
"tsconfig-paths": "^4.1.0",
"valid-url": "^1.0.9"
}
}
2 changes: 1 addition & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ program.name("metlo").description("Metlo's command line tool.").version("0.0.0")
program
.command("init")
.description("Initialize the Metlo CLI.")
.argument("-b, --backend_url <string>", "The backend address for Metlo")
.option("-b, --backend_url <string>", "The backend address for Metlo")
.action(init)
program
.command("test")
Expand Down
62 changes: 42 additions & 20 deletions cli/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,63 @@ import fs, { writeFileSync } from "fs"
import os from "os"
import path from "path"
import { CREDENTIAL_FILE } from "./utils"
import readline from "readline"
import { promisify } from "util"
import { prompt } from "enquirer"
import { isUri } from "valid-url"

const setup_host = async () => {
const get_host_from_cli = async () => {
// Function returns string, is marked as void for some reason.
return (
await prompt({
const resp = await prompt([
{
type: "input",
name: "host",
message: "Host address of metlo backend :",
},
])
return resp["host"] as string
}

const should_replace_credentials = async () => {
// Function returns string, is marked as void for some reason.
return (
await prompt({
type: "confirm",
name: "replace",
message: `Credentials already exist at ~/${CREDENTIAL_FILE}. Are you sure you want to replace them?`,
})
)["host"]
)["replace"] as boolean
}

const init = async host => {
const setup_new_cred = async host => {
const homeDir = os.homedir()
const creditialFileExists = fs.existsSync(path.join(homeDir, CREDENTIAL_FILE))
if (creditialFileExists) {
console.log(
`Credentials already exist at ~/${CREDENTIAL_FILE}. Are you sure you want to replace them? [Y/n]`,
)
var backend_host: string
if (host && !(host instanceof Object)) {
backend_host = host
} else {
var backend_host: string
console.log(host)
// console.log(rest)
if (host) {
backend_host = host
} else {
backend_host = await setup_host()
}
backend_host = await get_host_from_cli()
}

if (isUri(backend_host)) {
writeFileSync(
path.join(homeDir, CREDENTIAL_FILE),
`METLO_HOST=${backend_host}`,
)
} else {
console.log(`Invalid url passed for backend host : ${backend_host}`)
}
}

const init = async ({ backend_url: host }) => {
const homeDir = os.homedir()
const creditialFileExists = fs.existsSync(path.join(homeDir, CREDENTIAL_FILE))
if (creditialFileExists) {
let shouldReplace = await should_replace_credentials()
if (shouldReplace) {
setup_new_cred(host)
} else {
console.log("Leaving credentials file untouched")
}
} else {
setup_new_cred(host)
}
}

Expand Down
28 changes: 19 additions & 9 deletions cli/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@
"@types/yargs" "^17.0.8"
chalk "^4.0.0"

"@metlo/testing@^0.0.1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@metlo/testing/-/testing-0.0.1.tgz#9c224970304fa465b6bbf9d7e2f9634aec104a77"
integrity sha512-XkgCNfYPG8GlnRMeXbW2K+fj1F+m5SoO+yx62knJxAwzYRoSZiwI0x87axCyMT2twt4+1wbxEvtExs0O6fOMyA==
dependencies:
axios "^0.27.2"
expect "^29.0.2"
vm2 "^3.9.11"

"@jridgewell/resolve-uri@^3.0.3":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.0.tgz#2203b118c157721addfe69d47b70465463066d78"
Expand All @@ -83,6 +74,15 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@metlo/testing@^0.0.1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@metlo/testing/-/testing-0.0.1.tgz#9c224970304fa465b6bbf9d7e2f9634aec104a77"
integrity sha512-XkgCNfYPG8GlnRMeXbW2K+fj1F+m5SoO+yx62knJxAwzYRoSZiwI0x87axCyMT2twt4+1wbxEvtExs0O6fOMyA==
dependencies:
axios "^0.27.2"
expect "^29.0.2"
vm2 "^3.9.11"

"@sinclair/typebox@^0.24.1":
version "0.24.39"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.24.39.tgz#21d0c56c295a00e391e20a88a48c11d4a1210ac5"
Expand Down Expand Up @@ -144,6 +144,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==

"@types/valid-url@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@types/valid-url/-/valid-url-1.0.3.tgz#a124389fb953559c7f889795a98620e91adb3687"
integrity sha512-+33x29mg+ecU88ODdWpqaie2upIuRkhujVLA7TuJjM823cNMbeggfI6NhxewaRaRF8dy+g33e4uIg/m5Mb3xDQ==

"@types/yargs-parser@*":
version "21.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b"
Expand Down Expand Up @@ -739,6 +744,11 @@ v8-compile-cache-lib@^3.0.1:
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==

valid-url@^1.0.9:
version "1.0.9"
resolved "https://registry.yarnpkg.com/valid-url/-/valid-url-1.0.9.tgz#1c14479b40f1397a75782f115e4086447433a200"
integrity sha512-QQDsV8OnSf5Uc30CKSwG9lnhMPe6exHtTXLRYX8uMwKENy640pU+2BgBL0LRbDh/eYRahNCS7aewCx0wf3NYVA==

vm2@^3.9.11:
version "3.9.11"
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.9.11.tgz#a880f510a606481719ec3f9803b940c5805a06fe"
Expand Down

0 comments on commit f2fe177

Please sign in to comment.