Skip to content

Commit

Permalink
fix: make add domains to new profile works.
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Apr 15, 2020
1 parent 32306c5 commit 007b415
Show file tree
Hide file tree
Showing 37 changed files with 67 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.idea
.vscode
dist
hostctl
./hostctl
hostctl.exe
.etchosts
docs/public
Expand Down
5 changes: 2 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project_name: hostctl
builds:
- id: hostctl
binary: hostctl
main: main.go
main: cmd/hostctl/main.go
env:
- GO111MODULE=on
- CGO_ENABLED=0
Expand All @@ -15,7 +15,7 @@ builds:
goarch:
- amd64
ldflags:
- -s -w -X github.com/guumaster/hostctl/cmd.version={{.Version}}
- -s -w -X github.com/guumaster/hostctl/pkg/cmd/cmd.version={{.Version}}

changelog:
sort: desc
Expand Down Expand Up @@ -89,4 +89,3 @@ snapcrafts:
apps:
hostctl:
plugs: ["network-control"]

7 changes: 7 additions & 0 deletions cmd/hostctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/guumaster/hostctl/pkg/cmd"

func main() {
cmd.Execute()
}
11 changes: 0 additions & 11 deletions main.go

This file was deleted.

File renamed without changes.
25 changes: 25 additions & 0 deletions cmd/add_domains_test.go → pkg/cmd/add_domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ func Test_AddDomains(t *testing.T) {
assert.Contains(t, actual, expected)
})

t.Run("Add domains new profile", func(t *testing.T) {
tmp := makeTempHostsFile(t, "addDomainCmd")
defer os.Remove(tmp.Name())
b := bytes.NewBufferString("")

cmd.SetOut(b)
cmd.SetArgs([]string{"add", "domains", "newprofile", "arg.domain.loc", "--host-file", tmp.Name()})

err := cmd.Execute()
assert.NoError(t, err)

out, err := ioutil.ReadAll(b)
assert.NoError(t, err)

actual := "\n" + string(out)
expected := `
+------------+--------+-----------+----------------+
| PROFILE | STATUS | IP | DOMAIN |
+------------+--------+-----------+----------------+
| newprofile | on | 127.0.0.1 | arg.domain.loc |
+------------+--------+-----------+----------------+
`
assert.Contains(t, actual, expected)
})

t.Run("Add domains with IP", func(t *testing.T) {
tmp := makeTempHostsFile(t, "addDomainCmd")
defer os.Remove(tmp.Name())
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 0 additions & 14 deletions cmd/helpers.go → pkg/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"os"
"runtime"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -68,16 +67,3 @@ func containsDefault(args []string) error {
}
return nil
}

func getDefaultHostFile() string {
envHostFile := os.Getenv("HOSTCTL_FILE")
if envHostFile != "" {
return envHostFile
}

if runtime.GOOS == "windows" {
return `C:/Windows/System32/Drivers/etc/hosts`
}

return "/etc/hosts"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 19 additions & 5 deletions cmd/root.go → pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"runtime"

"github.com/spf13/cobra"
)
Expand All @@ -16,11 +17,11 @@ var rootCmd = &cobra.Command{
Use: "hostctl",
Short: "Your dev tool to manage /etc/hosts like a pro",
Long: `
__ __ __ __
/ /_ ____ _____ / /_ _____ / /_ / /
/ __ \ / __ \ / ___/ / __/ / ___/ / __/ / /
/ / / // /_/ / (__ ) / /_ / /__ / /_ / /
/_/ /_/ \____/ /____/ \__/ \___/ \__/ /_/
__ __ __ __
/ /_ ____ _____ / /_ _____ / /_ / /
/ __ \ / __ \ / ___/ / __/ / ___/ / __/ / /
/ / / // /_/ / (__ ) / /_ / /__ / /_ / /
/_/ /_/ \____/ /____/ \__/ \___/ \__/ /_/
hostctl is a CLI tool to manage your hosts file with ease.
Expand Down Expand Up @@ -56,3 +57,16 @@ func init() {
rootCmd.PersistentFlags().Bool("raw", false, "Output without table borders")
rootCmd.PersistentFlags().StringSliceP("column", "c", nil, "Columns to show on lists")
}

func getDefaultHostFile() string {
envHostFile := os.Getenv("HOSTCTL_FILE")
if envHostFile != "" {
return envHostFile
}

if runtime.GOOS == "windows" {
return `C:/Windows/System32/Drivers/etc/hosts`
}

return "/etc/hosts"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 13 additions & 1 deletion pkg/host/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package host

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -84,7 +85,18 @@ func (f *File) GetDisabled() []string {

func (f *File) AddRoutes(name, ip string, hostnames []string) error {
profile, err := f.GetProfile(name)
if err != nil {
if err != nil && !errors.Is(err, UnknownProfileError) {
return err
}

if profile == nil {
p := Profile{
Name: name,
Status: Enabled,
Routes: map[string]*Route{},
}
p.AddRoutes(ip, hostnames)
err := f.AddProfile(p)
return err
}

Expand Down

0 comments on commit 007b415

Please sign in to comment.