@@ -48,52 +48,61 @@ type sshConfig struct {
4848 IdentityFile string
4949}
5050
51- func AddSSHConfig (cfg config.PipedGit ) error {
51+ func AddSSHConfig (cfg config.PipedGit ) ( string , error ) {
5252 cfgPath := cfg .SSHConfigFilePath
5353 if cfgPath == "" {
5454 home , err := os .UserHomeDir ()
5555 if err != nil {
56- return fmt .Errorf ("failed to detect the current user's home directory: %w" , err )
56+ return "" , fmt .Errorf ("failed to detect the current user's home directory: %w" , err )
5757 }
5858 cfgPath = path .Join (home , ".ssh" , "config" )
5959 }
6060 sshDir := filepath .Dir (cfgPath )
6161
6262 if err := os .MkdirAll (sshDir , 0700 ); err != nil {
63- return fmt .Errorf ("failed to create a directory %s: %v" , sshDir , err )
63+ return "" , fmt .Errorf ("failed to create a directory %s: %v" , sshDir , err )
6464 }
6565
6666 sshKey , err := cfg .LoadSSHKey ()
6767 if err != nil {
68- return err
68+ return "" , err
6969 }
7070
7171 sshKeyFile , err := os .CreateTemp (sshDir , "piped-ssh-key-*" )
7272 if err != nil {
73- return err
73+ return "" , err
7474 }
75+ needCleanUp := false
76+ defer func () {
77+ if needCleanUp {
78+ os .Remove (sshKeyFile .Name ())
79+ }
80+ }()
7581
76- // TODO: Remove this key file when Piped terminating.
7782 if _ , err := sshKeyFile .Write (sshKey ); err != nil {
78- return err
83+ needCleanUp = true
84+ return "" , err
7985 }
8086
8187 configData , err := generateSSHConfig (cfg , sshKeyFile .Name ())
8288 if err != nil {
83- return err
89+ needCleanUp = true
90+ return "" , err
8491 }
8592
8693 f , err := os .OpenFile (cfgPath , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
8794 if err != nil {
88- return fmt .Errorf ("could not create/append to %s: %v" , cfgPath , err )
95+ needCleanUp = true
96+ return "" , fmt .Errorf ("could not create/append to %s: %v" , cfgPath , err )
8997 }
9098 defer f .Close ()
9199
92100 if _ , err := f .Write ([]byte (configData )); err != nil {
93- return fmt .Errorf ("failed to write sshConfig to %s: %v" , cfgPath , err )
101+ needCleanUp = true
102+ return "" , fmt .Errorf ("failed to write sshConfig to %s: %v" , cfgPath , err )
94103 }
95104
96- return nil
105+ return sshKeyFile . Name (), nil
97106}
98107
99108func generateSSHConfig (cfg config.PipedGit , sshKeyFile string ) (string , error ) {
0 commit comments