11#!/usr/bin/env node
22
3- const fs = require ( 'fs' ) ;
4- const path = require ( 'path' ) ;
5- const axios = require ( 'axios' ) ;
3+ import * as fs from 'fs' ;
4+ import * as path from 'path' ;
5+ import axios from 'axios' ;
66
77const API_BASE_URL = process . env . GIT_PROXY_API_URL || 'http://localhost:3000' ;
88const GIT_PROXY_COOKIE_FILE = path . join (
9- process . env . HOME || process . env . USERPROFILE ,
9+ process . env . HOME || process . env . USERPROFILE || '' ,
1010 '.git-proxy-cookies.json' ,
1111) ;
1212
13- async function addSSHKey ( username , keyPath ) {
13+ interface ApiErrorResponse {
14+ error: string ;
15+ }
16+
17+ interface ErrorWithResponse {
18+ response ?: {
19+ data : ApiErrorResponse ;
20+ status: number ;
21+ } ;
22+ code ? : string ;
23+ message: string ;
24+ }
25+
26+ async function addSSHKey ( username : string , keyPath : string ) : Promise < void > {
1427 try {
1528 // Check for authentication
1629 if ( ! fs . existsSync ( GIT_PROXY_COOKIE_FILE ) ) {
@@ -32,6 +45,7 @@ async function addSSHKey(username, keyPath) {
3245 }
3346
3447 console . log ( 'Making API request to:' , `${ API_BASE_URL } /api/v1/user/${ username } /ssh-keys` ) ;
48+
3549 // Make the API request
3650 await axios . post (
3751 `${ API_BASE_URL } /api/v1/user/${ username } /ssh-keys` ,
@@ -47,20 +61,22 @@ async function addSSHKey(username, keyPath) {
4761
4862 console . log ( 'SSH key added successfully!' ) ;
4963 } catch ( error ) {
64+ const axiosError = error as ErrorWithResponse ;
5065 console . error ( 'Full error:' , error ) ;
51- if ( error . response ) {
52- console . error ( 'Response error:' , error . response . data ) ;
53- console . error ( 'Response status:' , error . response . status ) ;
54- } else if ( error . code === 'ENOENT' ) {
66+
67+ if ( axiosError . response ) {
68+ console . error ( 'Response error:' , axiosError . response . data ) ;
69+ console . error ( 'Response status:' , axiosError . response . status ) ;
70+ } else if ( axiosError . code === 'ENOENT' ) {
5571 console . error ( `Error: Could not find SSH key file at ${ keyPath } ` ) ;
5672 } else {
57- console . error ( 'Error:' , error . message ) ;
73+ console . error ( 'Error:' , axiosError . message ) ;
5874 }
5975 process . exit ( 1 ) ;
6076 }
6177}
6278
63- async function removeSSHKey ( username , keyPath ) {
79+ async function removeSSHKey ( username : string , keyPath : string ) : Promise < void > {
6480 try {
6581 // Check for authentication
6682 if ( ! fs . existsSync ( GIT_PROXY_COOKIE_FILE ) ) {
@@ -86,12 +102,14 @@ async function removeSSHKey(username, keyPath) {
86102
87103 console . log ( 'SSH key removed successfully!' ) ;
88104 } catch ( error ) {
89- if ( error . response ) {
90- console . error ( 'Error:' , error . response . data . error ) ;
91- } else if ( error . code === 'ENOENT' ) {
105+ const axiosError = error as ErrorWithResponse ;
106+
107+ if ( axiosError . response ) {
108+ console . error ( 'Error:' , axiosError . response . data . error ) ;
109+ } else if ( axiosError . code === 'ENOENT' ) {
92110 console . error ( `Error: Could not find SSH key file at ${ keyPath } ` ) ;
93111 } else {
94- console . error ( 'Error:' , error . message ) ;
112+ console . error ( 'Error:' , axiosError . message ) ;
95113 }
96114 process . exit ( 1 ) ;
97115 }
@@ -106,8 +124,8 @@ const keyPath = args[2];
106124if ( ! command || ! username || ! keyPath ) {
107125 console . log ( `
108126Usage:
109- Add SSH key: node ssh-key.js add <username> <path-to-public-key>
110- Remove SSH key: node ssh-key.js remove <username> <path-to-public-key>
127+ Add SSH key: npx tsx src/cli/ ssh-key.ts add <username> <path-to-public-key>
128+ Remove SSH key: npx tsx src/cli/ ssh-key.ts remove <username> <path-to-public-key>
111129 ` ) ;
112130 process . exit ( 1 ) ;
113131}
0 commit comments