refactor: pass functions into own file

This commit is contained in:
decentral1se 2021-09-04 23:28:54 +02:00
parent 9bf11961d5
commit ba7b18f703
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 49 additions and 37 deletions

49
secret/pass.go Normal file
View File

@ -0,0 +1,49 @@
package secret
import (
"errors"
"fmt"
"os/exec"
"github.com/sirupsen/logrus"
)
// PassInsertSecret inserts a secret into a pass store.
func PassInsertSecret(secretValue, secretName, appName, server string) error {
if _, err := exec.LookPath("pass"); err != nil {
return errors.New("pass command not found on $PATH, is it installed?")
}
cmd := fmt.Sprintf(
"echo %s | pass insert hosts/%s/%s/%s -m",
secretValue, server, appName, secretName,
)
if err := exec.Command("bash", "-c", cmd).Run(); err != nil {
return err
}
logrus.Infof("'%s' inserted into pass store", secretName)
return nil
}
// PassRmSecret deletes a secret from a pass store.
func PassRmSecret(secretName, appName, server string) error {
if _, err := exec.LookPath("pass"); err != nil {
return errors.New("pass command not found on $PATH, is it installed?")
}
cmd := fmt.Sprintf(
"pass rm --force hosts/%s/%s/%s",
server, appName, secretName,
)
if err := exec.Command("bash", "-c", cmd).Run(); err != nil {
return err
}
logrus.Infof("'%s' removed from pass store", secretName)
return nil
}

View File

@ -1,9 +1,7 @@
package secret
import (
"errors"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
@ -140,38 +138,3 @@ func GenerateSecrets(secretEnvVars map[string]string, appName, server string) (m
return secrets, nil
}
func PassInsertSecret(secretValue, secretName, appName, server string) error {
_, err := exec.LookPath("pass")
if err != nil {
return errors.New("pass cannot be found on your $PATH, is it installed?")
}
cmd := fmt.Sprintf(
"echo %s | pass insert hosts/%s/%s/%s -m",
secretValue, server, appName, secretName,
)
if err := exec.Command("bash", "-c", cmd).Run(); err != nil {
return err
}
return nil
}
func PassRmSecret(secretName, appName, server string) error {
_, err := exec.LookPath("pass")
if err != nil {
return errors.New("pass cannot be found on your $PATH, is it installed?")
}
cmd := fmt.Sprintf(
"pass rm --force hosts/%s/%s/%s",
server, appName, secretName,
)
if err := exec.Command("bash", "-c", cmd).Run(); err != nil {
return err
}
return nil
}