Fixed some issues with writing out the key

This commit is contained in:
Leon Mika 2024-07-13 11:06:04 +10:00
parent 6a7482dde4
commit ce4e5a69eb

View file

@ -20,20 +20,19 @@ func NewServices(cfg config) *Services {
func (s *Services) AddPrivateKey() error { func (s *Services) AddPrivateKey() error {
log.Println("Adding private key") log.Println("Adding private key")
if err := script.Exec(`mkdir -p $HOME/.ssh`).Error(); err != nil { if err := runCmd(`mkdir -p $HOME/.ssh`); err != nil {
return err return err
} }
if err := script.Exec(`chmod 700 $HOME/.ssh`).Error(); err != nil { if err := runCmd(`chmod 700 $HOME/.ssh`); err != nil {
return err return err
} }
_, err := script.Echo(s.cfg.PrivateKey).WriteFile(os.ExpandEnv(`${HOME}/.ssh/id_rsa`)) if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey); err != nil {
if err != nil {
return err return err
} }
if err := script.Exec(`chmod -R 400 $HOME/.ssh/.`).Error(); err != nil { if err := runCmd(`chmod -R 400 $HOME/.ssh/.`); err != nil {
return err return err
} }
@ -54,10 +53,11 @@ func (s *Services) ConfigureSSH() error {
return err return err
} }
if _, err := script.Echo(bfr.String()).WriteFile(os.ExpandEnv(`${HOME}/.ssh/config`)); err != nil { if err := writeFile(`${HOME}/.ssh/config`, bfr.String()); err != nil {
return err return err
} }
if err := script.Exec(`chmod 400 $HOME/.ssh/config`).Error(); err != nil {
if err := runCmd(`chmod 400 $HOME/.ssh/config`); err != nil {
return err return err
} }
@ -69,22 +69,36 @@ func (s *Services) PushRepository() error {
remoteUrl := fmt.Sprintf(`ssh://%v:22/%v`, sshUrl, s.cfg.AppName) remoteUrl := fmt.Sprintf(`ssh://%v:22/%v`, sshUrl, s.cfg.AppName)
log.Println("Testing Dokku SSH connection") log.Println("Testing Dokku SSH connection")
if err := script.Exec(fmt.Sprintf(`ssh %v version`, sshUrl)).Error(); err != nil { if err := runCmd(fmt.Sprintf(`ssh %v version`, sshUrl)); err != nil {
return err return err
} }
log.Println("SSH connection good. Pushing repository") log.Println("SSH connection good. Pushing repository")
if err := script.Exec(fmt.Sprintf(`git remote add dokku '%v'`, remoteUrl)).Error(); err != nil { if err := runCmd(fmt.Sprintf(`git remote add dokku '%v'`, remoteUrl); err != nil {
return err return err
} }
if err := script.Exec(`git push dokku main`).Error(); err != nil { if err := runCmd(`git push dokku main`); err != nil {
return err return err
} }
return nil return nil
} }
func runCmd(cmd string) error {
fullCmd := os.ExpandEnv(cmd)
log.Printf(" .. [exec] %v", fullCmd)
return script.Exec(fullCmd).Error()
}
func writeFile(path string, content string) error {
fullPath := os.ExpandEnv(path)
log.Printf(" .. [file] %v", fullPath)
_, err := script.Echo(content).WriteFile(fullPath)
return err
}
var ( var (
sshConfigTemplate = template.Must(template.New("ssh-config").Parse(` sshConfigTemplate = template.Must(template.New("ssh-config").Parse(`
Host * Host *