Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control sockets manager #152

Merged
merged 17 commits into from
Jul 8, 2016
10 changes: 6 additions & 4 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ import:
- package: golang.org/x/crypto
subpackages:
- ssh/terminal
- package: github.com/mattn/go-zglob
4 changes: 3 additions & 1 deletion pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
. "github.com/moul/advanced-ssh-config/pkg/logger"
)

func cmdBuild(c *cli.Context) {
func cmdBuild(c *cli.Context) error {
conf, err := config.Open(c.GlobalString("config"))
if err != nil {
Logger.Fatalf("Cannot open configuration file: %v", err)
}

conf.WriteSSHConfigTo(os.Stdout)

return nil
}
16 changes: 16 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ var Commands = []cli.Command{
Usage: "Search entries by given search text",
Action: cmdSearch,
},
{
Name: "cs",
Usage: "Manage control sockets",
Subcommands: []cli.Command{
{
Name: "list",
Action: cmdCsList,
Usage: "List active control sockets",
},
{
Name: "flush",
Action: cmdCsFlush,
Usage: "Close control sockets",
},
},
},
// FIXME: tree
{
Name: "wrapper",
Expand Down
79 changes: 79 additions & 0 deletions pkg/commands/control-sockets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package commands

import (
"fmt"
"os"
"time"

"github.com/codegangsta/cli"
"github.com/docker/go-units"

"github.com/moul/advanced-ssh-config/pkg/config"
"github.com/moul/advanced-ssh-config/pkg/control-sockets"
. "github.com/moul/advanced-ssh-config/pkg/logger"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not use dot imports

)

func cmdCsList(c *cli.Context) error {
conf, err := config.Open(c.GlobalString("config"))
if err != nil {
panic(err)
}

controlPath := conf.Defaults.ControlPath

activeSockets, err := controlsockets.LookupControlPathDir(controlPath)
if err != nil {
panic(err)
}

if len(activeSockets) == 0 {
fmt.Println("No active control sockets.")
return nil
}

fmt.Printf("%d active control sockets in %q:\n\n", len(activeSockets), controlPath)
now := time.Now().UTC()
for _, socket := range activeSockets {
createdAt, err := socket.CreatedAt()
if err != nil {
Logger.Warnf("%v", err)
}

fmt.Printf("- %s (%v)\n", socket.RelativePath(), units.HumanDuration(now.Sub(createdAt)))
}

return nil
}

func cmdCsFlush(c *cli.Context) error {
conf, err := config.Open(c.GlobalString("config"))
if err != nil {
panic(err)
}

controlPath := conf.Defaults.ControlPath

activeSockets, err := controlsockets.LookupControlPathDir(controlPath)
if err != nil {
panic(err)
}

if len(activeSockets) == 0 {
fmt.Println("No active control sockets.")
}

success := 0
for _, socket := range activeSockets {
if err := os.Remove(socket.Path()); err != nil {
Logger.Warnf("Failed to close control socket %q: %v", socket.Path(), err)
} else {
success++
}
}

if success > 0 {
fmt.Printf("Closed %d control sockets.", success)
}

return nil
}
7 changes: 5 additions & 2 deletions pkg/commands/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"github.com/codegangsta/cli"

"github.com/moul/advanced-ssh-config/pkg/config"
"github.com/moul/advanced-ssh-config/pkg/utils"
// . "github.com/moul/advanced-ssh-config/pkg/logger"
)

func cmdInfo(c *cli.Context) {
func cmdInfo(c *cli.Context) error {
conf, err := config.Open(c.GlobalString("config"))
if err != nil {
panic(err)
Expand All @@ -26,7 +27,7 @@ func cmdInfo(c *cli.Context) {
fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
fmt.Println("")
fmt.Printf("RC files:\n")
homeDir := config.GetHomeDir()
homeDir := utils.GetHomeDir()
for _, filename := range conf.IncludedFiles() {
relativeFilename := strings.Replace(filename, homeDir, "~", -1)
fmt.Printf("- %s\n", relativeFilename)
Expand All @@ -38,4 +39,6 @@ func cmdInfo(c *cli.Context) {
fmt.Printf("- %d included files\n", len(conf.IncludedFiles()))
// FIXME: print info about connections/running processes
// FIXME: print info about current config file version

return nil
}
4 changes: 3 additions & 1 deletion pkg/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// . "github.com/moul/advanced-ssh-config/pkg/logger"
)

func cmdList(c *cli.Context) {
func cmdList(c *cli.Context) error {
conf, err := config.Open(c.GlobalString("config"))
if err != nil {
panic(err)
Expand Down Expand Up @@ -51,4 +51,6 @@ func cmdList(c *cli.Context) {
}
fmt.Println()
}

return nil
}
4 changes: 3 additions & 1 deletion pkg/commands/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
. "github.com/moul/advanced-ssh-config/pkg/logger"
)

func cmdProxy(c *cli.Context) {
func cmdProxy(c *cli.Context) error {
Logger.Debugf("assh args: %s", c.Args())

if len(c.Args()) < 1 {
Expand Down Expand Up @@ -85,6 +85,8 @@ func cmdProxy(c *cli.Context) {
if err != nil {
Logger.Fatalf("Proxy error: %v", err)
}

return nil
}

func computeHost(dest string, portOverride int, conf *config.Config) (*config.Host, error) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/commands/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// . "github.com/moul/advanced-ssh-config/pkg/logger"
)

func cmdSearch(c *cli.Context) {
func cmdSearch(c *cli.Context) error {
conf, err := config.Open(c.GlobalString("config"))
if err != nil {
panic(err)
Expand All @@ -26,11 +26,13 @@ func cmdSearch(c *cli.Context) {

if len(found) == 0 {
fmt.Println("no results found.")
return
return nil
}

fmt.Printf("Listing results for %s:\n", needle)
for _, host := range found {
fmt.Printf(" %s -> %s\n", host.Name(), host.Prototype())
}

return nil
}
4 changes: 3 additions & 1 deletion pkg/commands/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
. "github.com/moul/advanced-ssh-config/pkg/logger"
)

func cmdWrapper(c *cli.Context) {
func cmdWrapper(c *cli.Context) error {
if len(c.Args()) < 1 {
Logger.Fatalf("Missing <target> argument. See usage with 'assh wrapper %s -h'.", c.Command.Name)
}
Expand Down Expand Up @@ -67,4 +67,6 @@ func cmdWrapper(c *cli.Context) {

// Execute Binary
syscall.Exec(bin, args, os.Environ())

return nil
}
15 changes: 8 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/moul/advanced-ssh-config/pkg/flexyaml"
. "github.com/moul/advanced-ssh-config/pkg/logger"
"github.com/moul/advanced-ssh-config/pkg/utils"
"github.com/moul/advanced-ssh-config/pkg/version"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func (c *Config) String() string {
func (c *Config) SaveNewKnownHost(target string) {
c.addKnownHost(target)

path, err := expandUser(c.ASSHKnownHostFile)
path, err := utils.ExpandUser(c.ASSHKnownHostFile)
if err != nil {
Logger.Errorf("Cannot append host %q, unknown ASSH known_hosts file: %v", target, err)
}
Expand All @@ -76,7 +77,7 @@ func (c *Config) addKnownHost(target string) {

// LoadKnownHosts loads known hosts list from disk
func (c *Config) LoadKnownHosts() error {
path, err := expandUser(c.ASSHKnownHostFile)
path, err := utils.ExpandUser(c.ASSHKnownHostFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -256,7 +257,7 @@ func (c *Config) GetHostSafe(name string) *Host {
// isSSHConfigOutdated returns true if assh.yml or an included file has a
// modification date more recent than .ssh/config
func (c *Config) isSSHConfigOutdated() (bool, error) {
filepath, err := expandUser(c.sshConfigPath)
filepath, err := utils.ExpandUser(c.sshConfigPath)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -381,7 +382,7 @@ func (c *Config) SaveSSHConfig() error {
if c.sshConfigPath == "" {
return fmt.Errorf("no Config.sshConfigPath configured")
}
filepath, err := expandUser(c.sshConfigPath)
filepath, err := utils.ExpandUser(c.sshConfigPath)
if err != nil {
return err
}
Expand All @@ -399,7 +400,7 @@ func (c *Config) LoadFile(filename string) error {
beforeHostsCount := len(c.Hosts)

// Resolve '~' and '$HOME'
filepath, err := expandUser(filename)
filepath, err := utils.ExpandUser(filename)
if err != nil {
return err
}
Expand Down Expand Up @@ -443,7 +444,7 @@ func (c *Config) LoadFile(filename string) error {
// LoadFiles will try to glob the pattern and load each maching entries
func (c *Config) LoadFiles(pattern string) error {
// Resolve '~' and '$HOME'
expandedPattern, err := expandUser(pattern)
expandedPattern, err := utils.ExpandUser(pattern)
if err != nil {
return err
}
Expand All @@ -462,7 +463,7 @@ func (c *Config) LoadFiles(pattern string) error {
}

if c.ASSHBinaryPath != "" {
path, err := expandUser(c.ASSHBinaryPath)
path, err := utils.ExpandUser(c.ASSHBinaryPath)
if err != nil {
return err
}
Expand Down
Loading