Skip to content

Commit

Permalink
add boilerplate in prep for impl and refactor build scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
UmanShahzad committed May 25, 2021
1 parent 63839c5 commit f6bc145
Show file tree
Hide file tree
Showing 33 changed files with 492 additions and 172 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.vim/
ipinfo/dist/
grepip/dist/
cidr2range/dist/
range2cidr/dist/
!ipinfo/dist/DEBIAN/
!grepip/dist/DEBIAN/
!cidr2range/dist/DEBIAN/
!range2cidr/dist/DEBIAN/
12 changes: 12 additions & 0 deletions cidr2range/build-all-platforms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Build binary for all platforms for version $1.

set -e

DIR=`dirname $0`
ROOT=$DIR/..

VSN=$1

$ROOT/scripts/build-all-platforms.sh "cidr2range" $VSN
10 changes: 10 additions & 0 deletions cidr2range/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Build local binary.

set -e

DIR=`dirname $0`
ROOT=$DIR/..

$ROOT/scripts/build.sh "cidr2range"
12 changes: 12 additions & 0 deletions cidr2range/completions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/complete"
)

var completions = lib.CompletionsCIDR2Range

func handleCompletions() {
completions.Complete(progBase)
}
14 changes: 14 additions & 0 deletions cidr2range/deb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

VSN=1.0.0

curl -LO https://github.com/ipinfo/cli/releases/download/cidr2range-${VSN}/cidr2range_${VSN}.deb
sudo dpkg -i cidr2range_${VSN}.deb
rm cidr2range_${VSN}.deb

echo
echo 'You can now run `cidr2range`'.

if [ -f "$0" ]; then
rm $0
fi
11 changes: 11 additions & 0 deletions cidr2range/dist/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Source: cidr2range
Section: utils
Version: 1.0.0
Priority: optional
Maintainer: IPinfo <support@ipinfo.io>
Vcs-Git: https://github.com/ipinfo/cli
Vcs-browser: https://github.com/ipinfo/cli
Homepage: https://ipinfo.io
Package: cidr2range
Architecture: amd64
Description: A simple tool for converting from CIDRs to IP ranges.
16 changes: 16 additions & 0 deletions cidr2range/macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

VSN=1.0.0
PLAT=darwin_amd64

curl -LO https://github.com/ipinfo/cli/releases/download/cidr2range-${VSN}/cidr2range_${VSN}_${PLAT}.tar.gz
tar -xf cidr2range_${VSN}_${PLAT}.tar.gz
rm cidr2range_${VSN}_${PLAT}.tar.gz
mv cidr2range_${VSN}_${PLAT} /usr/local/bin/cidr2range

echo
echo 'You can now run `cidr2range`'.

if [ -f "$0" ]; then
rm $0
fi
55 changes: 55 additions & 0 deletions cidr2range/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/ipinfo/cli/lib"
"github.com/spf13/pflag"
)

var progBase = filepath.Base(os.Args[0])
var version = "1.0.0"

func printHelp() {
fmt.Printf(
`Usage: %s [<opts>]
Options:
--version, -v
show binary release number.
--help, -h
show help.
`, progBase)
}

func cmd() error {
var fVsn bool

f := lib.CmdCIDR2RangeFlags{}
f.Init()
pflag.BoolVarP(&fVsn, "version", "v", false, "print binary release number.")
pflag.Parse()

if fVsn {
fmt.Println(version)
return nil
}

return lib.CmdCIDR2Range(f, pflag.Args(), printHelp)
}

func main() {
// obey NO_COLOR env var.
if os.Getenv("NO_COLOR") != "" {
color.NoColor = true
}

handleCompletions()

if err := cmd(); err != nil {
fmt.Printf("err: %v\n", err)
}
}
12 changes: 12 additions & 0 deletions cidr2range/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# Build and upload (to GitHub) for all platforms for version $1.

set -e

DIR=`dirname $0`
ROOT=$DIR/..

VSN=$1

$ROOT/scripts/release.sh "cidr2range" $VSN
48 changes: 1 addition & 47 deletions grepip/build-all-platforms.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,4 @@ ROOT=$DIR/..

VSN=$1

if [ -z "$VSN" ]; then
echo "require version as first parameter" 2>&1
exit 1
fi

for t in \
darwin_amd64 \
darwin_arm64 \
dragonfly_amd64 \
freebsd_386 \
freebsd_amd64 \
freebsd_arm \
freebsd_arm64 \
linux_386 \
linux_amd64 \
linux_arm \
linux_arm64 \
netbsd_386 \
netbsd_amd64 \
netbsd_arm \
netbsd_arm64 \
openbsd_386 \
openbsd_amd64 \
openbsd_arm \
openbsd_arm64 \
plan9_386 \
plan9_amd64 \
plan9_arm \
solaris_amd64 \
windows_386 \
windows_amd64 \
windows_arm ;
do
os="${t%_*}"
arch="${t#*_}"
output="grepip_${VSN}_${os}_${arch}"

if [ "$os" == "windows" ] ; then
output+=".exe"
fi

