-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from moul/wrapper
Add wrapper (#122)
- Loading branch information
Showing
10 changed files
with
512 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/codegangsta/cli" | ||
|
||
"github.com/moul/advanced-ssh-config/pkg/config" | ||
. "github.com/moul/advanced-ssh-config/pkg/logger" | ||
) | ||
|
||
func cmdWrapper(c *cli.Context) { | ||
if len(c.Args()) < 1 { | ||
Logger.Fatalf("Missing <target> argument. See usage with 'assh wrapper %s -h'.", c.Command.Name) | ||
} | ||
|
||
// prepare variables | ||
target := c.Args()[0] | ||
command := c.Args()[1:] | ||
options := []string{} | ||
for _, flag := range config.SSHBoolFlags { | ||
if c.Bool(flag) { | ||
options = append(options, fmt.Sprintf("-%s", flag)) | ||
} | ||
} | ||
for _, flag := range config.SSHStringFlags { | ||
if val := c.String(flag); val != "" { | ||
options = append(options, fmt.Sprintf("-%s", flag)) | ||
options = append(options, val) | ||
} | ||
} | ||
args := []string{c.Command.Name} | ||
args = append(args, options...) | ||
args = append(args, target) | ||
args = append(args, command...) | ||
bin, err := exec.LookPath(c.Command.Name) | ||
if err != nil { | ||
Logger.Fatalf("Cannot find %q in $PATH", c.Command.Name) | ||
} | ||
|
||
Logger.Debugf("Wrapper called with bin=%v target=%v command=%v options=%v, args=%v", bin, target, command, options, args) | ||
|
||
// check if config is up-to-date | ||
conf, err := config.Open() | ||
if err != nil { | ||
Logger.Fatalf("Cannot open configuration file: %v", err) | ||
} | ||
|
||
if err = conf.LoadKnownHosts(); err != nil { | ||
Logger.Debugf("Failed to load assh known_hosts: %v", err) | ||
} | ||
|
||
// check if .ssh/config is outdated | ||
isOutdated, err := conf.IsConfigOutdated(target) | ||
if err != nil { | ||
Logger.Error(err) | ||
} | ||
if isOutdated { | ||
Logger.Debugf("The configuration file is outdated, rebuilding it before calling %s", c.Command.Name) | ||
if err = conf.SaveSshConfig(); err != nil { | ||
Logger.Error(err) | ||
} | ||
} | ||
|
||
// Execute Binary | ||
syscall.Exec(bin, args, os.Environ()) | ||
} |
Oops, something went wrong.