@@ -17,6 +17,7 @@ import (
1717 "github.com/mitchellh/cli"
1818 "github.com/mitchellh/go-homedir"
1919 "github.com/posener/complete"
20+ "github.com/roots/trellis-cli/command"
2021 "github.com/roots/trellis-cli/trellis"
2122)
2223
@@ -82,7 +83,7 @@ func (c *KeyGenerateCommand) Run(args []string) int {
8283 return 1
8384 }
8485
85- _ , err = exec . Command ("gh" , "auth" , "status" ).Output ()
86+ _ , err = command . Cmd ("gh" , [] string { "auth" , "status" } ).Output ()
8687 if err != nil {
8788 c .UI .Error ("Error: GitHub CLI is not authenticated." )
8889 c .UI .Error ("Run `gh auth login` first." )
@@ -128,7 +129,7 @@ func (c *KeyGenerateCommand) Run(args []string) int {
128129 }
129130
130131 keygenArgs := []string {"-t" , "ed25519" , "-C" , deployKeyName , "-f" , keyPath , "-P" , "" }
131- sshKeygen := exec . Command ("ssh-keygen" , keygenArgs ... )
132+ sshKeygen := command . Cmd ("ssh-keygen" , keygenArgs )
132133 sshKeygen .Stdout = io .Discard
133134 sshKeygen .Stderr = os .Stderr
134135 err := sshKeygen .Run ()
@@ -197,14 +198,15 @@ func (c *KeyGenerateCommand) Run(args []string) int {
197198 return 1
198199 }
199200
200- keyscanOutput , err := exec .Command ("ssh-keyscan" , "-t" , "ed25519" , "-H" , strings .Join (hosts , " " )).Output ()
201- if err != nil {
202- c .UI .Error ("Error: could not set SSH known hosts. ssh-keyscan command failed." )
203- c .UI .Error (err .Error ())
201+ knownHosts := keyscanHosts (hosts )
202+
203+ if len (knownHosts ) == 0 {
204+ c .UI .Error ("Error: could not set SSH known hosts." )
205+ c .UI .Error (fmt .Sprintf ("ssh-keyscan command failed for all hosts: %s" , hosts ))
204206 return 1
205207 }
206208
207- err = githubCLI ("secret" , "set" , sshKnownHostsSecret , "--body" , string ( keyscanOutput ))
209+ err = githubCLI ("secret" , "set" , sshKnownHostsSecret , "--body" , strings . Join ( knownHosts , " \n " ))
208210 if err != nil {
209211 c .UI .Error ("Error: could not set GitHub secret" )
210212 c .UI .Error (err .Error ())
@@ -308,15 +310,16 @@ func (c *KeyGenerateCommand) AutocompleteFlags() complete.Flags {
308310}
309311
310312func githubCLI (args ... string ) error {
311- ghCmd := exec . Command ("gh" , args ... )
313+ ghCmd := command . Cmd ("gh" , args )
312314 ghCmd .Stdout = io .Discard
313315 ghCmd .Stderr = os .Stderr
314316
315317 return ghCmd .Run ()
316318}
317319
318320func getAnsibleHosts () (hosts []string , err error ) {
319- hostsOutput , err := exec .Command ("ansible" , "all" , "--list-hosts" ).Output ()
321+ args := []string {"all" , "--list-hosts" , "--limit" , "!development" }
322+ hostsOutput , err := command .Cmd ("ansible" , args ).Output ()
320323
321324 if err != nil {
322325 return nil , err
@@ -352,8 +355,26 @@ func parseAnsibleHosts(output string) (hosts []string) {
352355 continue
353356 }
354357
358+ // remove default placeholder since it will cause an error
359+ // this isn't ideal, but it will do
360+ if host == "your_server_hostname" {
361+ continue
362+ }
363+
355364 hosts = append (hosts , host )
356365 }
357366
358367 return hosts
359368}
369+
370+ func keyscanHosts (hosts []string ) (knownHosts []string ) {
371+ for _ , host := range hosts {
372+ output , err := command .Cmd ("ssh-keyscan" , []string {"-t" , "ed25519" , "-H" , "-T" , "1" , host }).Output ()
373+
374+ if err == nil {
375+ knownHosts = append (knownHosts , string (output ))
376+ }
377+ }
378+
379+ return knownHosts
380+ }
0 commit comments