Skip to content

Commit 002d1d8

Browse files
committed
feat: connect to ssh by username/password #3
Signed-off-by: seven <[email protected]>
1 parent ccedc54 commit 002d1d8

File tree

6 files changed

+35
-31
lines changed

6 files changed

+35
-31
lines changed

src/store/secretStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type Secret = {
1616
username?: string;
1717
password?: string;
1818
};
19+
1920
export const useSecretStore = defineStore('secretStore', {
2021
state: (): {
2122
secrets: Secret[];
@@ -46,5 +47,8 @@ export const useSecretStore = defineStore('secretStore', {
4647
this.secrets.splice(index, 1);
4748
await secretDataSource.removeSecret(secret);
4849
},
50+
async getSecret(secretId: string) {
51+
return this.secrets.find(({ id }) => id === secretId);
52+
},
4953
},
5054
});

src/views/secret/components/new-key-dialog.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<n-tab-pane :name="SecretType.PASSWORD" tab="Username/Password">
6262
<n-grid cols="8" item-responsive responsive="screen" x-gap="10" y-gap="10">
6363
<n-grid-item span="8">
64-
<n-form-item :label="$t('secret.name')" path="username">
64+
<n-form-item :label="$t('secret.username')" path="username">
6565
<n-input
6666
v-model:value="formData.username"
6767
clearable

src/views/ssh/components/ssh-dialog.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</n-grid-item>
4646
<n-grid-item span="8">
4747
<n-form-item :label="$t('ssh.secret')" path="secret">
48-
<n-select v-model:value="formData.secret" :options="options" />
48+
<n-select v-model:value="formData.secretId" :options="options" />
4949
</n-form-item>
5050
</n-grid-item>
5151
</n-grid>
@@ -132,7 +132,7 @@ const formRules = reactive({
132132
trigger: ['input', 'blur'],
133133
},
134134
],
135-
secret: [
135+
secretId: [
136136
{
137137
required: true,
138138
renderMessage: () => lang.t('ssh.formValidation.secretRequired'),

src/views/ssh/components/ssh-list.vue

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@
2020
</div>
2121
</template>
2222
</n-card>
23-
<new-key-dialog ref="editKeyDialogRef" />
2423
</div>
2524
</template>
2625

2726
<script setup lang="ts">
2827
import { storeToRefs } from 'pinia';
2928
import { useLang } from '../../../lang';
3029
import { Connection, useConnectionStore } from '../../../store';
31-
import { CustomError } from '../../../common';
32-
import NewKeyDialog from '../../secret/components/new-key-dialog.vue';
3330
34-
const emits = defineEmits(['edit-connect']);
31+
const emits = defineEmits(['edit-connect', 'establish-connect']);
3532
3633
const dialog = useDialog();
3734
const message = useMessage();
@@ -43,7 +40,7 @@ const options = reactive([
4340
{ key: 3, label: lang.t('ssh.operations.remove') },
4441
]);
4542
const connectionStore = useConnectionStore();
46-
const { fetchConnections, removeConnection, establishConnection } = connectionStore;
43+
const { fetchConnections, removeConnection } = connectionStore;
4744
const { connections } = storeToRefs(connectionStore);
4845
fetchConnections();
4946
@@ -62,17 +59,7 @@ const handleSelect = (key: number, connection: Connection) => {
6259
};
6360
6461
const establishConnect = async (connection: Connection) => {
65-
try {
66-
await establishConnection(connection);
67-
} catch (err) {
68-
const error = err as CustomError;
69-
message.error(`status: ${error.status}, details: ${error.details}`, {
70-
closable: true,
71-
keepAliveOnHover: true,
72-
duration: 36000000,
73-
});
74-
// debug('connect error');
75-
}
62+
emits('establish-connect', connection);
7663
};
7764
7865
// edit connect info

src/views/ssh/components/ssh-terminal.vue

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import { invoke } from '@tauri-apps/api/tauri';
77
import '@xterm/xterm/css/xterm.css';
88
import { Terminal } from '@xterm/xterm';
99
import { FitAddon } from 'xterm-addon-fit';
10+
import { defineProps } from 'vue';
11+
import { Connection, useSecretStore } from '../../../store';
12+
13+
const props = defineProps({
14+
connectRef: Object as Connection,
15+
});
16+
17+
const secretStore = useSecretStore();
18+
const { getSecret, loadSecrets } = secretStore;
1019
1120
const terminal = new Terminal({
1221
cursorBlink: true, // 光标闪烁
@@ -54,7 +63,8 @@ const exec = (command: string) => {
5463
.catch(e => terminal.writeln(e));
5564
};
5665
57-
onMounted(() => {
66+
onMounted(async () => {
67+
await loadSecrets();
5868
terminal.loadAddon(fitAddon);
5969
// Attach the terminal to the container
6070
terminal.open(terminalContainer.value);
@@ -68,14 +78,10 @@ onMounted(() => {
6878
// terminal.write(data);
6979
// console.log('terminal on data', data);
7080
// });
71-
81+
const { host, port, secretId } = props.connectRef;
82+
const secret = await getSecret(secretId);
7283
// Invoke the command
73-
invoke('connect_ssh', {
74-
host: 'xxx',
75-
port: 22,
76-
username: 'xxxx',
77-
password: 'xxxx',
78-
})
84+
invoke('connect_ssh', { host, port, username: secret?.username, password: secret?.password })
7985
// eslint-disable-next-line no-console
8086
.then(res => console.log(`ssh connect res: ${res}`))
8187
// eslint-disable-next-line no-console

src/views/ssh/index.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
>
1111
<n-tab-pane v-for="panel in panelsRef" :key="panel" :name="panel">
1212
<div class="ssh-list">
13-
<ssh-terminal v-if="terminalRef" />
14-
<ssh-list v-else @edit-connect="editSshHandler" />
13+
<ssh-terminal v-if="terminalRef" :connect-ref="connectRef" />
14+
<ssh-list v-else @edit-connect="editSshHandler" @establish-connect="establishSshHandler" />
1515
</div>
1616
</n-tab-pane>
1717
</n-tabs>
@@ -24,20 +24,27 @@ import sshModal from './components/ssh-dialog.vue';
2424
import sshList from './components/ssh-list.vue';
2525
import sshTerminal from './components/ssh-terminal.vue';
2626
import SshFloatMenu from './components/ssh-float-menu.vue';
27+
import { Connection } from '../../store';
2728
2829
// DOM
2930
const sshModalRef = ref();
3031
3132
const terminalRef = ref(false);
32-
33+
const connectRef = ref({});
3334
const addSsh = () => sshModalRef.value.showMedal();
3435
3536
const editSshHandler = (row: object) => {
3637
sshModalRef.value.showMedal(row);
3738
};
39+
const establishSshHandler = (row: Connection) => {
40+
// eslint-disable-next-line no-console
41+
console.log('establish emit', row);
42+
connectRef.value = row;
43+
terminalRef.value = true;
44+
};
3845
3946
const valueRef = ref('home');
40-
const panelsRef = ref(['home', '2', '3', '4', '5']);
47+
const panelsRef = ref(['home']);
4148
4249
const addableRef = computed(() => {
4350
return {

0 commit comments

Comments
 (0)