Files
lazyssh/cmd/main.go
Adem BaccaraandGitHub b7fb586e73 feat(parser): refactor ssh_config parser and writer to preserve unmanaged fields, comments, and directives (#45)
This PR introduces a major refactor of the SSH config parsing and writing logic. The new implementation is more robust and secure, ensuring that only the intended changes are applied while preserving the original file’s structure.

Key changes

- Lossless parsing/writing: Preserve unmanaged fields (e.g., `ProxyJump`), comments, and directives such as `Include` and `Match`.

- Library update: Switched to [github.com/kevinburke/ssh_config](https://github.com/kevinburke/ssh_config)
 as the base parser, with a custom fork to support required modifications https://github.com/adembc/ssh_config.

- Backup policy: Before any modification, create a backup of the SSH config file. Maintain up to 10 backups (configurable in the future) and automatically delete older ones.

- IdentityFile handling: Parse IdentityFile as an array instead of a single string, with improved update logic.

- Bug fix: Resolve issue where tags could not be fully removed.

- Multiple aliases: Support defining and managing multiple aliases for a single server.

- Testability: Code has been refactored with testability in mind. Follow-up PRs will include dedicated tests.
2025-09-10 23:24:50 +01:00

72 lines
1.9 KiB
Go

// Copyright 2025.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/Adembc/lazyssh/internal/adapters/data/ssh_config_file"
"github.com/Adembc/lazyssh/internal/logger"
"github.com/Adembc/lazyssh/internal/adapters/ui"
"github.com/Adembc/lazyssh/internal/core/services"
"github.com/spf13/cobra"
)
var (
version = "develop"
gitCommit = "unknown"
)
func main() {
log, err := logger.New("LAZYSSH")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//nolint:errcheck // log.Sync may return an error which is safe to ignore here
defer log.Sync()
home, err := os.UserHomeDir()
if err != nil {
log.Errorw("failed to get user home directory", "error", err)
//nolint:gocritic // exitAfterDefer: ensure immediate exit on unrecoverable error
os.Exit(1)
}
sshConfigFile := filepath.Join(home, ".ssh", "config")
metaDataFile := filepath.Join(home, ".lazyssh", "metadata.json")
serverRepo := ssh_config_file.NewRepository(log, sshConfigFile, metaDataFile)
serverService := services.NewServerService(log, serverRepo)
tui := ui.NewTUI(log, serverService, version, gitCommit)
rootCmd := &cobra.Command{
Use: ui.AppName,
Short: "Lazy SSH server picker TUI",
RunE: func(cmd *cobra.Command, args []string) error {
return tui.Run()
},
}
rootCmd.SilenceUsage = true
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}