Skip to content

Commit

Permalink
rm edit mode from rm command
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Mar 31, 2023
1 parent 904de5e commit 6c5b986
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 98 deletions.
68 changes: 33 additions & 35 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,45 @@ var editCmd = &cobra.Command{
return comps, cobra.ShellCompDirectiveNoFileComp
},
Args: cobra.MaximumNArgs(1),
Run: Edit,
}
Run: func(cmd *cobra.Command, args []string) {
editor := os.Getenv("EDITOR")
if len(args) == 1 {

func init() {
rootCmd.AddCommand(editCmd)
}
if _, err := exec.LookPath(args[0]); err != nil {
cmd.Printf("Executable '%s' not found in $PATH. Try nano or vi!\n", args[0])

os.Exit(1)
}

func Edit(cmd *cobra.Command, args []string) {
editor := os.Getenv("EDITOR")
if len(args) == 1 {
editor = args[0]
} else if editor == "" {
editor = "vi"
}

if _, err := exec.LookPath(args[0]); err != nil {
cmd.Printf("Executable '%s' not found in $PATH. Try nano or vi!\n", args[0])
err := getFilePaths()
if err != nil {
cmd.Printf("Error retrieving file paths: %v", err)

os.Exit(1)
}

editor = args[0]
} else if editor == "" {
editor = "vi"
}
if etcHosts {
vi := exec.Command(editor, hostsFilePath)
vi.Stdin = os.Stdin
vi.Stdout = os.Stdout
if err := vi.Start(); err != nil {
cmd.Printf("Error opening file with vi: %v", err)

err := getFilePaths()
if err != nil {
cmd.Printf("Error retrieving file paths: %v", err)
os.Exit(1)
}
if err := vi.Wait(); err != nil {
cmd.Printf("Unexpected error occurred: %v", err)

os.Exit(1)
}
os.Exit(1)
}
}

if etcHosts {
vi := exec.Command(editor, hostsFilePath)
vi := exec.Command(editor, sshConfigFilePath)
vi.Stdin = os.Stdin
vi.Stdout = os.Stdout
if err := vi.Start(); err != nil {
Expand All @@ -90,19 +98,9 @@ func Edit(cmd *cobra.Command, args []string) {

os.Exit(1)
}
}

vi := exec.Command(editor, sshConfigFilePath)
vi.Stdin = os.Stdin
vi.Stdout = os.Stdout
if err := vi.Start(); err != nil {
cmd.Printf("Error opening file with vi: %v", err)

os.Exit(1)
}
if err := vi.Wait(); err != nil {
cmd.Printf("Unexpected error occurred: %v", err)
},
}

os.Exit(1)
}
func init() {
rootCmd.AddCommand(editCmd)
}
111 changes: 48 additions & 63 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ import (
)

var (
rmCmdMinimumNArgs int
editMode bool
interactiveMode bool
interactiveMode bool
)

// TODO add edit mode like --edit and open file in vim using exec pkg
// TODO add interactive mode (using survey lib) if no args provided
// rmCmd represents the rm command
var rmCmd = &cobra.Command{
Use: "rm HOST...",
Use: "rm [HOST...]",
Short: "Remove one or more host entries from ssh-config and hosts file",
Long: `Remove one or more host entries from ssh-config and hosts file. Gonna keep those files clean!`,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var comps []string
if len(args) >= 0 {
comps = cobra.AppendActiveHelp(comps, "Expecting one or more host names")
if len(args) == 0 {
comps = cobra.AppendActiveHelp(comps, "Hit enter for interactive mode or provide one or more host names")
// comps = cobra.AppendActiveHelp(comps, "Hit enter for interactive removal or specify one or more host names here")
}
if len(args) > 0 {
comps = cobra.AppendActiveHelp(comps, "Provide more host names or hit enter")
}
return comps, cobra.ShellCompDirectiveNoFileComp
},
Args: cobra.MinimumNArgs(rmCmdMinimumNArgs), // TODO remove when interactive mode implemented
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
err := getFilePaths()
if err != nil {
Expand All @@ -60,83 +60,68 @@ var rmCmd = &cobra.Command{
os.Exit(1)
}

mode := ""
if editMode {
mode = "EDIT"
} else if interactiveMode {
mode = "INTERACTIVE"
}

switch mode {
case "INTERACTIVE": // TODO tbimplemented
case "EDIT":
Edit(cmd, args)
if interactiveMode {

default:
if etcHosts {
if hostsFilePath == "" {
hostsFilePath = "/etc/hosts"
}
hosts, err := files.GetHosts(hostsFilePath)
if err != nil {
cmd.Printf("Error reading file: %v", err)
return
}

os.Exit(1)
}
if etcHosts {
if hostsFilePath == "" {
hostsFilePath = "/etc/hosts"
}
hosts, err := files.GetHosts(hostsFilePath)
if err != nil {
cmd.Printf("Error reading file: %v", err)

hosts.RemoveHosts(args)
os.Exit(1)
}

if !dryRun {
if err := hosts.Write(); err != nil {
cmd.Printf("Error writing file %s: %v", hostsFilePath, err)
hosts.RemoveHosts(args)

os.Exit(1)
}
}
if !dryRun {
if err := hosts.Write(); err != nil {
cmd.Printf("Error writing file %s: %v", hostsFilePath, err)

if dryRun {
cmd.Print(helpers.PrintFileWithSpacer(hostsFilePath, hosts))
os.Exit(1)
}
}

if sshConfigFilePath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
cmd.Printf("Error retrieving user's home directory: %v", err)

os.Exit(1)
}
sshConfigFilePath = fmt.Sprintf("%s/.ssh/config", homeDir)
if dryRun {
cmd.Print(helpers.PrintFileWithSpacer(hostsFilePath, hosts))
}
sshConfig, err := files.GetSSHConfig(sshConfigFilePath)
}

if sshConfigFilePath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
cmd.Printf("Error reading file: %v", err)
cmd.Printf("Error retrieving user's home directory: %v", err)

os.Exit(1)
}
sshConfigFilePath = fmt.Sprintf("%s/.ssh/config", homeDir)
}
sshConfig, err := files.GetSSHConfig(sshConfigFilePath)
if err != nil {
cmd.Printf("Error reading file: %v", err)

sshConfig.RemoveHosts(args)
os.Exit(1)
}

if !dryRun {
sshConfig.Write()
}
sshConfig.RemoveHosts(args)

if dryRun {
cmd.Print(helpers.PrintFile(sshConfigFilePath, sshConfig))
}
if !dryRun {
sshConfig.Write()
}

if dryRun {
cmd.Print(helpers.PrintFile(sshConfigFilePath, sshConfig))
}
},
}

func init() {
rootCmd.AddCommand(rmCmd)

flags := rmCmd.Flags()

flags.BoolVarP(&editMode, "edit", "e", false, "Use editor for removal; default: $EDITOR; fallback: vi")

rmCmdMinimumNArgs = 1 // TODO test whether this works with live build
if editMode || interactiveMode {
rmCmdMinimumNArgs = 0
}
// flags := rmCmd.Flags()
// flags.BoolVarP(&interactiveMode, "interactive", "i", false, "Interactively select host entries to remove")
}

0 comments on commit 6c5b986

Please sign in to comment.