Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Marin committed Mar 14, 2020
0 parents commit cbab815
Show file tree
Hide file tree
Showing 27 changed files with 1,108 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: goreleaser

on:
push:
tags:
- '*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Unshallow
run: git fetch --prune --unshallow
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.14.x
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: release --rm-dist
key: ${{ secrets.YOUR_PRIVATE_KEY }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18 changes: 18 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.13.x, 1.14.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.vscode
dist
47 changes: 47 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at https://goreleaser.com
project_name: hostctl
builds:
- id: hostctl
binary: hostctl
main: main.go
env:
- GO111MODULE=on
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- amd64

changelog:
sort: desc

archives:
- format_overrides:
- goos: windows
format: zip
replacements:
amd64: 64-bit
darwin: macOS

before:
hooks:
- go mod tidy

release:
github:
owner: guumaster
name: hostctl

nfpms:
- id: hostctl
builds:
- hostctl
formats:
- deb
vendor: Guumaster
homepage: https://github.com/guumaster/hostctl
maintainer: guumaster <[email protected]>
description: manage your hosts file like a pro
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2020] [Gustavo Marin]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[![Tests](https://img.shields.io/github/workflow/status/guumaster/hostctl/Test)](https://github.com/guumaster/hostctl/actions?query=workflow%3ATest)
[![GitHub Release](https://img.shields.io/github/release/guumaster/hostctl.svg?logo=github&labelColor=262b30)](https://github.com/guumaster/hostctl/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/guumaster/hostctl)](https://goreportcard.com/report/github.com/guumaster/hostctl)
[![License](https://img.shields.io/github/license/guumaster/hostctl)](https://github.com/guumaster/hostctl/LICENSE)

# hostctl

> Manage your /etc/hosts like a pro!
This tool gives you more control over the use of your `hosts` file. You can have multiple profiles and enable/disable as you need.


## Why?

It is a tedious task to handle the `hosts` file by editing manually. With this tool you can automate some aspects to do it cleaner and quick.


## Installation

Go to [release page](https://github.com/guumaster/hostctl/releases) and download the binary you need.


## Features

* Manage groups of host names by profile.
* Enable/disable complete profiles.
* add/remove groups of host names.
* add profiles directly from a `.etchosts` file that you can add to your vcs.


## Usage

`hostctl list`
+---------+--------+----------------+----------------------------+
| PROFILE | STATUS | IP | DOMAIN |
+---------+--------+----------------+----------------------------+
| lite | on | 192.168.1.51 | jupyter.toolkit-lite.local |
+---------+--------+----------------+----------------------------+
| toolkit | on | 192.168.99.119 | minio.toolkit.local |
| toolkit | on | 192.168.99.119 | app.toolkit.local |
| toolkit | on | 192.168.99.119 | gitea.toolkit.local |
| toolkit | on | 192.168.99.119 | jupyter.toolkit.local |
| toolkit | on | 192.168.99.119 | drone.toolkit.local |
| toolkit | on | 192.168.99.119 | code.toolkit.local |
+---------+--------+----------------+----------------------------+


`hostctl from-file` -f /path/to/.etchosts [-p profile]
`hostctl remove ` [-p profile]
`hostctl enable` [-p profile]
`hostctl disable` [-p profile]
`hostctl backup -f path_to_file`
`hostctl restore -f path_to_file`


### TODO

`hostctl from-k8s -n namespace`
`hostctl from-minikube -n namespace`


`hostctl set ip` IP [-p profile]
`hostctl set domains` dom1,dom2,etc [-p profile]
`hostctl add domains` dom1,dom2 [-p profile]
`hostctl rm domains` dom1,dom2 [-p profile]


#### References

* Dependencies:
* [spf13/cobra](https://github.com/spf13/cobra)
* [spf13/viper](https://github.com/spf13/viper)
* [mitchellh/go-homedir](https://github.com/mitchellh/go-homedir)
* [guumaster/tablewriter](https://github.com/guumaster/tablewriter)

* Inspiration:
* [txn2/txeh: CLI for /etc/hosts management](https://github.com/txn2/txeh)


### LICENSE

[MIT license](LICENSE)


### Author(s)

* [guumaster](https://github.com/guumaster)
34 changes: 34 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/spf13/cobra"

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

// addFromFileCmd represents the fromFile command
var addFromFileCmd = &cobra.Command{
Use: "add",
Short: "Add content to a profile in your hosts file.",
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.`,
RunE: func(cmd *cobra.Command, args []string) error {
from, _ := cmd.Flags().GetString("from")
profile, _ := cmd.Flags().GetString("profile")

h, _ := cmd.Flags().GetString("host-file")

return host.AddFromFile(from, h, profile, false)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
return host.CheckProfile(profile)
},
}

func init() {
rootCmd.AddCommand(addFromFileCmd)

addFromFileCmd.Flags().StringP("from", "f", "", "file to read")
}
45 changes: 45 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"errors"
"log"
"os"

"github.com/spf13/cobra"

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

// backupCmd represents the backup command
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.
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

if profile != "" {
return errors.New("backup can only be done to whole file. remove profile flag")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
dst, _ := cmd.Flags().GetString("path")

return host.BackupFile(src, dst)
},
}

func init() {
rootCmd.AddCommand(backupCmd)

cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
backupCmd.Flags().String("path", cwd, "A path to save the backup")
}
46 changes: 46 additions & 0 deletions cmd/disable.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"
)

// disableCmd represents the disable command
var disableCmd = &cobra.Command{
Use: "disable",
Short: "Disable a profile from your hosts file.",
Long: `
Disable a profile from your hosts file without removing it.
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")

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

var err error
if all {
profile = ""
}
err = host.Disable(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.CheckProfile(profile)
},
}

func init() {
rootCmd.AddCommand(disableCmd)

disableCmd.Flags().BoolP("all", "", false, "Disable all profiles")
}
46 changes: 46 additions & 0 deletions cmd/enable.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"
)

// enableCmd represents the enable command
var enableCmd = &cobra.Command{
Use: "enable",
Short: "Enable a profile on your hosts file.",
Long: `
Disable an existing profile from your hosts file without removing it.
It will be listed as "on" while it is enabled.
`,
RunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")

all, _ := cmd.Flags().GetBool("all")
if all {
profile = ""
}

src, _ := cmd.Flags().GetString("host-file")

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.CheckProfile(profile)
},
}

func init() {
rootCmd.AddCommand(enableCmd)

enableCmd.Flags().BoolP("all", "", false, "Enable all profiles")
}
Loading

0 comments on commit cbab815

Please sign in to comment.