Skip to content

Commit fef9efe

Browse files
Johan Thomsenadamtulinius
authored andcommitted
runtime deps: just-in-time validation for ssh and scp
1 parent 0630d6b commit fef9efe

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

morph.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/dbcdk/morph/ssh"
1414
"github.com/dbcdk/morph/utils"
1515
"os"
16-
"os/exec"
1716
"path/filepath"
1817
"strings"
1918
)
@@ -206,7 +205,7 @@ func listSecretsCmd(cmd *kingpin.CmdClause) *kingpin.CmdClause {
206205
}
207206

208207
func setup() {
209-
handleError(validateEnvironment())
208+
utils.ValidateEnvironment("nix")
210209

211210
utils.AddFinalizer(func() {
212211
assets.Teardown(assetRoot)
@@ -218,22 +217,7 @@ func setup() {
218217
handleError(assetErr)
219218
}
220219

221-
func validateEnvironment() (err error) {
222-
dependencies := []string{"nix", "scp", "ssh"}
223-
missingDepencies := make([]string, 0)
224-
for _, dependency := range dependencies {
225-
_, err := exec.LookPath(dependency)
226-
if err != nil {
227-
missingDepencies = append(missingDepencies, dependency)
228-
}
229-
}
230220

231-
if len(missingDepencies) > 0 {
232-
return errors.New("Missing dependencies: " + strings.Join(missingDepencies, ", "))
233-
}
234-
235-
return nil
236-
}
237221

238222
func main() {
239223

nix/nix.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ func GetPathsToPush(host Host, resultPath string) (paths []string, err error) {
253253
}
254254

255255
func Push(ctx *ssh.SSHContext, host Host, paths ...string) (err error) {
256+
utils.ValidateEnvironment("ssh")
257+
256258
var userArg = ""
257259
var keyArg = ""
258260
var env = os.Environ()

ssh/ssh.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (ctx *SSHContext) sshArgs(host Host, transfer *FileTransfer) (cmd string, a
7777
} else {
7878
cmd = "ssh"
7979
}
80+
utils.ValidateEnvironment(cmd)
8081

8182
if ctx.SkipHostKeyCheck {
8283
args = append(args,

utils/files.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package utils
22

33
import (
4+
"errors"
5+
"fmt"
6+
"os"
7+
"os/exec"
48
"path/filepath"
9+
"strings"
510
)
611

712
func GetAbsPathRelativeTo(path string, reference string) string {
@@ -11,3 +16,18 @@ func GetAbsPathRelativeTo(path string, reference string) string {
1116
return filepath.Join(reference, path)
1217
}
1318
}
19+
20+
func ValidateEnvironment(dependencies ...string) {
21+
missingDepencies := make([]string, 0)
22+
for _, dependency := range dependencies {
23+
_, err := exec.LookPath(dependency)
24+
if err != nil {
25+
missingDepencies = append(missingDepencies, dependency)
26+
}
27+
}
28+
29+
if len(missingDepencies) > 0 {
30+
fmt.Fprint(os.Stderr, errors.New("Missing dependencies: '" + strings.Join(missingDepencies, ", ") + "' on $PATH"))
31+
Exit(1)
32+
}
33+
}

0 commit comments

Comments
 (0)