Documentation ¶
Overview ¶
Package cmd provides command-line interfaces for interacting with SSH configurations.
Package cmd provides command-line interfaces for interacting with SSH configurations.
Package cmd provides command-line interfaces for interacting with SSH configurations.
Package cmd provides command-line interfaces for interacting with SSH configurations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AddCmd = &cobra.Command{
Use: "add",
Short: "Add a new SSH configuration or keys",
}
AddCmd represents the Cobra command for adding a new SSH configuration or keys.
var EditCmd = &cobra.Command{ Use: "edit [config|keys|hosts]", Short: "Edits SSH config, authorized_keys, or known_hosts file", Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { editor := getenvFunc("EDITOR") if editor == "" { editor = "vim" } configPath := utils.ExpandUser(utils.SshConfigPath) if len(args) > 0 { switch args[0] { case "keys": configPath = utils.ExpandUser("~/.ssh/authorized_keys") case "hosts": configPath = utils.ExpandUser("~/.ssh/known_hosts") case "config": default: cmd.Println("Invalid argument. Use 'config' or 'keys'.") return } } command := execCommand(editor, configPath) command.Stdin = os.Stdin command.Stdout = cmd.OutOrStdout() command.Stderr = os.Stderr err := command.Run() if err != nil { fmt.Println("Error:", err) } }, }
var ListCmd = &cobra.Command{ Use: "list [config|keys|github|gitlab] [username]", Short: "List SSH configurations or fetch SSH keys from GitHub/GitLab", Run: func(cmd *cobra.Command, args []string) { if len(args) == 2 { platform := args[0] username := args[1] url := "" switch platform { case "github": url = "https://github.com/" + username + ".keys" case "gitlab": url = "https://gitlab.com/" + username + ".keys" default: fmt.Println("Unknown platform. Use 'github' or 'gitlab'.") return } resp, err := http.Get(url) if err != nil { fmt.Println("Error fetching keys:", err) return } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { body, _ := io.ReadAll(resp.Body) fmt.Println(string(body)) } else { fmt.Println("Error fetching keys. HTTP status:", resp.Status) } } else if len(args) == 1 { switch args[0] { case "keys": keysPath := utils.ExpandUser("~/.ssh/authorized_keys") content, err := os.ReadFile(keysPath) if err != nil { fmt.Println("Error:", err) return } fmt.Println(string(content)) case "config": configPath := utils.ExpandUser(utils.SshConfigPath) content, err := os.ReadFile(configPath) if err != nil { fmt.Println("Error:", err) return } fmt.Fprintln(cmd.OutOrStdout(), string(content)) default: fmt.Println("Invalid argument. Use 'config', 'keys', 'github [username]' or 'gitlab [username]'.") } } else { fmt.Println("Invalid number of arguments. Use 'ssh-config list --help' for usage information.") } }, }
ListCmd represents the Cobra command for listing SSH configuration of ~/.ssh/config or the public keys on gitlab.com and github.com for a specific user.
var RemoveCmd = &cobra.Command{ Use: "remove [name]", Short: "Remove a SSH host from the SSH configuration file", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { name := args[0] configPath := utils.ExpandUser(utils.SshConfigPath) content, err := os.ReadFile(configPath) if err != nil { fmt.Println("Error:", err) return } lines := strings.Split(string(content), "\n") newContent := "" skip := false for _, line := range lines { if strings.HasPrefix(line, "Host "+name) { skip = true } else if skip && strings.HasPrefix(line, " ") { continue } else { skip = false newContent += line + "\n" } } err = os.WriteFile(configPath, []byte(newContent), 0644) if err != nil { fmt.Println("Error:", err) } }, }
RemoveCmd represents the Cobra command for removing a host from the SSH configuration file ~/.ssh/config.
Functions ¶
This section is empty.