diff --git a/.goreleaser.yml b/.goreleaser.yml index 2798e35..73011b7 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -3,7 +3,7 @@ builds: - id: txeh # Path to main.go file. # Default is `main.go` - main: ./util/txeh.go + main: ./txeh/txeh.go binary: txeh env: @@ -29,7 +29,7 @@ builds: - "6" - "7" - ldflags: "-s -w -X github.com/txn2/txeh/util/cmd.Version={{.Version}}" + ldflags: "-s -w -X github.com/txn2/txeh/txeh/cmd.Version={{.Version}}" checksum: name_template: '{{ .ProjectName }}_checksums.txt' @@ -91,4 +91,4 @@ snapcrafts: description: | Kubernetes bulk port forwarding utility. grade: stable - confinement: classic \ No newline at end of file + confinement: classic diff --git a/README.md b/README.md index d3d0297..56068d3 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,21 @@ A computer's [/etc/hosts] file is a powerful utility for developers and system a MacOS [homebrew](https://brew.sh) users can `brew install txn2/tap/txeh`, otherwise see [releases](https://github.com/txn2/txeh/releases) for packages and binaries for a number of distros and architectures including Windows, Linux and Arm based systems. -Complie and run from source (dependencies are vendored): +#### Install with go install + +When installing with Go please use the latest stable Go release. At least go1.16 or greater is required. + +To install use: `go install github.com/txn2/txeh/txeh@master` + +If you are building from a local source clone, use `go install ./txeh` from the top-level directory of the clone. + +go install will typically put the txeh binary inside the bin directory under go env GOPATH, see Go’s [Compile and install packages and dependencies](https://golang.org/cmd/go/#hdr-Compile_and_install_packages_and_dependencies) for more on this. You may need to add that directory to your $PATH if you encounter the error `txeh: command not found` after installation, you can find a guide for adding a directory to your PATH at https://gist.github.com/nex3/c395b2f8fd4b02068be37c961301caa7#file-path-md. + +#### Compile and run from source + +dependencies are vendored: ``` -go run ./util/txeh.go +go run ./txeh/txeh.go ``` ### Use @@ -69,7 +81,7 @@ sudo txeh add 127.0.0.1 test test.two # remove the hostname "test" sudo txeh remove host test -# remove multiple hostnames +# remove multiple hostnames sudo txeh remove host test test2 test.two # remove an IP address and all the hosts that point to it @@ -129,27 +141,27 @@ func main() { hosts.AddHost("127.100.100.100", "test") hosts.AddHost("127.100.100.101", "logstash") hosts.AddHosts("127.100.100.102", []string{"a", "b", "c"}) - + hosts.RemoveHosts([]string{"example", "example.machine", "example.machine.example.com"}) hosts.RemoveHosts(strings.Fields("example2 example.machine2 example.machine.example.com2")) - + hosts.RemoveAddress("127.1.27.1") - + removeList := []string{ "127.1.27.15", "127.1.27.14", "127.1.27.13", } - + hosts.RemoveAddresses(removeList) - + hfData := hosts.RenderHostsFile() // if you like to see what the outcome will // look like fmt.Println(hfData) - + hosts.Save() // or hosts.SaveAs("./test.hosts") } @@ -160,12 +172,12 @@ func main() { Build test release: ```bash -goreleaser --skip-publish --rm-dist --skip-validate +goreleaser --skip-publish --clean --skip-validate ``` Build and release: ```bash -GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --rm-dist +GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --clean ``` ### License diff --git a/util/cmd/add.go b/txeh/cmd/add.go similarity index 100% rename from util/cmd/add.go rename to txeh/cmd/add.go diff --git a/util/cmd/list.go b/txeh/cmd/list.go similarity index 100% rename from util/cmd/list.go rename to txeh/cmd/list.go diff --git a/util/cmd/list_cidr.go b/txeh/cmd/list_cidr.go similarity index 100% rename from util/cmd/list_cidr.go rename to txeh/cmd/list_cidr.go diff --git a/util/cmd/list_hosts.go b/txeh/cmd/list_hosts.go similarity index 100% rename from util/cmd/list_hosts.go rename to txeh/cmd/list_hosts.go diff --git a/util/cmd/list_ip.go b/txeh/cmd/list_ip.go similarity index 100% rename from util/cmd/list_ip.go rename to txeh/cmd/list_ip.go diff --git a/util/cmd/remove.go b/txeh/cmd/remove.go similarity index 100% rename from util/cmd/remove.go rename to txeh/cmd/remove.go diff --git a/util/cmd/remove_cidr.go b/txeh/cmd/remove_cidr.go similarity index 100% rename from util/cmd/remove_cidr.go rename to txeh/cmd/remove_cidr.go diff --git a/util/cmd/remove_host.go b/txeh/cmd/remove_host.go similarity index 100% rename from util/cmd/remove_host.go rename to txeh/cmd/remove_host.go diff --git a/util/cmd/remove_ip.go b/txeh/cmd/remove_ip.go similarity index 100% rename from util/cmd/remove_ip.go rename to txeh/cmd/remove_ip.go diff --git a/util/cmd/root.go b/txeh/cmd/root.go similarity index 98% rename from util/cmd/root.go rename to txeh/cmd/root.go index cf12aa1..4df0305 100644 --- a/util/cmd/root.go +++ b/txeh/cmd/root.go @@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{ | |___ _____| |__ | __\ \/ / _ \ '_ \ | |_ > < __/ | | | - \__/_/\_\___|_| |_| v` + Version + ` + \__/_/\_\___|_| |_| Version: ` + VersionFromBuild() + ` Add, remove and re-associate hostname entries in your /etc/hosts file. Read more including usage as a Go library at https://github.com/txn2/txeh`, diff --git a/util/cmd/show.go b/txeh/cmd/show.go similarity index 100% rename from util/cmd/show.go rename to txeh/cmd/show.go diff --git a/txeh/cmd/version.go b/txeh/cmd/version.go new file mode 100644 index 0000000..4f76a72 --- /dev/null +++ b/txeh/cmd/version.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "fmt" + "runtime/debug" + + "github.com/spf13/cobra" +) + +var Version = "0.0.0" + +func init() { + rootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version number of txeh", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + version := VersionFromBuild() + fmt.Printf("txeh Version: %s\n", version) + }, +} + +// Version returns the version of txeh binary +func VersionFromBuild() (version string) { + // Version is managed with goreleaser + if Version != "0.0.0" { + return Version + } + // Version is managed by "go install" + b, ok := debug.ReadBuildInfo() + if !ok { + return "unknown" + } + if b == nil { + version = "nil" + } else { + version = b.Main.Version + } + return version +} diff --git a/util/txeh.go b/txeh/txeh.go similarity index 54% rename from util/txeh.go rename to txeh/txeh.go index 25a2c47..8fb490f 100644 --- a/util/txeh.go +++ b/txeh/txeh.go @@ -1,6 +1,6 @@ package main -import "github.com/txn2/txeh/util/cmd" +import "github.com/txn2/txeh/txeh/cmd" func main() { cmd.Execute() diff --git a/util/cmd/version.go b/util/cmd/version.go deleted file mode 100644 index f1558a8..0000000 --- a/util/cmd/version.go +++ /dev/null @@ -1,22 +0,0 @@ -package cmd - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -var Version = "0.0.0" - -func init() { - rootCmd.AddCommand(versionCmd) -} - -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Print the version number of txeh", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("txeh Version %s\n", Version) - }, -}