~egtann/inv2ips

84b5456acf59d14a3be86aff226d033582e693a6 — Evan Tann 2 months ago 0bc9c44 master
update cmd syntax, add man pages, install instructions
5 files changed, 109 insertions(+), 13 deletions(-)

A Makefile
M README.md
M main.go
A man/man1/inv2ips.1
A man/man5/inventory.json.5
A Makefile => Makefile +18 -0
@@ 0,0 1,18 @@
GOPATH?=$(USER)/go
BINDIR?=$(GOPATH)/bin
MANDIR?=/usr/local/man

$(BINDIR)/inv2ips:
	go get -u egt.run/inv2ips

install: $(BINDIR)/inv2ips
	mkdir -m755 -p $(MANDIR)/man1 $(MANDIR)/man5
	cp man/man1/inv2ips.1 $(MANDIR)/man1/
	cp man/man5/inventory.json.5 $(MANDIR)/man5/
.PHONY: install

uninstall:
	rm -f $(BINDIR)/inv2ips
	rm -f $(MANDIR)/man1/inv2ips.1
	rm -f $(MANDIR)/man5/inventory.json.5
.PHONY: uninstall

M README.md => README.md +17 -6
@@ 1,14 1,18 @@
# inventory2ips
# inv2ips(1)

inventory2ips is a small utility that outputs CSV IP addresses for a given
service name.
inv2ips(1) is a small utility that outputs CSV IP addresses for a given service
name.

## Install

```
$ go get egt.run/inventory2ips
$ git clone git@git.sr.ht:~egtann/inv2ips
$ sudo make install
```

Check the man pages for inv2ips(1) and inventory.json(5) for detailed usage
information and examples.

## Usage

Given an inventory.json file that looks like this:


@@ 20,7 24,7 @@ Given an inventory.json file that looks like this:
```

```bash
$ inventory2ips -s my-app
$ inv2ips my-app
10.128.0.1,10.128.0.2
```



@@ 29,5 33,12 @@ flags using their service names, such as appending them to a config file during
deploy:

```bash
$ echo "IPS=$(inventory2ips -s inventory)" >> config.env
$ echo "IPS=$(inv2ips -p 3000 inventory)" >> config.env
```

It may also be useful when combined with Up to deploy services or environments
by name:

```bash
$ up -t $(inv2ips production) deploy
```

M main.go => main.go +18 -7
@@ 11,7 11,10 @@ import (

func main() {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		if err.Error() != "" {
			fmt.Fprintln(os.Stderr, err)
		}
		fmt.Fprintln(os.Stderr, usage())
		os.Exit(1)
	}
}


@@ 19,14 22,20 @@ func main() {
func run() error {
	// Read an inventory file and an SRP config file
	var (
		invFile     = flag.String("i", "inventory.json", "inventory file")
		serviceName = flag.String("s", "", "service name")
		port        = flag.Int("p", 0, "port (optional)")
		invFile = flag.String("f", "inventory.json", "inventory file")
		port    = flag.Int("p", 0, "port (optional)")
	)
	flag.Parse()

	if *serviceName == "" {
		return errors.New("service name [-s] is required")
	var serviceName string
	args := flag.Args()
	switch len(args) {
	case 0:
		return errors.New("")
	case 1:
		serviceName = args[0]
	default:
		return errors.New("unknown args")
	}
	inventory, err := parseInventory(*invFile)
	if err != nil {


@@ 35,7 44,7 @@ func run() error {
	var ips []string
	for ip, serviceNames := range inventory {
		for _, name := range serviceNames {
			if name == *serviceName {
			if name == serviceName {
				if *port > 0 {
					ip = fmt.Sprintf("%s:%d", ip, *port)
				}


@@ 72,3 81,5 @@ func parseInventory(filename string) (inventory, error) {
	}
	return inv, nil
}

func usage() string { return "usage: inv2ips [-f file] [-p port] service" }

A man/man1/inv2ips.1 => man/man1/inv2ips.1 +36 -0
@@ 0,0 1,36 @@
.Dd $Mdocdate$
.Dt INV2IPS 1
.Os
.Sh NAME
.Nm inv2ips
.Nd inventory to ips converter
.Sh SYNOPSIS
.Nm inv2ips
.Bk -words
.Op Fl f Ar file
.Op Fl p Ar port
.Cm service
.Ek
.Sh DESCRIPTION
The
.Nm
utility parses an
.Xr inventory.json 5
file and outputs the IP addresses associated with a name.
.Pp
The options are as follows:
.Pp
.Bl -tag -width xxxxxxx -compact
.It Fl f Ar file
Path to the
.Xr inventory.json 5
file (default "inventory.json")
.It Fl h
Help.
.It Fl p Ar port
Port to append to each IP address.
.El
.Sh EXIT STATUS
.Ex -std
.Sh AUTHORS
.An Evan Tann Aq Mt os@evantann.com

A man/man5/inventory.json.5 => man/man5/inventory.json.5 +20 -0
@@ 0,0 1,20 @@
.Dd $Mdocdate$
.Dt INVENTORY.JSON 5
.Os
.Sh NAME
.Nm inventory.json
.Nd inventory file
.Sh DESCRIPTION
The
.Nm
file associates IP addresses with service names. It has the following format:
.Bd -literal -offset indent
\(lC\*qip\*q: \(lB\*qservice\*q\(rB\(rC
.Ed
.Pp
The same service may be defined across several IPs to associate these IPs
together under a common name, such as the environment (e.g. production,
staging) or the type of service (e.g. dashboard, www).
.Sh SEE ALSO
.Xr inv2ips 1
.Xr inv2config 1