Skip to content

Commit

Permalink
adds several features and refactor:
Browse files Browse the repository at this point in the history
* add status command
* add --quiet persistent flag to silence
* refactor one command or subcommand per files
* add --only to enable command

Closes #30
Closes #24
  • Loading branch information
guumaster committed Apr 8, 2020
1 parent fe76994 commit 86d5a69
Show file tree
Hide file tree
Showing 21 changed files with 520 additions and 303 deletions.
70 changes: 12 additions & 58 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ var addFromFileCmd = &cobra.Command{
Long: `
Reads from a file and set content to a profile in your hosts file.
If the profile already exists it will be added to it.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
return host.MissingProfileError
}

if profile == "default" {
return host.DefaultProfileError
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
from, _ := cmd.Flags().GetString("from")
Expand All @@ -42,64 +54,6 @@ If the profile already exists it will be added to it.`,
return err
}

return host.ListProfiles(src, &host.ListOptions{
Profile: profile,
})
},
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
return host.MissingProfileError
}

if profile == "default" {
return host.DefaultProfileError
}
return nil
},
}

// addDomainsCmd represents the fromFile command
var addDomainsCmd = &cobra.Command{
Use: "domains",
Short: "Add content in your hosts file.",
Long: `
Set content in your hosts file.
If the profile already exists it will be added to it.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
return host.MissingProfileError
}

if profile == "default" {
return host.DefaultProfileError
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
ip, _ := cmd.Flags().GetString("ip")
profile, _ := cmd.Flags().GetString("profile")

err := host.AddFromArgs(&host.AddFromArgsOptions{
Domains: args,
IP: ip,
Dst: src,
Profile: profile,
Reset: false,
})
if err != nil {
return err
}

err = host.Enable(src, profile)
if err != nil {
return err
}

return host.ListProfiles(src, &host.ListOptions{
Profile: profile,
})
Expand Down
58 changes: 58 additions & 0 deletions cmd/add_domains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
)

// addDomainsCmd represents the fromFile command
var addDomainsCmd = &cobra.Command{
Use: "domains",
Short: "Add content in your hosts file.",
Long: `
Set content in your hosts file.
If the profile already exists it will be added to it.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
return host.MissingProfileError
}

if profile == "default" {
return host.DefaultProfileError
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
ip, _ := cmd.Flags().GetString("ip")
profile, _ := cmd.Flags().GetString("profile")
quiet, _ := cmd.Flags().GetBool("quiet")

err := host.AddFromArgs(&host.AddFromArgsOptions{
Domains: args,
IP: ip,
Dst: src,
Profile: profile,
Reset: false,
})
if err != nil {
return err
}

err = host.Enable(src, profile)
if err != nil {
return err
}

if quiet {
return nil
}

return host.ListProfiles(src, &host.ListOptions{
Profile: profile,
})
},
}
12 changes: 7 additions & 5 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var backupCmd = &cobra.Command{
Use: "backup",
Short: "Creates a backup copy of your hosts file",
Long: `
Creates a backup copy of your hosts file with the date in .YYYYMMDD
as extension.
Creates a backup copy of your hosts file with the date in .YYYYMMDD
as extension.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
Expand All @@ -30,17 +30,19 @@ as extension.
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
dst, _ := cmd.Flags().GetString("path")
quiet, _ := cmd.Flags().GetBool("quiet")

backupFile, err := host.BackupFile(src, dst)
if err != nil {
return err
}

_ = host.ListProfiles(backupFile, &host.ListOptions{})

if quiet {
return nil
}
fmt.Printf("Backup completed.")

return nil
return host.ListProfiles(backupFile, &host.ListOptions{})
},
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ It will be listed as "off" while it is disabled.
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
profile, _ := cmd.Flags().GetString("profile")
quiet, _ := cmd.Flags().GetBool("quiet")

all, _ := cmd.Flags().GetBool("all")

Expand All @@ -42,6 +43,9 @@ It will be listed as "off" while it is disabled.
return err
}

if quiet {
return nil
}
return host.ListProfiles(src, &host.ListOptions{
Profile: profile,
})
Expand Down
13 changes: 12 additions & 1 deletion cmd/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ It will be listed as "on" while it is enabled.
}

src, _ := cmd.Flags().GetString("host-file")
enableOnly, _ := cmd.Flags().GetBool("only")
quiet, _ := cmd.Flags().GetBool("quiet")

err := host.Enable(src, profile)
var err error
if enableOnly {
err = host.EnableOnly(src, profile)
} else {
err = host.Enable(src, profile)
}
if err != nil {
return err
}

if quiet {
return nil
}
return host.ListProfiles(src, &host.ListOptions{
Profile: profile,
})
Expand All @@ -52,4 +62,5 @@ func init() {
rootCmd.AddCommand(enableCmd)

enableCmd.Flags().BoolP("all", "", false, "Enable all profiles")
enableCmd.Flags().Bool("only", false, "Disable all other profiles")
}
9 changes: 6 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var listCmd = &cobra.Command{
Shows a detailed list of profiles on your hosts file with name, ip and host name.
You can filter by profile name.
The "default"" profile is all the content that is not handled by hostctl tool.
The "default" profile is all the content that is not handled by hostctl tool.
`,
RunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
Expand All @@ -36,6 +36,9 @@ The "default"" profile is all the content that is not handled by hostctl tool.
func init() {
rootCmd.AddCommand(listCmd)

listCmd.Flags().StringSliceP("column", "c", nil, "Columns to show on lists")
listCmd.Flags().Bool("raw", false, "Output without table borders")
listCmd.AddCommand(makeListStatusCmd("enabled"))
listCmd.AddCommand(makeListStatusCmd("disabled"))

listCmd.PersistentFlags().StringSliceP("column", "c", nil, "Columns to show on lists")
listCmd.PersistentFlags().Bool("raw", false, "Output without table borders")
}
41 changes: 41 additions & 0 deletions cmd/list_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
)

