mugit/internal/cli/ssh_shell.go (view raw)
| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/urfave/cli/v3" |
| 10 | ) |
| 11 | |
| 12 | var errSSHDisabled = errors.New("ssh is disabled") |
| 13 | |
| 14 | func (c *Cli) sshShellAction(ctx context.Context, cmd *cli.Command) error { |
| 15 | if !c.cfg.SSH.Enable { |
| 16 | return errSSHDisabled |
| 17 | } |
| 18 | |
| 19 | sshCommand := os.Getenv("SSH_ORIGINAL_COMMAND") |
| 20 | return c.ssh.HandleCommand(ctx, sshCommand, os.Stdin, os.Stdout, os.Stderr) |
| 21 | } |
| 22 | |
| 23 | func (c *Cli) sshAuthorizedKeysAction(ctx context.Context, cmd *cli.Command) error { |
| 24 | if !c.cfg.SSH.Enable { |
| 25 | return errSSHDisabled |
| 26 | } |
| 27 | |
| 28 | fingerprint := cmd.Args().First() |
| 29 | if fingerprint == "" { |
| 30 | return fmt.Errorf("fingerprint is required") |
| 31 | } |
| 32 | |
| 33 | executablePath, err := os.Executable() |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | out := c.ssh.AuthorizedKeys(executablePath) |
| 39 | fmt.Fprint(os.Stdout, out) |
| 40 | |
| 41 | return nil |
| 42 | } |