Skip to content

Commit

Permalink
refactor file printing
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Mar 30, 2023
1 parent c956048 commit c212184
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
6 changes: 2 additions & 4 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ var addCmd = &cobra.Command{
}

if dryRun {
cmd.Print(helpers.Header(fmt.Sprintf("%s:", hostsFilePath), ""))
cmd.Print(hosts)
cmd.Print(helpers.PrintFile(hostsFilePath, hosts))
}

sshConfigPath, _ := cmd.PersistentFlags().GetString("ssh-config")
Expand Down Expand Up @@ -102,8 +101,7 @@ var addCmd = &cobra.Command{
}

if dryRun {
cmd.Print(helpers.Header(fmt.Sprintf("%s:", sshConfigPath), "\n--\n"))
cmd.Print(sshConfig)
cmd.Print(helpers.PrintFileWithSpacer(sshConfigPath, sshConfig))
}
},
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func Print(cmd *cobra.Command, args []string) {
os.Exit(1)
}

cmd.Print(helpers.Header(fmt.Sprintf("%s:", hostsFilePath), ""))
cmd.Print(hosts)
cmd.Print(helpers.PrintFile(hostsFilePath, hosts))

sshConfigPath, _ := cmd.Flags().GetString("ssh-config")
if sshConfigPath == "" {
Expand All @@ -82,6 +81,5 @@ func Print(cmd *cobra.Command, args []string) {
os.Exit(1)
}

cmd.Print(helpers.Header(fmt.Sprintf("%s:", sshConfigPath), "\n--\n"))
cmd.Print(sshConfig)
cmd.Print(helpers.PrintFileWithSpacer(hostsFilePath, sshConfig))
}
6 changes: 2 additions & 4 deletions cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ var rmCmd = &cobra.Command{
}

if dryRun {
cmd.Print(helpers.Header(fmt.Sprintf("%s:", hostsFilePath), ""))
cmd.Print(hosts)
cmd.Print(helpers.PrintFile(hostsFilePath, hosts))
}

sshConfigPath, _ := cmd.Flags().GetString("ssh-config")
Expand All @@ -95,8 +94,7 @@ var rmCmd = &cobra.Command{
}

if dryRun {
cmd.Print(helpers.Header(fmt.Sprintf("%s:", sshConfigPath), "\n--\n"))
cmd.Print(sshConfig)
cmd.Print(helpers.PrintFileWithSpacer(sshConfigPath, sshConfig))
}
},
}
Expand Down
16 changes: 14 additions & 2 deletions internal/helpers/print.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package helpers

func Header(heading string, spacer string) string {
return spacer + heading + "\n"
import "fmt"

func PrintFile(filename string, file interface{}) (output string) {
output = filename + "\n"
output = output + fmt.Sprint(file)

return
}

func PrintFileWithSpacer(filename string, file interface{}) (output string) {
output = "\n--\n" + filename + ":\n"
output = output + fmt.Sprint(file)

return
}

0 comments on commit c212184

Please sign in to comment.