Skip to content

Commit

Permalink
Fix unexported member names, other golint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhiram committed Apr 16, 2018
1 parent 192836b commit b83cc3a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 53 deletions.
63 changes: 37 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# go-wol

[![Build Status](https://travis-ci.org/sabhiram/go-wol.svg?branch=master)](https://travis-ci.org/sabhiram/go-wol) [![Coverage Status](https://coveralls.io/repos/github/sabhiram/go-wol/badge.svg?branch=master)](https://coveralls.io/github/sabhiram/go-wol?branch=master)
[![Build Status](https://travis-ci.org/sabhiram/go-wol.svg?branch=master)](https://travis-ci.org/sabhiram/go-wol) [![Coverage Status](https://coveralls.io/repos/github/sabhiram/go-wol/badge.svg?branch=master)](https://coveralls.io/github/sabhiram/go-wol?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/sabhiram/go-wol)](https://goreportcard.com/report/github.com/sabhiram/go-wol)

Wake on LAN magic packet generator for golang.

Simple wake on LAN magic packet generator for golang

## WOL in the world?

Expand All @@ -18,13 +19,15 @@ The listening interface just looks for a `Magic Packet` with it's MAC address en

It is important to remember that since this is typically sent over the [data link layer](http://en.wikipedia.org/wiki/Data_link_layer), the target machine's IP address is irrelevant.


## Installation

```
go get github.com/sabhiram/go-wol/cmd/wol
wol wake 08:BA:AD:F0:00:0D
```


## Usage

Valid commands include:
Expand All @@ -44,34 +47,56 @@ With the following options (mostly apply to the wake command):
{`i`, `interface`, `outbound interface to broadcast using`},
```

Wake up a machine with mac address `00:11:22:aa:bb:cc`:

## Defaults

The default Broadcast IP is `255.255.255.255` and the UDP Port is `9`. Typically the UDP port is either `7` or `9`. The default interface is set to `""` which tell the program to use any available interface.


## Alias file

The alias file is typically stored in the user's Home directory under the path of `~/.config/go-wol/aliases`. This is a very simple [`BoltDB`](https://github.com/coreos/bbolt) which reads a per-alias `Gob` made up of a MAC address and an optional preferred outbound interface.


## Supported MAC addresses

The following MAC addresses are valid and will match:
`01-23-45-56-67-89`, `89:0A:CD:EF:00:12`, `89:0a:cd:ef:00:12`

The following MAC addresses are not (yet) valid:
`1-2-3-4-5-6`, `01 23 45 56 67 89`


## CLI examples

#### Wake up a machine with mac address `00:11:22:aa:bb:cc`:

wol wake 00:11:22:aa:bb:cc

Store an alias:
#### Store an alias:

wol alias skynet 00:11:22:aa:bb:cc

Note that when waking up a machine, the `wake` command pretty much exists for clarity. You can safely omit it (unless your alias name is `list`, `wake`, `alias` or `remove`).

Wake up a machine using an alias:
#### Wake up a machine using an alias:

wol wake skynet
wol skynet

View all aliases and corresponding MAC addresses:
#### View all aliases and corresponding MAC addresses:

wol list

Delete an alias:
#### Delete an alias:

wol remove skynet

Store an alias to a MAC using a default interface:
#### Store an alias to a MAC using a default interface:

wol alias skynet 00:11:22:aa:bb:cc eth0

Specify a Broadcast Interface (Local to the sender):
#### Specify a Broadcast Interface (Local to the sender):
```
wol wake skynet -i eth0
Expand All @@ -80,9 +105,9 @@ wol wake skynet -i eth0
wol wake skynet --interface eth0
```

Please note that when specifying an interface to use, you can set that as part of the alias. However, if the `-i` option is specified, the specified interface will be used and the one in the alias map will be ignored.
Note that when specifying an interface to use, you can set that as part of the alias. However, if the `-i` option is specified, the specified interface will be used and the one in the alias map will be ignored.

Specify the Broadcast Port and IP:
#### Specify the Broadcast Port and IP:
```
wol wake 00:11:22:aa:bb:cc -b 255.255.255.255 -p 7
Expand All @@ -91,21 +116,6 @@ wol wake 00:11:22:aa:bb:cc -b 255.255.255.255 -p 7
wol wake skynet --bcast 255.255.255.255 --port 7
```

#### Defaults

The default Broadcast IP is `255.255.255.255` and the UDP Port is `9`. Typically the UDP port is either `7` or `9`. The default interface is set to `""` which tell the program to use any available interface.

#### Alias file

The alias file is typically stored in the user's Home directory under the path of `~/.config/go-wol/aliases`. This is a very simple [`BoltDB`](https://github.com/boltdb/bolt) which reads a per-alias `Gob` made up of a MAC address and an optional preferred outbound interface.

#### This is how `wol` expects MAC addresses to look

The following MAC addresses are valid and will match:
`01-23-45-56-67-89`, `89:0A:CD:EF:00:12`, `89:0a:cd:ef:00:12`

The following MAC addresses are not (yet) valid:
`1-2-3-4-5-6`, `01 23 45 56 67 89`

## Tests

Expand All @@ -115,6 +125,7 @@ To run the tests:

go test -v github.com/sabhiram/go-wol/...


## Contributors:

1. @traetox for helping adding the optional outbound interface for MagicPackets, and swapping out the `Gob` written to disk for a more elegant `BoltDB` solution.
Expand Down
12 changes: 6 additions & 6 deletions cmd/wol/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
////////////////////////////////////////////////////////////////////////////////

const (
MainBucketName string = "Aliases"
bucketName = "Aliases"
)

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -70,7 +70,7 @@ func LoadAliases(dbpath string) (*Aliases, error) {
}

if err := db.Update(func(tx *bolt.Tx) error {
if _, lerr := tx.CreateBucketIfNotExists([]byte(MainBucketName)); lerr != nil {
if _, lerr := tx.CreateBucketIfNotExists([]byte(bucketName)); lerr != nil {
return lerr
}
return nil
Expand Down Expand Up @@ -99,7 +99,7 @@ func (a *Aliases) Add(alias, mac, iface string) error {
// We don't have to worry about the key existing, as we will update it
// provided it exists.
return a.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(MainBucketName))
bucket := tx.Bucket([]byte(bucketName))
return bucket.Put([]byte(alias), buf.Bytes())
})
}
Expand All @@ -110,7 +110,7 @@ func (a *Aliases) Del(alias string) error {
defer a.mtx.Unlock()

return a.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(MainBucketName))
bucket := tx.Bucket([]byte(bucketName))
return bucket.Delete([]byte(alias))
})
}
Expand All @@ -124,7 +124,7 @@ func (a *Aliases) Get(alias string) (MacIface, error) {
err := a.db.View(func(tx *bolt.Tx) error {
var err error

bucket := tx.Bucket([]byte(MainBucketName))
bucket := tx.Bucket([]byte(bucketName))
value := bucket.Get([]byte(alias))
if value == nil {
return fmt.Errorf("alias (%s) not found in db", alias)
Expand All @@ -143,7 +143,7 @@ func (a *Aliases) List() (map[string]MacIface, error) {

aliasMap := make(map[string]MacIface, 1)
err := a.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(MainBucketName))
bucket := tx.Bucket([]byte(bucketName))
cursor := bucket.Cursor()
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
if entry, err := DecodeToMacIface(bytes.NewBuffer(v)); err == nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/wol/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

// Helper regex to strip the preamble from the function name. This is
// used to create a temp db file per test (based on the test name).
var RE_stripFnPreamble = regexp.MustCompile(`^.*\.(.*)$`)
var reStripFnPreamble = regexp.MustCompile(`^.*\.(.*)$`)

////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -75,7 +75,7 @@ type AliasDBTests struct {
func (suite *AliasDBTests) SetupTest() {
pc, _, _, ok := runtime.Caller(1)
if ok {
suite.dbName = RE_stripFnPreamble.ReplaceAllString(runtime.FuncForPC(pc).Name(), "$1")
suite.dbName = reStripFnPreamble.ReplaceAllString(runtime.FuncForPC(pc).Name(), "$1")
}

var err error
Expand Down
12 changes: 6 additions & 6 deletions cmd/wol/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
////////////////////////////////////////////////////////////////////////////////

var (
ValidCommands = []struct {
validCommands = []struct {
name, description string
}{
{`wake`, `wakes up a machine by mac address or alias`},
Expand All @@ -22,7 +22,7 @@ var (
{`remove`, `removes an alias or a mac address`},
}

ValidOptions = []struct {
validOptions = []struct {
short, long, description string
}{
{`v`, `version`, `prints the application version`},
Expand All @@ -32,7 +32,7 @@ var (
{`i`, `interface`, `outbound interface to broadcast using`},
}

UsageString = `Usage:
usageString = `Usage:
To wake up a machine:
<cyan>wol</cyan> [<options>] <yellow>wake</yellow> <mac address | alias> <optional interface>
Expand Down Expand Up @@ -67,7 +67,7 @@ Version:
// Build a command string from the above valid ones.
func getAllCommands() string {
commands := ""
for _, c := range ValidCommands {
for _, c := range validCommands {
commands += fmt.Sprintf(" <yellow>%-16s</yellow> %s\n", c.name, c.description)
}
return commands
Expand All @@ -76,13 +76,13 @@ func getAllCommands() string {
// Build an option string from the above valid ones.
func getAllOptions() string {
options := ""
for _, o := range ValidOptions {
for _, o := range validOptions {
options += fmt.Sprintf(" <yellow>-%s --%-8s</yellow> %s\n", o.short, o.long, o.description)
}
return options
}

// Returns the Usage string for this application.
func getAppUsageString() string {
return colorize.Colorize(fmt.Sprintf(UsageString, getAllCommands(), getAllOptions(), wol.Version))
return colorize.Colorize(fmt.Sprintf(usageString, getAllCommands(), getAllOptions(), wol.Version))
}
22 changes: 12 additions & 10 deletions cmd/wol/wol.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (

////////////////////////////////////////////////////////////////////////////////

const DBPath = "/.config/go-wol/bolt.db"
const (
dbPath = "/.config/go-wol/bolt.db"
)

var (
// Define holders for the cli arguments we wish to parse.
Options struct {
cliFlags struct {
Version bool `short:"v" long:"version"`
Help bool `short:"h" long:"help"`
BroadcastInterface string `short:"i" long:"interface" default:""`
Expand Down Expand Up @@ -93,11 +95,11 @@ func wakeCmd(args []string, aliases *Aliases) error {
}

// Always use the interface specified in the command line, if it exists.
if Options.BroadcastInterface != "" {
bcastInterface = Options.BroadcastInterface
if cliFlags.BroadcastInterface != "" {
bcastInterface = cliFlags.BroadcastInterface
}

err = wol.SendMagicPacket(macAddr, Options.BroadcastIP+":"+Options.UDPPort, bcastInterface)
err = wol.SendMagicPacket(macAddr, cliFlags.BroadcastIP+":"+cliFlags.UDPPort, bcastInterface)
if err != nil {
return err
}
Expand Down Expand Up @@ -144,13 +146,13 @@ func main() {
usr, err := user.Current()
fatalOnError(err)

// Load the list of aliases from the file at DBPath.
aliases, err := LoadAliases(path.Join(usr.HomeDir, DBPath))
// Load the list of aliases from the file at dbPath.
aliases, err := LoadAliases(path.Join(usr.HomeDir, dbPath))
fatalOnError(err)
defer aliases.Close()

// Parse arguments which might get passed to "wol".
parser := flags.NewParser(&Options, flags.Default & ^flags.HelpFlag)
parser := flags.NewParser(&cliFlags, flags.Default & ^flags.HelpFlag)
args, err = parser.Parse()

ec := 0
Expand All @@ -161,11 +163,11 @@ func main() {
ec = printUsageGetExitCode("", 1)

// No arguments, or help requested, print usage.
case len(os.Args) == 1 || Options.Help:
case len(os.Args) == 1 || cliFlags.Help:
ec = printUsageGetExitCode("", 0)

// "--version" requested.
case Options.Version:
case cliFlags.Version:
fmt.Printf("%s\n", wol.Version)

// Make sure we are being asked to run a something.
Expand Down
6 changes: 3 additions & 3 deletions version_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package wol

// WARNING: Auto generated version file. Do not edit this file by hand.
// WARNING: go get github.com/sabhiram/gover to manage this file.
// Version: 1.1.0
// Version: 1.1.1
const (
Major = 1
Minor = 1
Patch = 0
Patch = 1

Version = "1.1.0"
Version = "1.1.1"
)

0 comments on commit b83cc3a

Please sign in to comment.