3
3
*/
4
4
5
5
import { TerminalKind } from "../../../Contracts/ViewModels" ;
6
+ import { userContext } from "../../../UserContext" ;
6
7
7
- export const getCommands = ( terminalKind : TerminalKind ) : string => {
8
- if ( ! Commands [ terminalKind ] ) {
9
- throw new Error ( `Unsupported terminal kind: ${ terminalKind } ` ) ;
8
+ export const getCommands = ( terminalKind : TerminalKind , key : string ) => {
9
+ let databaseacc = userContext . databaseAccount ;
10
+ let endpoint ;
11
+ switch ( terminalKind ) {
12
+ case TerminalKind . Postgres :
13
+ endpoint = databaseacc . properties . postgresqlEndpoint ;
14
+ break ;
15
+ case TerminalKind . Mongo :
16
+ endpoint = databaseacc . properties . mongoEndpoint ;
17
+ break ;
18
+ case TerminalKind . VCoreMongo :
19
+ endpoint = databaseacc . properties . vcoreMongoEndpoint ;
20
+ break ;
21
+ case TerminalKind . Cassandra :
22
+ endpoint = databaseacc . properties . cassandraEndpoint ;
23
+ break ;
24
+ default :
25
+ throw new Error ( "Unknown Terminal Kind" ) ;
10
26
}
11
- return Commands [ terminalKind ] . join ( "\n" ) . concat ( "\n" ) ;
27
+
28
+ let config = {
29
+ host : getHostFromUrl ( endpoint ) ,
30
+ name : databaseacc . name ,
31
+ password : key ,
32
+ endpoint : endpoint
33
+ } ;
34
+
35
+ return commands ( terminalKind , config ) . join ( "\n" ) . concat ( "\n" ) ;
12
36
} ;
13
37
14
- export const Commands : Record < TerminalKind , string [ ] > = {
15
- [ TerminalKind . Postgres ] : [
16
- "curl -s https://ipinfo.io" ,
17
- "curl -LO https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.bz2" ,
18
- "tar -xvjf postgresql-15.2.tar.bz2" ,
19
- "cd postgresql-15.2" ,
20
- "mkdir ~/pgsql" ,
21
- "curl -LO https://ftp.gnu.org/gnu/readline/readline-8.1.tar.gz" ,
22
- "tar -xvzf readline-8.1.tar.gz" ,
23
- "cd readline-8.1" ,
24
- "./configure --prefix=$HOME/pgsql"
25
- ] ,
26
- [ TerminalKind . Mongo ] : [
27
- "curl -s https://ipinfo.io" ,
28
- "curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz" ,
29
- "tar -xvzf mongosh-2.3.8-linux-x64.tgz" ,
30
- "mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/" ,
31
- "echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc" ,
32
- "source ~/.bashrc" ,
33
- "mongosh --version"
34
- ] ,
35
- [ TerminalKind . VCoreMongo ] : [
36
- "curl -s https://ipinfo.io" ,
37
- "curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz" ,
38
- "tar -xvzf mongosh-2.3.8-linux-x64.tgz" ,
39
- "mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/" ,
40
- "echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc" ,
41
- "source ~/.bashrc" ,
42
- "mongosh --version"
43
- ] ,
44
- [ TerminalKind . Cassandra ] : [
45
- "curl -s https://ipinfo.io" ,
46
- "curl -LO https://downloads.apache.org/cassandra/4.1.2/apache-cassandra-4.1.2-bin.tar.gz" ,
47
- "tar -xvzf apache-cassandra-4.1.2-bin.tar.gz" ,
48
- "cd apache-cassandra-4.1.2" ,
49
- "mkdir ~/cassandra" ,
50
- "echo 'export CASSANDRA_HOME=$HOME/cassandra' >> ~/.bashrc" ,
51
- "source ~/.bashrc"
52
- ] ,
53
- [ TerminalKind . Default ] : [
54
- "echo Unknown Shell"
55
- ] ,
38
+ export interface CommandConfig {
39
+ host : string ,
40
+ name : string ,
41
+ password : string ,
42
+ endpoint : string
43
+ }
44
+
45
+ export const commands = ( terminalKind : TerminalKind , config ?: CommandConfig ) : string [ ] => {
46
+ switch ( terminalKind ) {
47
+ case TerminalKind . Postgres :
48
+ return [
49
+ "curl -s https://ipinfo.io" ,
50
+ "curl -LO https://ftp.postgresql.org/pub/source/v15.2/postgresql-15.2.tar.bz2" ,
51
+ "tar -xvjf postgresql-15.2.tar.bz2" ,
52
+ "cd postgresql-15.2" ,
53
+ "mkdir ~/pgsql" ,
54
+ "curl -LO https://ftp.gnu.org/gnu/readline/readline-8.1.tar.gz" ,
55
+ "tar -xvzf readline-8.1.tar.gz" ,
56
+ "cd readline-8.1" ,
57
+ "./configure --prefix=$HOME/pgsql"
58
+ ] ;
59
+ case TerminalKind . Mongo || TerminalKind . VCoreMongo :
60
+ return [
61
+ "curl -s https://ipinfo.io" ,
62
+ "curl -LO https://downloads.mongodb.com/compass/mongosh-2.3.8-linux-x64.tgz" ,
63
+ "tar -xvzf mongosh-2.3.8-linux-x64.tgz" ,
64
+ "mkdir -p ~/mongosh && mv mongosh-2.3.8-linux-x64/* ~/mongosh/" ,
65
+ "echo 'export PATH=$PATH:$HOME/mongosh/bin' >> ~/.bashrc" ,
66
+ "source ~/.bashrc" ,
67
+ "mongosh --version" ,
68
+ `mongosh --host ${ config . host } --port 10255 --username ${ config . name } --password ${ config . password } --ssl --sslAllowInvalidCertificates`
69
+ ] ;
70
+ case TerminalKind . Cassandra :
71
+ return [
72
+ "curl -s https://ipinfo.io" ,
73
+ "curl -LO https://archive.apache.org/dist/cassandra/5.0.3/apache-cassandra-5.0.3-bin.tar.gz" ,
74
+ "tar -xvzf apache-cassandra-5.0.3-bin.tar.gz" ,
75
+ "mkdir -p ~/cassandra && mv apache-cassandra-5.0.3/* ~/cassandra/" ,
76
+ "echo 'export PATH=$PATH:$HOME/cassandra/bin' >> ~/.bashrc" ,
77
+ "echo 'export SSL_VERSION=TLSv1_2' >> ~/.bashrc" ,
78
+ "echo 'export SSL_VALIDATE=false' >> ~/.bashrc" ,
79
+ "source ~/.bashrc" ,
80
+
81
+ `cqlsh ${ config . host } 10350 -u ${ config . name } -p ${ config . password } --ssl --protocol-version=4`
82
+ ] ;
83
+ default :
84
+ return [ "echo Unknown Shell" ] ;
85
+ }
86
+ }
87
+
88
+ const getHostFromUrl = ( mongoEndpoint : string ) : string => {
89
+ try {
90
+ const url = new URL ( mongoEndpoint ) ;
91
+ return url . hostname ;
92
+ } catch ( error ) {
93
+ console . error ( "Invalid Mongo Endpoint URL:" , error ) ;
94
+ return "" ;
95
+ }
56
96
} ;
0 commit comments