@@ -16,6 +16,7 @@ package main
1616
1717import (
1818 "fmt"
19+ "io"
1920 "os"
2021 "path/filepath"
2122
@@ -28,44 +29,82 @@ import (
2829)
2930
3031var (
31- version = "develop"
32- gitCommit = "unknown"
33- )
32+ version = "develop"
33+ gitCommit = "unknown"
34+ sshConfigFile string
3435
35- func main () {
36- log , err := logger .New ("LAZYSSH" )
37- if err != nil {
38- fmt .Println (err )
39- os .Exit (1 )
40- }
36+ rootCmd = & cobra.Command {
37+ Use : ui .AppName ,
38+ Short : "Lazy SSH server picker TUI" ,
39+ RunE : func (cmd * cobra.Command , args []string ) error {
40+ log , err := logger .New ("LAZYSSH" )
41+ if err != nil {
42+ fmt .Println (err )
43+ os .Exit (1 )
44+ }
4145
42- //nolint:errcheck // log.Sync may return an error which is safe to ignore here
43- defer log .Sync ()
46+ //nolint:errcheck // log.Sync may return an error which is safe to ignore here
47+ defer log .Sync ()
4448
45- home , err := os .UserHomeDir ()
46- if err != nil {
47- log .Errorw ("failed to get user home directory" , "error" , err )
48- //nolint:gocritic // exitAfterDefer: ensure immediate exit on unrecoverable error
49- os .Exit (1 )
50- }
51- sshConfigFile := filepath .Join (home , ".ssh" , "config" )
52- metaDataFile := filepath .Join (home , ".lazyssh" , "metadata.json" )
49+ home , err := os .UserHomeDir ()
50+ if err != nil {
51+ log .Errorw ("failed to get user home directory" , "error" , err )
52+ // nolint:gocritic // exitAfterDefer: ensure immediate exit on unrecoverable error
53+ os .Exit (1 )
54+ }
5355
54- serverRepo := ssh_config_file .NewRepository (log , sshConfigFile , metaDataFile )
55- serverService := services .NewServerService (log , serverRepo )
56- tui := ui .NewTUI (log , serverService , version , gitCommit )
56+ if sshConfigFile == "" {
57+ sshConfigFile = filepath .Join (home , ".ssh" , "config" )
58+ } else {
59+ f , err := os .CreateTemp ("" , "tmpfile-" )
60+ if err != nil {
61+ log .Fatal (err )
62+ }
63+
64+ // close and remove the temporary file at the end of the program
65+ defer f .Close ()
66+ defer os .Remove (f .Name ())
67+
68+ // write data to the temporary file
69+ fd , err := os .Open (sshConfigFile )
70+ if err != nil {
71+ fmt .Fprintf (os .Stderr , "Error opening file: %v\n " , err )
72+ os .Exit (1 )
73+ }
74+ defer fd .Close ()
75+
76+ // Read the entire contents at once
77+ content , err := io .ReadAll (fd )
78+ if err != nil {
79+ fmt .Fprintf (os .Stderr , "Error reading file: %v\n " , err )
80+ os .Exit (1 )
81+ }
82+ if _ , err := f .WriteString (string (content )); err != nil {
83+ log .Fatal (err )
84+ }
85+
86+ sshConfigFile = f .Name ()
87+ }
88+
89+ metaDataFile := filepath .Join (home , ".lazyssh" , "metadata.json" )
90+ serverRepo := ssh_config_file .NewRepository (log , sshConfigFile , metaDataFile )
91+ serverService := services .NewServerService (log , serverRepo )
92+ tui := ui .NewTUI (log , serverService , version , gitCommit )
5793
58- rootCmd := & cobra.Command {
59- Use : ui .AppName ,
60- Short : "Lazy SSH server picker TUI" ,
61- RunE : func (cmd * cobra.Command , args []string ) error {
6294 return tui .Run ()
6395 },
6496 }
65- rootCmd . SilenceUsage = true
97+ )
6698
99+ func main () {
67100 if err := rootCmd .Execute (); err != nil {
68101 _ , _ = fmt .Fprintln (os .Stderr , err )
69102 os .Exit (1 )
70103 }
71104}
105+
106+ func init () {
107+ rootCmd .PersistentFlags ().StringVar (& sshConfigFile , "sshconfig" , "" , "path to ssh config file (default: ~/.ssh/config)" )
108+
109+ rootCmd .SilenceUsage = true
110+ }
0 commit comments