diff --git a/services.go b/services.go index a29eee4..237e491 100644 --- a/services.go +++ b/services.go @@ -20,20 +20,19 @@ func NewServices(cfg config) *Services { func (s *Services) AddPrivateKey() error { 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 } - if err := script.Exec(`chmod 700 $HOME/.ssh`).Error(); err != nil { + if err := runCmd(`chmod 700 $HOME/.ssh`); err != nil { return err } - _, err := script.Echo(s.cfg.PrivateKey).WriteFile(os.ExpandEnv(`${HOME}/.ssh/id_rsa`)) - if err != nil { + if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey); err != nil { 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 } @@ -54,10 +53,11 @@ func (s *Services) ConfigureSSH() error { 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 } - if err := script.Exec(`chmod 400 $HOME/.ssh/config`).Error(); err != nil { + + if err := runCmd(`chmod 400 $HOME/.ssh/config`); err != nil { return err } @@ -69,22 +69,36 @@ func (s *Services) PushRepository() error { remoteUrl := fmt.Sprintf(`ssh://%v:22/%v`, sshUrl, s.cfg.AppName) 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 } 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 } - if err := script.Exec(`git push dokku main`).Error(); err != nil { + if err := runCmd(`git push dokku main`); err != nil { return err } 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 ( sshConfigTemplate = template.Must(template.New("ssh-config").Parse(` Host *