Skip to content

Show Feature #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pasty is a cli for pastebin.com. With it you can:
first, login:

- visit [pastebin.com](https://pastebin.com/) and login
- visit [https://pastebin.com/doc_api#1](https://pastebin.com/api_doc)
- visit [https://pastebin.com/doc_api](https://pastebin.com/doc_api)
- save your api key in a file called `pasty.api` in your home directory (`~/.pasty.api`)
- the contents of your `.pasty.api` should just be the key, nothing else

Expand Down
25 changes: 19 additions & 6 deletions bin/main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/env node

const yargs = require('yargs');
const { hideBin } = require('yargs/helpers');
const { readFileSync } = require('fs');
const { homedir } = require('os');
const {
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { readFileSync }from 'fs';
import { homedir } from 'os';
import {
createPaste,
deletePaste,
listPastes,
loginUser,
logout,
getUserInfo,
} = require('../lib/api-functions.js');
} from '../lib/api-functions.js';
import { openInBrowser } from '../lib/util.js';

let userToken;
let apiToken;
Expand Down Expand Up @@ -144,5 +145,17 @@ yargs(hideBin(process.argv))
console.log(await getUserInfo(apiToken, userToken));
}
)
.command(
'show <paste id>',
'opens specified paste in your default browser',
(yargs) => {
return yargs.positional('pasteid', {
describe: 'id of the paste you want to open in browser'
});
},
async (argv) => {
await openInBrowser(argv.pasteid);
}
)
.demandCommand(1)
.parse();
39 changes: 15 additions & 24 deletions lib/api-functions.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
const inquirer = require('inquirer');
const fetch = require('node-fetch');
const { API_URLS, FORMAT_CHOICES } = require('./constant.js');
const { readFileSync, rmSync, writeFileSync, existsSync } = require('fs');
const { homedir } = require('os');
const { table } = require('table');
const {
import inquirer from 'inquirer';
import fetch from 'node-fetch';
import { API_URLS, FORMAT_CHOICES } from './constant.js';
import { readFileSync, rmSync, writeFileSync, existsSync } from 'fs';
import { homedir } from 'os';
import { table } from 'table';
import {
getListTable,
mapToVisiblityCode,
tokenGuard,
getUserData,
} = require('./util.js');

module.exports = {
loginUser,
listPastes,
logout,
deletePaste,
createPaste,
getUserInfo,
};

async function loginUser(argv, apiToken) {
} from './util.js';

export async function loginUser(argv, apiToken) {
if (tokenGuard(apiToken)) {
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
}
Expand Down Expand Up @@ -56,7 +47,7 @@ async function loginUser(argv, apiToken) {
}
}

async function listPastes(amount, apiToken, userToken) {
export async function listPastes(amount, apiToken, userToken) {
if (tokenGuard(apiToken)) {
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
}
Expand All @@ -83,15 +74,15 @@ async function listPastes(amount, apiToken, userToken) {
}
}

function logout() {
export function logout() {
if (existsSync(`${homedir}/.pasty.user`)) {
rmSync(`${homedir()}/.pasty.user`);
return 'Successfully logged you out.';
}
return "You're currently not logged in (could not find ~/.pasty.user)";
}

async function deletePaste(pasteId, apiToken, userToken) {
export async function deletePaste(pasteId, apiToken, userToken) {
if (tokenGuard(apiToken)) {
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
}
Expand All @@ -113,7 +104,7 @@ async function deletePaste(pasteId, apiToken, userToken) {
return response.status === 200 ? `Success! ${text}` : `Error! ${text}`;
}

async function createPaste(argv, apiToken, userToken) {
export async function createPaste(argv, apiToken, userToken) {
if (tokenGuard(apiToken)) {
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
}
Expand Down Expand Up @@ -152,7 +143,7 @@ async function createPaste(argv, apiToken, userToken) {
return response.status === 200 ? `Success! ${text}` : `Error! ${text}`;
}

async function getUserInfo(apiToken, userToken) {
export async function getUserInfo(apiToken, userToken) {
if (tokenGuard(apiToken)) {
return 'Please provide your pastebin.com API token in the ~/.pasty.api file.';
}
Expand Down
Loading