Skip to content

Commit 1c5a8d0

Browse files
committed
Adding basic CLI infrastructure
1 parent d31e082 commit 1c5a8d0

File tree

9 files changed

+248
-0
lines changed

9 files changed

+248
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ _testmain.go
2121

2222
*.exe
2323
*.test
24+
bin/

command/version.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package command
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/mitchellh/cli"
7+
)
8+
9+
// VersionCommand is a Command implementation prints the version.
10+
type VersionCommand struct {
11+
Revision string
12+
Version string
13+
VersionPrerelease string
14+
Ui cli.Ui
15+
}
16+
17+
func (c *VersionCommand) Help() string {
18+
return ""
19+
}
20+
21+
func (c *VersionCommand) Run(_ []string) int {
22+
var versionString bytes.Buffer
23+
fmt.Fprintf(&versionString, "Consul v%s", c.Version)
24+
if c.VersionPrerelease != "" {
25+
fmt.Fprintf(&versionString, ".%s", c.VersionPrerelease)
26+
27+
if c.Revision != "" {
28+
fmt.Fprintf(&versionString, " (%s)", c.Revision)
29+
}
30+
}
31+
32+
c.Ui.Output(versionString.String())
33+
return 0
34+
}
35+
36+
func (c *VersionCommand) Synopsis() string {
37+
return "Prints the Consul version"
38+
}

command/version_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package command
2+
3+
import (
4+
"github.com/mitchellh/cli"
5+
"testing"
6+
)
7+
8+
func TestVersionCommand_implements(t *testing.T) {
9+
var _ cli.Command = &VersionCommand{}
10+
}

commands.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"github.com/hashicorp/consul/command"
5+
"github.com/mitchellh/cli"
6+
"os"
7+
"os/signal"
8+
)
9+
10+
// Commands is the mapping of all the available Serf commands.
11+
var Commands map[string]cli.CommandFactory
12+
13+
func init() {
14+
ui := &cli.BasicUi{Writer: os.Stdout}
15+
16+
Commands = map[string]cli.CommandFactory{
17+
"version": func() (cli.Command, error) {
18+
return &command.VersionCommand{
19+
Revision: GitCommit,
20+
Version: Version,
21+
VersionPrerelease: VersionPrerelease,
22+
Ui: ui,
23+
}, nil
24+
},
25+
}
26+
}
27+
28+
// makeShutdownCh returns a channel that can be used for shutdown
29+
// notifications for commands. This channel will send a message for every
30+
// interrupt received.
31+
func makeShutdownCh() <-chan struct{} {
32+
resultCh := make(chan struct{})
33+
34+
signalCh := make(chan os.Signal, 4)
35+
signal.Notify(signalCh, os.Interrupt)
36+
go func() {
37+
for {
38+
<-signalCh
39+
resultCh <- struct{}{}
40+
}
41+
}()
42+
43+
return resultCh
44+
}

main.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/mitchellh/cli"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
)
10+
11+
func main() {
12+
os.Exit(realMain())
13+
}
14+
15+
func realMain() int {
16+
log.SetOutput(ioutil.Discard)
17+
18+
// Get the command line args. We shortcut "--version" and "-v" to
19+
// just show the version.
20+
args := os.Args[1:]
21+
for _, arg := range args {
22+
if arg == "-v" || arg == "--version" {
23+
newArgs := make([]string, len(args)+1)
24+
newArgs[0] = "version"
25+
copy(newArgs[1:], args)
26+
args = newArgs
27+
break
28+
}
29+
}
30+
31+
cli := &cli.CLI{
32+
Args: args,
33+
Commands: Commands,
34+
}
35+
36+
exitCode, err := cli.Run()
37+
if err != nil {
38+
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
39+
return 1
40+
}
41+
42+
return exitCode
43+
}

main_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main

