Removed the use of script

This commit is contained in:
Leon Mika 2024-07-13 11:21:00 +10:00
parent e872bcccae
commit e45923c947
2 changed files with 13 additions and 21 deletions

7
go.mod
View file

@ -1,10 +1,3 @@
module lmika.dev/actions/push-to-dokku
go 1.22.4
require (
github.com/bitfield/script v0.22.1 // indirect
github.com/itchyny/gojq v0.12.13 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
mvdan.cc/sh/v3 v3.7.0 // indirect
)

View file

@ -3,9 +3,10 @@ package main
import (
"bytes"
"fmt"
"github.com/bitfield/script"
"log"
"os"
"os/exec"
"strings"
"text/template"
)
@ -20,7 +21,7 @@ func NewServices(cfg config) *Services {
func (s *Services) AddPrivateKey() error {
log.Println("Adding private key")
if err := runCmd(`mkdir -p $HOME/.ssh`); err != nil {
if err := os.MkdirAll(os.ExpandEnv("${HOME}/.ssh"), 0700); err != nil {
return err
}
@ -28,10 +29,6 @@ func (s *Services) AddPrivateKey() error {
return err
}
if err := runCmd(`chmod 700 $HOME/.ssh`); err != nil {
return err
}
return nil
}
@ -61,31 +58,33 @@ func (s *Services) PushRepository() error {
remoteUrl := fmt.Sprintf(`ssh://%v:22/%v`, sshUrl, s.cfg.AppName)
log.Println("Testing Dokku SSH connection")
if err := runCmd(fmt.Sprintf(`ssh %v version`, sshUrl)); err != nil {
if err := runCmd("ssh", sshUrl, "version"); err != nil {
return err
}
log.Println("SSH connection good. Pushing repository")
if err := runCmd(fmt.Sprintf(`git remote add dokku '%v'`, remoteUrl)); err != nil {
if err := runCmd("git", "remote", "add", "dokku", remoteUrl); err != nil {
return err
}
if err := runCmd(`git push dokku main`); 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 runCmd(cmd ...string) error {
log.Printf(" .. [exec] %v", strings.Join(cmd, " "))
e := exec.Command(cmd[0], cmd[1:]...)
e.Stderr = os.Stderr
e.Stdout = os.Stdout
return e.Run()
}
func writeFile(path string, content string, mode os.FileMode) error {
fullPath := os.ExpandEnv(path)
log.Printf(" .. [file] %v", fullPath)
log.Printf(" .. [file] %v (%v bytes)", fullPath, len(content))
return os.WriteFile(fullPath, []byte(content), mode)
}