Skip to content

Commit

Permalink
Fixes #3. Enable commands to be executed non-interactively
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Sep 10, 2016
1 parent c121d60 commit 7ee7a15
Show file tree
Hide file tree
Showing 1,315 changed files with 632,692 additions and 29 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Useful for performing quick reboots or for pulling statistics from the Home Hub.

## Usage

#### Using the CLI

Download one of the [releases](https://github.com/jamesnetherton/homehub-cli/releases) for your operating system.

For *nix and OS X:
Expand All @@ -21,6 +23,16 @@ For Windows:

You'll need to authenticate against the Home Hub in order to do anything useful. Use the `Login` command to do this. Pressing the `TAB` key shows all of the available commands.

#### Running indivdual commands

You can run indivdual commands outside of the CLI shell by specifying the desired function to execute, together with the Home Hub authentication details.

./homehub-cli Reboot --huburl=http://192.168.1.254 --username=admin --password=bar

The `huburl` and `username` arguments are defaulted to the standard Home Hub IP address and admin user name. So you can omit these arguments if you want to and just specify the `password`.

See `./homehub-cli --help` for all options.

## Building

git clone [email protected]:jamesnetherton/homehub-cli.git $GOPATH/src/github.com/jamesnetherton/homehub-cli
Expand Down
1 change: 0 additions & 1 deletion functions/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var FuncNames = []string{
"InternetConnectionStatus",
"LightStatus",
"LocalTime",
"Login",
"MaintenaceFirmwareVersion",
"PublicIPAddress",
"PublicSubnetMask",
Expand Down
2 changes: 1 addition & 1 deletion generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
for _, line := range strings.Split(body, "\n") {
if strings.HasPrefix(line, "func") {
matches := [][]string{regex.FindStringSubmatch(line)}
if len(matches[0]) > 0 {
if len(matches[0]) > 0 && matches[0][1] != "Login" {
names = append(names, strconv.Quote(matches[0][1]))
}
}
Expand Down
69 changes: 67 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ import:
- package: github.com/chzyer/readline
version: ~1.4.0
- package: github.com/jamesnetherton/homehub-client
- package: github.com/spf13/cobra
subpackages:
- cobra
Binary file added homehub-cli
Binary file not shown.
109 changes: 84 additions & 25 deletions homehub-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,95 @@ import (
"github.com/chzyer/readline"
"github.com/jamesnetherton/homehub-cli/functions"
"github.com/jamesnetherton/homehub-client"
"github.com/spf13/cobra"
"github.com/spf13/cobra/cobra/cmd"
)

var (
hubURL string
userName string
password string
hub *homehub.Hub
)

func main() {
var hub *homehub.Hub
cmd.RootCmd.PersistentFlags().StringVarP(&hubURL, "huburl", "r", "http://192.168.1.254", "URL of the home hub router")
cmd.RootCmd.PersistentFlags().StringVarP(&userName, "username", "u", "admin", "The hub router user name")
cmd.RootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "The home hub router password")

cmdHandler := func(cmd *cobra.Command, args []string) {
if !stringIsEmpty(hubURL) && !stringIsEmpty(userName) && !stringIsEmpty(password) {
hub = homehub.New(hubURL, userName, password)
success, _ := hub.Login()

if !success {
fmt.Println("Login failed")
return
}

invokeMethod(cmd.Name())
} else {
cmd.Usage()
}
}

for _, funcName := range functions.FuncNames {
cmd.RootCmd.AddCommand(&cobra.Command{
Use: funcName,
Run: cmdHandler,
})
}

banner()
helpFunc := func(cmd *cobra.Command, args []string) {
fmt.Println("Usage:\n homehub-cli [command] --huburl=<home hub url> --username=<home hub username> --password=<home hub password>")
fmt.Println("\nCommands:\n ", strings.Join(functions.FuncNames, "\n "))
}

l, err := createReadline()
if err != nil {
panic(err)
usageFunc := func(cmd *cobra.Command) error {
fmt.Printf("Usage:\n homehub-cli %s --huburl=<home hub url> --username=<home hub username> --password=<home hub password>\n", cmd.Name())
return nil
}
defer l.Close()

for {
line, err := l.Readline()
cmd.RootCmd.SetHelpFunc(helpFunc)
cmd.RootCmd.SetUsageFunc(usageFunc)
cmd.RootCmd.SilenceErrors = true

if len(os.Args[1:]) == 0 {
banner()

if err == readline.ErrInterrupt {
if len(line) == 0 {
l, err := createReadline()
if err != nil {
panic(err)
}
defer l.Close()

for {
line, err := l.Readline()

if err == readline.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
} else {
continue
}
} else if err == io.EOF {
break
}

line = strings.TrimSpace(line)
if len(line) > 0 {
if line != "Login" {
invokeMethod(hub, line)
} else {
hub = doLogin(l)
line = strings.TrimSpace(line)
if !stringIsEmpty(line) {
if line != "Login" {
invokeMethod(line)
} else {
hub = doLogin(l)
}
}
}
} else {
err := cmd.RootCmd.Execute()
if err != nil {
helpFunc(nil, nil)
}
}
}

Expand All @@ -60,9 +115,9 @@ func createReadline() (l *readline.Instance, err error) {
func getUserPrompt() string {
var user string

if len(strings.TrimSpace(os.Getenv("USER"))) > 0 {
if !stringIsEmpty(os.Getenv("USER")) {
user = os.Getenv("USER")
} else if len(strings.TrimSpace(os.Getenv("USERNAME"))) > 0 {
} else if !stringIsEmpty(os.Getenv("USERNAME")) {
user = os.Getenv("USERNAME")
} else {
user = "unknown"
Expand All @@ -73,11 +128,11 @@ func getUserPrompt() string {

func listFuncNames() func(string) []string {
return func(s string) []string {
return functions.FuncNames
return append(functions.FuncNames, "Login")
}
}

func invokeMethod(hub *homehub.Hub, methodName string) {
func invokeMethod(methodName string) {
m := reflect.ValueOf(hub).MethodByName(methodName)
if m.IsValid() {
if hub != nil {
Expand All @@ -92,6 +147,10 @@ func invokeMethod(hub *homehub.Hub, methodName string) {
}
}

func stringIsEmpty(s string) bool {
return len(strings.TrimSpace(s)) == 0
}

func doLogin(l *readline.Instance) *homehub.Hub {
var URL string
var username string
Expand Down
6 changes: 6 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7ee7a15

Please sign in to comment.