Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
feat(cmd): moving target to arg instead of separate flag
Browse files Browse the repository at this point in the history
Signed-off-by: danmx <[email protected]>
  • Loading branch information
danmx committed Aug 27, 2020
1 parent ac86e48 commit cfc7ce2
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<a name="unreleased"></a>
## [Unreleased]

### Feat
- **cmd:** moving target to arg instead of separate flag

### Fix
- **aws:** error log on a send ssh pub key failure
- **docs:** adding profile config entry description
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ docker run --rm -it -v "${HOME}"/.sigil:/home/nonroot/.sigil -v "${HOME}"/.aws:/
Binary:

```shell
sigil -r eu-west-1 session --type instance-id --target i-xxxxxxxxxxxxxxxxx
sigil -r eu-west-1 session --type instance-id i-xxxxxxxxxxxxxxxxx
```

Using with [aws-vault](https://github.com/99designs/aws-vault):

```shell
aws-vault exec AWS_PROFILE -- sigil -r eu-west-1 session --type instance-id --target i-xxxxxxxxxxxxxxxxx
aws-vault exec AWS_PROFILE -- sigil -r eu-west-1 session --type instance-id i-xxxxxxxxxxxxxxxxx
```

### SSH integration
Expand All @@ -86,11 +86,11 @@ Add an entry to your `ssh_config`:
Host i-* mi-*
IdentityFile /tmp/sigil/%h/temp_key
IdentitiesOnly yes
ProxyCommand sigil ssh --target %h --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/
ProxyCommand sigil ssh --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/ %h
Host *.compute.internal
IdentityFile /tmp/sigil/%h/temp_key
IdentitiesOnly yes
ProxyCommand sigil ssh --type private-dns --target %h --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/
ProxyCommand sigil ssh --type private-dns --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/ %h
```

and run:
Expand Down
5 changes: 3 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ var (
}
// listCmd represents the list command
listCmd = &cobra.Command{
Use: "list",
Short: "List available EC2 instances or SSM sessions",
Use: "list [--type TYPE] ... { [--instance-ids IDs] [--instance-tags TAGS] | [--session-filters FILTERS] }",
DisableFlagsInUseLine: true,
Short: "List available EC2 instances or SSM sessions",
Long: `Show list of all EC2 instances with AWS SSM Agent running or active SSM sessions.
Supported groups of filters:
Expand Down
23 changes: 18 additions & 5 deletions cmd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import (

// sessionCmd represents the session command
var sessionCmd = &cobra.Command{
Use: "session",
Short: "Start a session",
Long: `Start a new session in chosen EC2 instance.`,
Aliases: []string{"sess", "s"},
Example: fmt.Sprintf("%s session --type instance-id --target i-xxxxxxxxxxxxxxxxx", appName),
Use: "session [--type TYPE] ... TARGET",
DisableFlagsInUseLine: true,
Short: "Start a session",
Long: `Start a new session in chosen EC2 instance.`,
Aliases: []string{"sess", "s"},
Example: fmt.Sprintf("%s session --type instance-id i-xxxxxxxxxxxxxxxxx", appName),
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 && cmd.Flags().Lookup("target").Changed {
return fmt.Errorf("can't use both target argument (%s) and deprecated flag (%s)", args[0], cmd.Flags().Lookup("target").Value.String())
}
// Config bindings
for _, flag := range []string{"target", "type"} {
if err := cfg.BindPFlag(flag, cmd.Flags().Lookup(flag)); err != nil {
Expand All @@ -27,6 +32,9 @@ var sessionCmd = &cobra.Command{
return err
}
}
if len(args) > 0 {
cfg.Set("target", args[0])
}
// returns err
return aws.VerifyDependencies()
},
Expand Down Expand Up @@ -63,5 +71,10 @@ func init() {
rootCmd.AddCommand(sessionCmd)

sessionCmd.Flags().String("target", "", "specify the target depending on the type")
// Deprecating the target flag
err := sessionCmd.Flags().MarkDeprecated("target", "this flag will be deprecated in future releases, use args instead")
if err != nil {
log.WithField("flag", sessionCmd.Flags().Lookup("target")).Error(err)
}
sessionCmd.Flags().String("type", aws.TargetTypeInstanceID, fmt.Sprintf("specify target type: %s/%s/%s", aws.TargetTypeInstanceID, aws.TargetTypePrivateDNS, aws.TargetTypeName))
}
19 changes: 16 additions & 3 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ const tempKeyName = "temp_key"
var (
portNum uint64 = 22
sshCmd = &cobra.Command{
Use: "ssh",
Short: "Start ssh session",
Long: `Start a new ssh for chosen EC2 instance.`,
Use: "ssh [--type TYPE] ... { [--gen-key-pair] [--gen-key-dir DIR] | [--pub-key PUB_KEY_PATH] } TARGET",
DisableFlagsInUseLine: true,
Short: "Start ssh session",
Long: `Start a new ssh for chosen EC2 instance.`,
Args: cobra.MaximumNArgs(1),
//nolint:dupl // deduplicating it wouldn't provide much value
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 && cmd.Flags().Lookup("target").Changed {
return fmt.Errorf("can't use both target argument (%s) and deprecated flag (%s)", args[0], cmd.Flags().Lookup("target").Value.String())
}
// Config bindings
for flag, lookup := range map[string]string{
"target": "target",
Expand All @@ -39,6 +44,9 @@ var (
return err
}
}
if len(args) > 0 {
cfg.Set("target", args[0])
}
// returns err
return aws.VerifyDependencies()
},
Expand Down Expand Up @@ -105,6 +113,11 @@ func init() {
rootCmd.AddCommand(sshCmd)

sshCmd.Flags().String("target", "", "specify the target depending on the type")
// Deprecating the target flag
err := sshCmd.Flags().MarkDeprecated("target", "this flag will be deprecated in future releases, use args instead")
if err != nil {
log.WithField("flag", sshCmd.Flags().Lookup("target")).Error(err)
}
sshCmd.Flags().String("type", aws.TargetTypeInstanceID, fmt.Sprintf("specify target type: %s/%s/%s", aws.TargetTypeInstanceID, aws.TargetTypePrivateDNS, aws.TargetTypeName))
sshCmd.Flags().Bool("gen-key-pair", false, fmt.Sprintf("generate a temporary key pair that will be send and used. By default use %s as an identity file", path.Join(workDir, tempKeyName)))
sshCmd.Flags().String("gen-key-dir", workDir, "the directory where temporary keys will be generated")
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/00-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This command list all active instances and sessions that match the filter.
When interactive mode is enabled you can start a session in a given instance or terminate active session.

```console
sigil list [flags]
sigil list [--type TYPE] ... { [--instance-ids IDs] [--instance-tags TAGS] | [--session-filters FILTERS] }
```

[Man](../man/sigil_list.md) page
Expand Down
4 changes: 2 additions & 2 deletions docs/usage/10-session.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Start a new session in chosen EC2 instance based on its instance ID, name tag, or private DNS name.

```console
sigil session [flags]
sigil session [--type TYPE] ... TARGET
```

[Man](../man/sigil_session.md) page
Expand All @@ -23,7 +23,7 @@ Config file settings that affect the command
## Examples

```console
$ sigil -r eu-west-1 session --type instance-id --target i-xxxxxxxxxxxxxxxxx
$ sigil -r eu-west-1 session --type instance-id i-xxxxxxxxxxxxxxxxx
Starting session with SessionId: example
sh-4.2$
```
6 changes: 3 additions & 3 deletions docs/usage/20-ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Start a new ssh for chosen EC2 instance based on its instance ID, name tag, or private DNS name.

```console
sigil ssh [flags]
sigil ssh [--type TYPE] ... { [--gen-key-pair] [--gen-key-dir DIR] | [--pub-key PUB_KEY_PATH] } TARGET
```

[Man](../man/sigil_ssh.md) page
Expand Down Expand Up @@ -31,11 +31,11 @@ Config file settings that affect the command
Host i-* mi-*
IdentityFile /tmp/sigil/%h/temp_key
IdentitiesOnly yes
ProxyCommand sigil ssh --target %h --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/
ProxyCommand sigil ssh --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/ %h
Host *.compute.internal
IdentityFile /tmp/sigil/%h/temp_key
IdentitiesOnly yes
ProxyCommand sigil ssh --type private-dns --target %h --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/
ProxyCommand sigil ssh --type private-dns --port %p --pub-key /tmp/sigil/%h/temp_key.pub --gen-key-pair --os-user %r --gen-key-dir /tmp/sigil/%h/ %h
```

```console
Expand Down

0 comments on commit cfc7ce2

Please sign in to comment.