Skip to content

Commit

Permalink
define some error vars and improve error handling. add more output to…
Browse files Browse the repository at this point in the history
… commands. sort cmd functions.
  • Loading branch information
Gustavo Marin committed Mar 20, 2020
1 parent ecbe34e commit 21078d2
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Go to [release page](https://github.com/guumaster/hostctl/releases) and download


## Sample Usage
[![sample usage](docs/hostctl.gif)]
![sample usage](docs/hostctl.gif)


## Linux/Mac/Windows and permissions
Expand Down
36 changes: 23 additions & 13 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ If the profile already exists it will be added to it.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

err := host.NotEmptyProfile(profile)
if err != nil {
return err
if profile == "" {
return host.MissingProfileError
}

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

Expand All @@ -53,14 +55,22 @@ var addDomainsCmd = &cobra.Command{
Long: `
Set content in your hosts file.
If the profile already exists it will be added to it.`,
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
ip, _ := cmd.Flags().GetString("ip")
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile == "" {
profile = "other"
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")
h, _ := cmd.Flags().GetString("host-file")

err := host.AddFromArgs(&host.AddFromArgsOptions{
Expand All @@ -74,15 +84,15 @@ If the profile already exists it will be added to it.`,
return err
}

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

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

return host.ValidProfile(profile)
},
}

func init() {
Expand Down
13 changes: 8 additions & 5 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"errors"
"github.com/guumaster/tablewriter"
"fmt"
"log"
"os"

Expand Down Expand Up @@ -32,12 +32,15 @@ as extension.
dst, _ := cmd.Flags().GetString("path")

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

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

table := tablewriter.NewWriter(os.Stdout)
table.Append([]string{backupFile})
table.Render()
fmt.Printf("Backup completed.")

return err
return nil
},
}

Expand Down
25 changes: 13 additions & 12 deletions cmd/disable.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"errors"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
Expand All @@ -16,6 +14,19 @@ var disableCmd = &cobra.Command{
Disable a profile from your hosts file without removing it.
It will be listed as "off" while it is disabled.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
all, _ := cmd.Flags().GetBool("all")

if !all && 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")
profile, _ := cmd.Flags().GetString("profile")
Expand All @@ -35,16 +46,6 @@ It will be listed as "off" while it is disabled.
Profile: profile,
})
},
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
all, _ := cmd.Flags().GetBool("all")

if !all && profile == "" {
return errors.New("missing profile name")
}

return host.ValidProfile(profile)
},
}

func init() {
Expand Down
25 changes: 13 additions & 12 deletions cmd/enable.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"errors"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
Expand All @@ -16,6 +14,19 @@ var enableCmd = &cobra.Command{
Disable an existing profile from your hosts file without removing it.
It will be listed as "on" while it is enabled.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
all, _ := cmd.Flags().GetBool("all")

if !all && 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")

Expand All @@ -35,16 +46,6 @@ It will be listed as "on" while it is enabled.
Profile: profile,
})
},
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
all, _ := cmd.Flags().GetBool("all")

if !all && profile == "" {
return errors.New("missing profile name")
}

return host.ValidProfile(profile)
},
}

func init() {
Expand Down
30 changes: 24 additions & 6 deletions cmd/remove.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/host"
Expand All @@ -17,20 +19,36 @@ 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,
use 'hosts disable' instead.
`,
RunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
dst, _ := cmd.Flags().GetString("host-file")
all, _ := cmd.Flags().GetBool("all")

if !all && profile == "" {
return host.MissingProfileError
}

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

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

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

return nil
},
}

func init() {
rootCmd.AddCommand(removeCmd)

removeCmd.Flags().BoolP("all", "", false, "Remove all profiles")
removeCmd.Flags().Bool("all", false, "Remove all profiles")
}
11 changes: 10 additions & 1 deletion cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"fmt"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -29,7 +30,15 @@ WARNING: the complete hosts file will be overwritten with the backup data.
dst, _ := cmd.Flags().GetString("host-file")
from, _ := cmd.Flags().GetString("from")

return host.RestoreFile(from, dst)
err := host.RestoreFile(from, dst)
if err != nil {
return err
}
_ = host.ListProfiles(dst, &host.ListOptions{})

fmt.Printf("Restore completed.")

return nil
},
}

Expand Down
47 changes: 28 additions & 19 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ var setFromFileCmd = &cobra.Command{
Reads from a file and set content to a profile in your hosts file.
If the profile already exists it will be overwritten.
`,
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 @@ -35,16 +47,6 @@ If the profile already exists it will be overwritten.
Profile: profile,
})
},
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

err := host.NotEmptyProfile(profile)
if err != nil {
return err
}

return host.ValidProfile(profile)
},
}

// setDomainsCmd represents the fromFile command
Expand All @@ -54,14 +56,26 @@ var setDomainsCmd = &cobra.Command{
Long: `
Set content in your hosts file.
If the profile already exists it will be added to it.`,
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
ip, _ := cmd.Flags().GetString("ip")
PreRunE: func(cmd *cobra.Command, domains []string) error {
profile, _ := cmd.Flags().GetString("profile")

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

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

if len(domains) == 0 {
return host.MissingDomainsError
}
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")
h, _ := cmd.Flags().GetString("host-file")

err := host.AddFromArgs(&host.AddFromArgsOptions{
Expand All @@ -79,11 +93,6 @@ If the profile already exists it will be added to it.`,
Profile: profile,
})
},
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

return host.ValidProfile(profile)
},
}

func init() {
Expand Down
9 changes: 4 additions & 5 deletions pkg/host/add.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package host

import (
"errors"
"os"
)

Expand All @@ -17,7 +16,7 @@ type AddFromFileOptions struct {
Dst string
Profile string
Reset bool
From string
From string
}

// AddFromArgsOptions contains available options for adding from arguments.
Expand All @@ -33,7 +32,7 @@ type AddFromArgsOptions struct {
// If you pass reset=true it will delete all previous content of the profile.
func AddFromFile(opts *AddFromFileOptions) error {
if opts.From == "" {
return errors.New("missing source file")
return MissingSourceError
}
newData, _ := ReadHostFileStrict(opts.From)

Expand All @@ -46,7 +45,7 @@ func AddFromFile(opts *AddFromFileOptions) error {

func AddFromArgs(opts *AddFromArgsOptions) error {
if len(opts.Domains) == 0 {
return errors.New("missing domains")
return MissingDomainsError
}
newData := ReadFromArgs(opts.Domains, opts.IP)

Expand All @@ -59,7 +58,7 @@ func AddFromArgs(opts *AddFromArgsOptions) error {

func add(n *hostFile, opts *commonAddOptions) error {
if opts.Dst == "" {
return errors.New("missing destination file")
return MissingDestError
}
if opts.Profile == "" {
opts.Profile = "default"
Expand Down
Loading

0 comments on commit 21078d2

Please sign in to comment.