Skip to content

Commit

Permalink
Add JSON output to sshc get
Browse files Browse the repository at this point in the history
  • Loading branch information
delucks committed Dec 16, 2018
1 parent c393118 commit 4d8a17c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ Host myremotehost
IdentityFile %d/.ssh/rsa_key
```

You can also optionally convert the host definition to JSON, with the following result:

```
$ sshc get -j myremotehost
{
"HostName" "127.0.0.1",
"Port" "22",
"User" "delucks",
"IdentityFile" "%d/.ssh/rsa_key"
}
```

This is useful for pulling out specific pieces of information using a json query tool like `jq`. For example, `sshc get -j myremotehost | jq .HostName` is a quick way of returning just the hostname field of an entry in your config file. I use this for constructing URLs on remote servers like `curl -s "http://$(sshc get -j myremotehost | jq .HostName):8080/path/index.html"`.

You can find the names of all the host definitions in your file by using `sshc hosts`:

```
Expand Down Expand Up @@ -91,15 +105,3 @@ From **ssh-config**: I like this tool a lot and started off using it, but the la
`sshc` is also novel in a couple ways:
- Copying definitions from the local host to a remote `~/.ssh/config`
- Choice of golang; small footprint & installation from a single binary

The rest of this README is scratch space left over from planning.

```
$ sshc get -j hostname
{"hostname": {
"HostName": "127.0.0.1",
"Port": "22",
"User": "delucks",
"IdentityFile": "%d/.ssh/rsa_key",
}}
```
25 changes: 23 additions & 2 deletions sshc.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package main

import (
"encoding/json"
"fmt"
"github.com/kevinburke/ssh_config"
"github.com/spf13/cobra"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/kevinburke/ssh_config"
"github.com/spf13/cobra"
)

// Variables holding data for Cobra flags used in the command-line interface
var (
UserConfigFile string
CopyDestinationPath string
JSONOutput bool
UseANSIColor bool
RegexIgnoreCase bool
)
Expand Down Expand Up @@ -269,6 +272,23 @@ func RunGet(hostname string) error {
if hostdef == nil {
return ColorError("No matching hosts", Red)
}
if JSONOutput {
output := map[string]interface{}{}
for _, n := range hostdef.Nodes {
trimmed := strings.TrimLeft(n.String(), " \t")
if trimmed == "" {
continue
}
spl := strings.SplitN(trimmed, " ", 2)
output[spl[0]] = spl[1]
}
blob, err := json.MarshalIndent(output, "", " ")
if err != nil {
return err
}
fmt.Printf("%s\n", blob)
return nil
}
fmt.Println(hostdef)
return nil
}
Expand All @@ -293,6 +313,7 @@ func NewGetCommand() *cobra.Command {
}
},
}
get.Flags().BoolVarP(&JSONOutput, "json-output", "j", false, "Output this host in JSON format")
return get
}

Expand Down

0 comments on commit 4d8a17c

Please sign in to comment.