44 "fmt"
55 "os"
66 "path/filepath"
7+ "slices"
78 "strings"
89)
910
@@ -36,16 +37,22 @@ func (m *Manager) Init() error {
3637 backupFile := configFile + ".backup"
3738
3839 if _ , err := os .Stat (configFile ); err == nil {
39- // 1. backup current ssh config
40- content , err := os .ReadFile (configFile )
41- if err != nil {
42- return fmt .Errorf ("failed to read ssh config for backup: %w" , err )
43- }
44- if err := os .WriteFile (backupFile , content , 0600 ); err != nil {
45- return fmt .Errorf ("failed to create backup: %w" , err )
40+ // 1. backup current ssh config if backup doesn't exist
41+ if _ , err := os .Stat (backupFile ); os .IsNotExist (err ) {
42+ content , err := os .ReadFile (configFile )
43+ if err != nil {
44+ return fmt .Errorf ("failed to read ssh config for backup: %w" , err )
45+ }
46+ if err := os .WriteFile (backupFile , content , 0600 ); err != nil {
47+ return fmt .Errorf ("failed to create backup: %w" , err )
48+ }
4649 }
4750
4851 // 2. Create a new ssh config with the include
52+ content , err := os .ReadFile (configFile )
53+ if err != nil {
54+ return fmt .Errorf ("failed to read ssh config: %w" , err )
55+ }
4956 // Check if Include line already exists
5057 found := false
5158 for line := range strings .SplitSeq (string (content ), "\n " ) {
@@ -128,23 +135,23 @@ func (m *Manager) UpdateConfig(name string, opts ConfigOptions) error {
128135 } else if strings .HasPrefix (lowerTrimmed , "hostname " ) {
129136 foundFields ["hostname" ] = true
130137 if opts .Hostname != "" {
131- indent := line [:strings .Index (lowerTrimmed , "hostname" )]
138+ indent := line [:strings .Index (strings . ToLower ( line ) , "hostname" )]
132139 newLines = append (newLines , indent + "Hostname " + opts .Hostname )
133140 } else {
134141 newLines = append (newLines , line )
135142 }
136143 } else if strings .HasPrefix (lowerTrimmed , "user " ) {
137144 foundFields ["user" ] = true
138145 if opts .User != "" {
139- indent := line [:strings .Index (lowerTrimmed , "user" )]
146+ indent := line [:strings .Index (strings . ToLower ( line ) , "user" )]
140147 newLines = append (newLines , indent + "User " + opts .User )
141148 } else {
142149 newLines = append (newLines , line )
143150 }
144151 } else if strings .HasPrefix (lowerTrimmed , "port " ) {
145152 foundFields ["port" ] = true
146153 if opts .Port != 0 {
147- indent := line [:strings .Index (lowerTrimmed , "port" )]
154+ indent := line [:strings .Index (strings . ToLower ( line ) , "port" )]
148155 newLines = append (newLines , indent + fmt .Sprintf ("Port %d" , opts .Port ))
149156 } else {
150157 newLines = append (newLines , line )
@@ -157,23 +164,23 @@ func (m *Manager) UpdateConfig(name string, opts ConfigOptions) error {
157164 if absPath , err := filepath .Abs (identity ); err == nil {
158165 identity = absPath
159166 }
160- indent := line [:strings .Index (lowerTrimmed , "identityfile" )]
167+ indent := line [:strings .Index (strings . ToLower ( line ) , "identityfile" )]
161168 newLines = append (newLines , indent + "IdentityFile " + identity )
162169 } else {
163170 newLines = append (newLines , line )
164171 }
165172 } else if strings .HasPrefix (lowerTrimmed , "forwardagent " ) {
166173 foundFields ["forwardagent" ] = true
167174 if opts .ForwardAgent != "" {
168- indent := line [:strings .Index (lowerTrimmed , "forwardagent" )]
175+ indent := line [:strings .Index (strings . ToLower ( line ) , "forwardagent" )]
169176 newLines = append (newLines , indent + "ForwardAgent " + opts .ForwardAgent )
170177 } else {
171178 newLines = append (newLines , line )
172179 }
173180 } else if strings .HasPrefix (lowerTrimmed , "proxyjump " ) {
174181 foundFields ["proxyjump" ] = true
175182 if opts .ProxyJump != "" {
176- indent := line [:strings .Index (lowerTrimmed , "proxyjump" )]
183+ indent := line [:strings .Index (strings . ToLower ( line ) , "proxyjump" )]
177184 newLines = append (newLines , indent + "ProxyJump " + opts .ProxyJump )
178185 } else {
179186 newLines = append (newLines , line )
@@ -258,5 +265,6 @@ func (m *Manager) ListConfigs() ([]string, error) {
258265 configs = append (configs , entry .Name ())
259266 }
260267 }
268+ slices .Sort (configs )
261269 return configs , nil
262270}
0 commit comments