GOOS=$os GOARCH=$arch go build \
-o $ROOT/build/${output} \
$ROOT/grepip &
done

wait
$ROOT/scripts/build-all-platforms.sh "grepip" $VSN
6 changes: 3 additions & 3 deletions grepip/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

# Build local binary.

set -e

DIR=`dirname $0`
ROOT=$DIR/..

go build \
-o $ROOT/build/grepip \
$ROOT/grepip
$ROOT/scripts/build.sh "grepip"
1 change: 1 addition & 0 deletions grepip/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/ipinfo/cli/lib"
"github.com/ipinfo/cli/lib/complete"
)

var completions = lib.CompletionsGrepIP
Expand Down
4 changes: 3 additions & 1 deletion grepip/deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ rm grepip_${VSN}.deb
echo
echo 'You can now run `grepip`'.

rm $0
if [ -f "$0" ]; then
rm $0
fi
4 changes: 3 additions & 1 deletion grepip/macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ mv grepip_${VSN}_${PLAT} /usr/local/bin/grepip
echo
echo 'You can now run `grepip`'.

rm $0
if [ -f "$0" ]; then
rm $0
fi
36 changes: 1 addition & 35 deletions grepip/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,4 @@ ROOT=$DIR/..

VSN=$1

if [ -z "$VSN" ]; then
echo "require version as first parameter" 2>&1
exit 1
fi

# build
rm -f $ROOT/build/grepip_${VSN}*
$ROOT/grepip/build-all-platforms.sh "$VSN"

# archive
cd $ROOT/build
for t in grepip_${VSN}_* ; do
if [[ $t == grepip_*_windows_* ]]; then
zip -q ${t/.exe/.zip} $t
else
tar -czf ${t}.tar.gz $t
fi
done
cd ..

# dist: debian
rm -rf $ROOT/grepip/dist/usr
mkdir -p $ROOT/grepip/dist/usr/local/bin
cp $ROOT/build/grepip_${VSN}_linux_amd64 $ROOT/grepip/dist/usr/local/bin/grepip
dpkg-deb --build ${ROOT}/grepip/dist build/grepip_${VSN}.deb

# release
gh release create grepip-${VSN} \
-R ipinfo/cli \
-t "grepip-${VSN}" \
$ROOT/build/grepip_*.tar.gz \
$ROOT/build/grepip_*.zip \
$ROOT/build/grepip_*.deb \
$ROOT/grepip/macos.sh \
$ROOT/grepip/deb.sh
$ROOT/scripts/release.sh "grepip" $VSN
48 changes: 1 addition & 47 deletions ipinfo/build-all-platforms.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,4 @@ ROOT=$DIR/..

VSN=$1

if [ -z "$VSN" ]; then
echo "require version as first parameter" 2>&1
exit 1
fi

for t in \
darwin_amd64 \
darwin_arm64 \
dragonfly_amd64 \
freebsd_386 \
freebsd_amd64 \
freebsd_arm \
freebsd_arm64 \
linux_386 \
linux_amd64 \
linux_arm \
linux_arm64 \
netbsd_386 \
netbsd_amd64 \
netbsd_arm \
netbsd_arm64 \
openbsd_386 \
openbsd_amd64 \
openbsd_arm \
openbsd_arm64 \
plan9_386 \
plan9_amd64 \
plan9_arm \
solaris_amd64 \
windows_386 \
windows_amd64 \
windows_arm ;
do
os="${t%_*}"
arch="${t#*_}"
output="ipinfo_${VSN}_${os}_${arch}"

if [ "$os" == "windows" ] ; then
output+=".exe"
fi

GOOS=$os GOARCH=$arch go build \
-o $ROOT/build/${output} \
$ROOT/ipinfo &
done

wait
$ROOT/scripts/build-all-platforms.sh "ipinfo" $VSN
6 changes: 3 additions & 3 deletions ipinfo/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

# Build local binary.

set -e

DIR=`dirname $0`
ROOT=$DIR/..

go build \
-o $ROOT/build/ipinfo \
$ROOT/ipinfo
$ROOT/scripts/build.sh "ipinfo"
26 changes: 26 additions & 0 deletions ipinfo/cmd_cidr2range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"

"github.com/ipinfo/cli/lib"
"github.com/spf13/pflag"
)

func printHelpCIDR2Range() {
fmt.Printf(
`Usage: %s cidr2range [<opts>]
Options:
--help, -h
show help.
`, progBase)
}

func cmdCIDR2Range() (err error) {
f := lib.CmdCIDR2RangeFlags{}
f.Init()
pflag.Parse()

return lib.CmdCIDR2Range(f, pflag.Args()[1:], printHelpCIDR2Range)
}
2 changes: 2 additions & 0 deletions ipinfo/cmd_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Commands:
map open a URL to a map showing the locations of a group of IPs.
prips print IP list from CIDR or range.
grepip grep for IPs matching criteria from any source.
cidr2range convert CIDRs to IP ranges.
range2cidr convert IP ranges to CIDRs.
login save an API token session.
logout delete your current API token session.
version show current version.
Expand Down
Loading

0 comments on commit f6bc145

Please sign in to comment.