Skip to content

Commit 30fb996

Browse files
Merge pull request #188 from misha-ssh/add-address-form
Add address form
2 parents c7d65e0 + 417b325 commit 30fb996

6 files changed

Lines changed: 55 additions & 8 deletions

File tree

internal/command/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var createCmd = &cobra.Command{
3333
connection := &connect.Connect{
3434
Alias: fields.Alias,
3535
Login: fields.Login,
36+
Address: fields.Address,
3637
Password: fields.Password,
3738
CreatedAt: time.Now().Format("2006.01.02 15:04:05"),
3839
UpdatedAt: time.Now().Format("2006.01.02 15:04:05"),

internal/command/update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var updateCmd = &cobra.Command{
4545
updatedConnection := &connect.Connect{
4646
Alias: fields.Alias,
4747
Login: fields.Login,
48+
Address: fields.Address,
4849
Password: fields.Password,
4950
UpdatedAt: time.Now().Format("2006.01.02 15:04:05"),
5051
CreatedAt: selectedConn.CreatedAt,

internal/component/create/create.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ func Run() (*Fields, error) {
4444
Validate(huh.ValidateNotEmpty()).
4545
Value(&fields.Login),
4646

47+
huh.NewInput().
48+
Title("Address").
49+
Description("Address of the remote machine").
50+
Validate(huh.ValidateNotEmpty()).
51+
Value(&fields.Address),
52+
4753
huh.NewInput().
4854
Title("Port").
4955
Description("Port number to connect to a remote machine").
@@ -72,7 +78,8 @@ func Run() (*Fields, error) {
7278
Description("select file with private key").
7379
CurrentDirectory(homedir).
7480
Validate(privateKeyValidate).
75-
Value(&fields.PrivateKey),
81+
Value(&fields.PrivateKey).
82+
Picking(true),
7683
).WithHideFunc(func() bool {
7784
return authPassConfirm
7885
}),

internal/component/create/fields.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package create
44
type Fields struct {
55
Alias string
66
Login string
7+
Address string
78
Password string
89
Port int
910
PrivateKey string

internal/component/update/fields.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package update
44
type Fields struct {
55
Alias string
66
Login string
7+
Address string
78
Password string
89
Port int
910
PrivateKey string

internal/component/update/update.go

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ var (
1818
// Run get form for update connect.Connect
1919
func Run(connection *connect.Connect) (*Fields, error) {
2020
var authPassConfirm bool
21+
var updatedPrivateKey bool
2122

2223
updatedConn := *connection
2324

24-
if len(updatedConn.Password) > 0 {
25+
hasOriginalPrivateKey := len(connection.SshOptions.PrivateKey) > 0
26+
hasOriginalPassword := len(connection.Password) > 0
27+
28+
if hasOriginalPassword {
2529
authPassConfirm = true
26-
} else if len(updatedConn.SshOptions.PrivateKey) > 0 {
30+
} else if hasOriginalPrivateKey {
2731
authPassConfirm = false
32+
} else {
33+
authPassConfirm = true
2834
}
2935

3036
homedir, err := os.UserHomeDir()
@@ -55,6 +61,12 @@ func Run(connection *connect.Connect) (*Fields, error) {
5561
Validate(huh.ValidateNotEmpty()).
5662
Value(&updatedConn.Login),
5763

64+
huh.NewInput().
65+
Title("Address").
66+
Description("Address of the remote machine").
67+
Validate(huh.ValidateNotEmpty()).
68+
Value(&updatedConn.Address),
69+
5870
huh.NewInput().
5971
Title("Port").
6072
Description("Port number to connect to a remote machine").
@@ -77,15 +89,34 @@ func Run(connection *connect.Connect) (*Fields, error) {
7789
).WithHideFunc(func() bool {
7890
return !authPassConfirm
7991
}),
92+
huh.NewGroup(
93+
huh.NewConfirm().
94+
Title("Update private key?").
95+
Description("Do you want to update the private key?").
96+
Affirmative("Yes").
97+
Negative("No").
98+
Value(&updatedPrivateKey),
99+
).WithHideFunc(func() bool {
100+
return authPassConfirm || !hasOriginalPrivateKey
101+
}),
80102
huh.NewGroup(
81103
huh.NewFilePicker().
82104
Title("PrivateKey").
83-
Description("select file with private key").
105+
Description("Select file with private key").
84106
CurrentDirectory(homedir).
85107
Validate(privateKeyValidate).
86-
Value(&updatedConn.SshOptions.PrivateKey),
108+
Value(&updatedConn.SshOptions.PrivateKey).
109+
Picking(true),
87110
).WithHideFunc(func() bool {
88-
return authPassConfirm
111+
if authPassConfirm {
112+
return true
113+
}
114+
115+
if hasOriginalPrivateKey {
116+
return !updatedPrivateKey
117+
}
118+
119+
return false
89120
}),
90121
).WithShowHelp(true).Run()
91122
if err != nil {
@@ -99,15 +130,20 @@ func Run(connection *connect.Connect) (*Fields, error) {
99130

100131
fields.Alias = updatedConn.Alias
101132
fields.Login = updatedConn.Login
133+
fields.Address = updatedConn.Address
102134
fields.Port = intPort
103135
fields.Password = updatedConn.Password
104136
fields.PrivateKey = updatedConn.SshOptions.PrivateKey
105137

106-
if len(connection.Password) > 0 && !authPassConfirm {
138+
if hasOriginalPrivateKey && !updatedPrivateKey && !authPassConfirm {
139+
fields.PrivateKey = connection.SshOptions.PrivateKey
140+
}
141+
142+
if hasOriginalPassword && !authPassConfirm {
107143
fields.Password = ""
108144
}
109145

110-
if len(updatedConn.SshOptions.PrivateKey) > 0 && authPassConfirm {
146+
if hasOriginalPrivateKey && authPassConfirm {
111147
fields.PrivateKey = ""
112148
}
113149

0 commit comments

Comments
 (0)