Skip to content

feat: Refactor CLI config-ssh to improve UX #1900

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

Merged
merged 38 commits into from
Jun 8, 2022

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented May 30, 2022

This PR improves the UX of coder config-ssh with multiple QoL improvements. Replaces #1848.

  • Magic block is replaced by Include statement
  • Writes are only done on changes
  • Inform user of changes via prompt
  • Allow displaying changes via --diff
  • Remove magic block if present
  • Safer config writing via tmp-file + rename
  • Parse previous config-ssh options, compare to new options and ask to use new (otherwise old ones are used)
  • Tests for the new functionality

Fixes #1326

TODO:

  • Auto-update ~/.ssh/coder on coder create and coder delete
    • This will be be left to a follow-up PR, or scrapped in unwanted

Examples:

New config:

❯ coder config-ssh
> The following changes will be made to your SSH configuration:

    * Remove old config block (START-CODER/END-CODER) from /home/maf/.ssh/config
    * Add "Include coder" to /home/maf/.ssh/config
    * Write auto-generated coder config file to /home/maf/.ssh/coder

  To see changes, run diff:

    $ coder config-ssh --diff

  Continue? (yes/no) yes

You should now be able to ssh into your workspace.
For example, try running:

	$ ssh coder.work

Options changed:

❯ coder config-ssh -o ForwardAgent=yes
> New options differ from previous options:

  New options:
    * ssh-option: ForwardAgent=yes

  Previous options: none

  Use new options? (yes/no) yes

> The following changes will be made to your SSH configuration:

    * Update auto-generated coder config file in /home/maf/.ssh/coder

  To see changes, run diff:

    $ coder config-ssh --ssh-option "ForwardAgent=yes" --diff

  Continue? (yes/no) yes

You should now be able to ssh into your workspace.
For example, try running:

	$ ssh coder.work

Options would change => select no (no changes):

❯ coder config-ssh -o ForwardAgent=yes -o IdentityAgent=none
> New options differ from previous options:

  New options:
    * ssh-option: ForwardAgent=yes
    * ssh-option: IdentityAgent=none

  Previous options:
    * ssh-option: ForwardAgent=yes

  Use new options? (yes/no) no

You should now be able to ssh into your workspace.
For example, try running:

	$ ssh coder.work

Diff:

❯ coder config-ssh --diff
Changes:

  * Remove old config block (START-CODER/END-CODER) from /home/maf/.ssh/config
  * Add "Include coder" to /home/maf/.ssh/config
  * Write auto-generated coder config file to /home/maf/.ssh/coder

--- /home/maf/.ssh/config
+++ /home/maf/.ssh/config.new
@@ -1,23 +1,6 @@
+Include coder
+
 Host github.com
 	HostName github.com
 	User git

-# ------------START-CODER-----------
-# This was generated by "coder config-ssh".
-#
-# To remove this blob, run:
-#
-#    coder config-ssh --remove
-#
-# You should not hand-edit this section, unless you are deleting it.
-
-Host coder.work
-	ForwardAgent=yes
-	HostName coder.work
-	ConnectTimeout=0
-	StrictHostKeyChecking=no
-	UserKnownHostsFile=/dev/null
-	LogLevel ERROR
-	ProxyCommand "coder" --global-config "/Users/maf/Library/Application Support/coderv2" ssh --stdio work
-
-# ------------END-CODER------------

--- /home/maf/.ssh/coder
+++ /home/maf/.ssh/coder.new
@@ -0,0 +1,14 @@
+# This file is managed by coder. DO NOT EDIT.
+#
+# You should not hand-edit this file, all changes will be lost upon workspace
+# creation, deletion or when running "coder config-ssh".
+#
+# Last config-ssh options:
+#
+Host coder.work
+	HostName coder.work
+	ConnectTimeout=0
+	StrictHostKeyChecking=no
+	UserKnownHostsFile=/dev/null
+	LogLevel ERROR
+	ProxyCommand "coder" --global-config "/home/maf/.config/coderv2" ssh --stdio work
exit status 1

@mafredri
Copy link
Member Author

Just noticed we need to place the Include directive before the first Host entry, otherwise it will be interpreted as part of one and included conditionally.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

I like the approach so far! The --diff option is really cool!

Some observations:

  • We should keep the configs under ~/.ssh because that's where they always go.
  • We should check that we "own" ~/.ssh/coder (maybe by checking for a magic string) before writing it

@mafredri
Copy link
Member Author

mafredri commented May 31, 2022

@johnstcn great observations, I've made the necessary changes.

I've now also implemented options parsing, so coder config-ssh can suggest to re-use previous options, whereas coder config-ssh -o MyOption=yes assumes the user knows what they're doing. As a result, the full diff command is output in the prompt. The two different scenarios look like this:

cli/configssh.go Outdated
#
# To remove this blob, run:
# You should not hand-edit this file, all changes will be lost upon workspace
# creation, deletion or when running "coder config-ssh".`
Copy link
Member Author

@mafredri mafredri May 31, 2022

Choose a reason for hiding this comment

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

I really would love to see this feature. (i.e. running coder create will automatically update the config). But I suggest we rewrite this comment to not include it, open up a ticket for this functionality, and leave it for a follow-up PR. (PS. This is one reason we write the used options to the config file.)

Copy link
Member

Choose a reason for hiding this comment

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

TODO: rewrite comment

@mafredri mafredri marked this pull request as ready for review June 1, 2022 16:59
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

I haven't tested this locally, but looks good to me. I appreciate the thorough tests!

I do think there's still scope for refactoring; a lot of the code related to reading and writing SSH configs seems like it could be ripped out to its own module. This should probably done in a separate PR however!

cli/configssh.go Outdated
#
# To remove this blob, run:
# You should not hand-edit this file, all changes will be lost upon workspace
# creation, deletion or when running "coder config-ssh".`
Copy link
Member

Choose a reason for hiding this comment

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

TODO: rewrite comment

@mafredri mafredri self-assigned this Jun 2, 2022
@mafredri mafredri force-pushed the mafredri/ssh-config-refactor-2 branch from af23128 to 142683b Compare June 6, 2022 16:14
@mafredri mafredri force-pushed the mafredri/ssh-config-refactor-2 branch from 142683b to e084a73 Compare June 8, 2022 08:06
@mafredri mafredri force-pushed the mafredri/ssh-config-refactor-2 branch from e084a73 to f1be4c6 Compare June 8, 2022 08:07
@mafredri mafredri merged commit b65259f into main Jun 8, 2022
@mafredri mafredri deleted the mafredri/ssh-config-refactor-2 branch June 8, 2022 08:45
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
- Magic block is replaced by Include statement
- Writes are only done on changes
- Inform user of changes via prompt
- Allow displaying changes via `--diff`
- Remove magic block if present
- Safer config writing via tmp-file + rename
- Parse previous `config-ssh` options, compare to new options and ask to use new (otherwise old ones are used)
- Tests the new functionality

Fixes #1326
mafredri added a commit that referenced this pull request Jun 15, 2022
This commit partially reverts #1900 and removes the separate
`~/.ssh/coder` config file whilst keeping most other features.

This will allow us to remain more compatible with different IDEs.

Fixes #2317
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modify user SSH config with single Include statement instead of managed block
2 participants