// makeListStatusCmd represents the list enabled command
var makeListStatusCmd = func(cmd string) *cobra.Command {
status := ""
switch cmd {
case "enabled":
status = "on"
case "disabled":
status = "off"
}
return &cobra.Command{
Use: cmd,
Aliases: []string{status},
Short: fmt.Sprintf("Shows list of %s profiles on your hosts file.", cmd),
Long: fmt.Sprintf(`
Shows a detailed list of %s profiles on your hosts file with name, ip and host name.
`, cmd),
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
raw, _ := cmd.Flags().GetBool("raw")
cols, _ := cmd.Flags().GetStringSlice("column")

err := host.ListProfiles(src, &host.ListOptions{
RawTable: raw,
Columns: cols,
StatusFilter: status,
})

return err
},
}
}
42 changes: 6 additions & 36 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var removeCmd = &cobra.Command{
Completely remove a profile content from your hosts file.
It cannot be undone unless you have a backup and restore it.
If you want to remove a profile but would like to use it later,
If you want to remove a profile but would like to use it later,
use 'hosts disable' instead.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -35,50 +35,20 @@ use 'hosts disable' instead.
RunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
dst, _ := cmd.Flags().GetString("host-file")
quiet, _ := cmd.Flags().GetBool("quiet")

err := host.RemoveProfile(dst, profile)
if err != nil {
return err
}

fmt.Printf("Profile '%s' removed.", profile)

return nil
},
}

// removeDomainsCmd represents the remove command
var removeDomainsCmd = &cobra.Command{
Use: "domains",
Short: "Remove domains from your hosts file.",
Long: `
Completely remove domains from your hosts file.
It cannot be undone unless you have a backup and restore it.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
return host.MissingProfileError
if quiet {
return nil
}

if profile == "default" {
return host.DefaultProfileError
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
dst, _ := cmd.Flags().GetString("host-file")

err := host.RemoveDomains(dst, profile, args)
if err != nil {
return err
}
fmt.Printf("Profile '%s' removed.\n\n", profile)

return host.ListProfiles(dst, &host.ListOptions{
Profile: profile,
})
return nil
},
}

Expand Down
46 changes: 46 additions & 0 deletions cmd/remove_domains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
)

// removeDomainsCmd represents the remove command
var removeDomainsCmd = &cobra.Command{
Use: "domains",
Short: "Remove domains from your hosts file.",
Long: `
Completely remove domains from your hosts file.
It cannot be undone unless you have a backup and restore it.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
return host.MissingProfileError
}

if profile == "default" {
return host.DefaultProfileError
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
dst, _ := cmd.Flags().GetString("host-file")
quiet, _ := cmd.Flags().GetBool("quiet")

err := host.RemoveDomains(dst, profile, args)
if err != nil {
return err
}

if quiet {
return nil
}
return host.ListProfiles(dst, &host.ListOptions{
Profile: profile,
})
},
}
Loading

0 comments on commit 86d5a69

Please sign in to comment.