scripts/build.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
#
3+
# This script builds the application from source.
4+
set -e
5+
6+
# Get the parent directory of where this script is.
7+
SOURCE="${BASH_SOURCE[0]}"
8+
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
9+
DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
10+
11+
# Change into that directory
12+
cd $DIR
13+
14+
# Get the git commit
15+
GIT_COMMIT=$(git rev-parse HEAD)
16+
GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
17+
18+
# If we're building on Windows, specify an extension
19+
EXTENSION=""
20+
if [ "$(go env GOOS)" = "windows" ]; then
21+
EXTENSION=".exe"
22+
fi
23+
24+
# Install dependencies
25+
echo "--> Installing dependencies to speed up builds..."
26+
go get ./...
27+
28+
# Build!
29+
echo "--> Building..."
30+
go build \
31+
-ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \
32+
-v \
33+
-o bin/consul${EXTENSION}
34+
cp bin/consul${EXTENSION} $GOPATH/bin

scripts/dist.sh

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Get the parent directory of where this script is.
5+
SOURCE="${BASH_SOURCE[0]}"
6+
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
7+
DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
8+
9+
# Change into that dir because we expect that
10+
cd $DIR
11+
12+
# Determine the version that we're building based on the contents
13+
# of packer/version.go.
14+
VERSION=$(grep "const Version " version.go | sed -E 's/.*"(.+)"$/\1/')
15+
VERSIONDIR="${VERSION}"
16+
PREVERSION=$(grep "const VersionPrerelease " version.go | sed -E 's/.*"(.*)"$/\1/')
17+
if [ ! -z $PREVERSION ]; then
18+
PREVERSION="${PREVERSION}.$(date -u +%s)"
19+
VERSIONDIR="${VERSIONDIR}-${PREVERSION}"
20+
fi
21+
22+
echo "Version: ${VERSION} ${PREVERSION}"
23+
24+
# Determine the arch/os combos we're building for
25+
XC_ARCH=${XC_ARCH:-"386 amd64 arm"}
26+
XC_OS=${XC_OS:-linux darwin windows freebsd openbsd}
27+
28+
echo "Arch: ${XC_ARCH}"
29+
echo "OS: ${XC_OS}"
30+
31+
# Make sure that if we're killed, we kill all our subprocseses
32+
trap "kill 0" SIGINT SIGTERM EXIT
33+
34+
# This function builds whatever directory we're in...
35+
goxc \
36+
-arch="$XC_ARCH" \
37+
-os="$XC_OS" \
38+
-d="${DIR}/pkg" \
39+
-pv="${VERSION}" \
40+
-pr="${PREVERSION}" \
41+
$XC_OPTS \
42+
go-install \
43+
xc
44+
45+
# Zip all the packages
46+
mkdir -p ./pkg/${VERSIONDIR}/dist
47+
for PLATFORM in $(find ./pkg/${VERSIONDIR} -mindepth 1 -maxdepth 1 -type d); do
48+
PLATFORM_NAME=$(basename ${PLATFORM})
49+
ARCHIVE_NAME="${VERSIONDIR}_${PLATFORM_NAME}"
50+
51+
if [ $PLATFORM_NAME = "dist" ]; then
52+
continue
53+
fi
54+
55+
pushd ${PLATFORM}
56+
zip ${DIR}/pkg/${VERSIONDIR}/dist/${ARCHIVE_NAME}.zip ./*
57+
popd
58+
done
59+
60+
# Make the checksums
61+
pushd ./pkg/${VERSIONDIR}/dist
62+
shasum -a256 * > ./${VERSIONDIR}_SHA256SUMS
63+
popd
64+
65+
exit 0

version.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
// The git commit that was compiled. This will be filled in by the compiler.
4+
var GitCommit string
5+
6+
// The main version number that is being run at the moment.
7+
const Version = "0.1.0"
8+
9+
// A pre-release marker for the version. If this is "" (empty string)
10+
// then it means that it is a final release. Otherwise, this is a pre-release
11+
// such as "dev" (in development), "beta", "rc1", etc.
12+
const VersionPrerelease = "dev"

0 commit comments

Comments
 (0)