diff --git a/Makefile b/Makefile index e9597ab0..3349a1b9 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,12 @@ run: install: @go install -ldflags=$(LDFLAGS) $(ABRA) -build-dev: +build-abra: @go build -v -ldflags=$(LDFLAGS) $(ABRA) +build-kadabra: + @go build -v -ldflags=$(LDFLAGS) $(KADABRA) + build: @go build -v -ldflags=$(DIST_LDFLAGS) $(ABRA) @go build -v -ldflags=$(DIST_LDFLAGS) $(KADABRA) diff --git a/cli/app/new.go b/cli/app/new.go index ac8e194c..9034b3bc 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -93,14 +93,14 @@ var appNewCommand = cli.Command{ logrus.Fatal(err) } - cl, err := client.New(internal.NewAppServer) - if err != nil { - logrus.Fatal(err) - } - var secrets AppSecrets var secretTable *jsontable.JSONTable if internal.Secrets { + cl, err := client.New(internal.NewAppServer) + if err != nil { + logrus.Fatal(err) + } + secrets, err := createSecrets(cl, sanitisedAppName) if err != nil { logrus.Fatal(err) diff --git a/cli/cli.go b/cli/cli.go index 51c2342c..b38833f9 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -16,6 +16,7 @@ import ( "coopcloud.tech/abra/cli/server" "coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/config" + "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/web" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -170,10 +171,10 @@ func newAbraApp(version, commit string) *cli.App { app.Before = func(c *cli.Context) error { paths := []string{ config.ABRA_DIR, - path.Join(config.SERVERS_DIR), - path.Join(config.RECIPES_DIR), - path.Join(config.VENDOR_DIR), - path.Join(config.BACKUP_DIR), + config.SERVERS_DIR, + config.RECIPES_DIR, + config.VENDOR_DIR, + config.BACKUP_DIR, } for _, path := range paths { @@ -185,6 +186,14 @@ func newAbraApp(version, commit string) *cli.App { } } + if _, err := os.Stat(config.CATALOGUE_DIR); os.IsNotExist(err) { + url := fmt.Sprintf("%s/%s.git", config.REPOS_BASE_URL, config.CATALOGUE_JSON_REPO_NAME) + logrus.Warnf("local recipe catalogue is missing, retrieving now") + if err := git.Clone(config.CATALOGUE_DIR, url); err != nil { + logrus.Fatal(err) + } + } + logrus.Debugf("abra version %s, commit %s", version, commit) return nil diff --git a/pkg/config/env.go b/pkg/config/env.go index e799f58b..0874fb57 100644 --- a/pkg/config/env.go +++ b/pkg/config/env.go @@ -14,7 +14,16 @@ import ( "github.com/sirupsen/logrus" ) -var ABRA_DIR = os.ExpandEnv("$HOME/.abra") +// getBaseDir retrieves the Abra base directory. +func getBaseDir() string { + home := os.ExpandEnv("$HOME/.abra") + if customAbraDir, exists := os.LookupEnv("ABRA_DIR"); exists && customAbraDir != "" { + home = customAbraDir + } + return home +} + +var ABRA_DIR = getBaseDir() var SERVERS_DIR = path.Join(ABRA_DIR, "servers") var RECIPES_DIR = path.Join(ABRA_DIR, "recipes") var VENDOR_DIR = path.Join(ABRA_DIR, "vendor") diff --git a/tests/integration/dirs.bats b/tests/integration/dirs.bats new file mode 100644 index 00000000..0ba86678 --- /dev/null +++ b/tests/integration/dirs.bats @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +setup() { + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + source "$DIR/helpers.sh" + _setup_env +} + +@test "ABRA_DIR can be overriden" { + ABRA_DIR="$HOME/.abra_foo" + + run $ABRA app ls + + # no servers yet, so will fail. however, it will run the required code which + # checks if it should create these base directories and that is what we want + assert_failure + + assert [ -d "$HOME/.abra_foo" ] +} + +@test "abra directories created" { + run $ABRA app ls + + # no servers yet, so will fail. however, it will run the required code which + # checks if it should create these base directories and that is what we want + assert_failure + + assert [ -d "$ABRA_DIR" ] + assert [ -d "$ABRA_DIR/servers" ] + assert [ -d "$ABRA_DIR/recipes" ] + assert [ -d "$ABRA_DIR/backups" ] + assert [ -d "$ABRA_DIR/vendor" ] + assert [ -d "$ABRA_DIR/catalogue" ] +} + +@test "catalogue recipe is a git repository" { + run $ABRA app ls + + # no servers yet, so will fail. however, it will run the required code which + # checks if it should create these base directories and that is what we want + assert_failure + + assert_output --partial 'local recipe catalogue is missing' + + assert [ -d "$ABRA_DIR/catalogue" ] + assert [ -d "$ABRA_DIR/catalogue/.git" ] +} + +teardown(){ + _default_teardown +} diff --git a/tests/integration/helpers.sh b/tests/integration/helpers.sh new file mode 100644 index 00000000..f3aa6ead --- /dev/null +++ b/tests/integration/helpers.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +_build_abra() { + if [[ ! -e "$ROOT/abra" ]]; then + cd "$ROOT" && make build-abra + return 0 + fi + return 0 +} + +_build_kadabra() { + if [[ ! -e "$ROOT/kadabra" ]]; then + cd "$ROOT" && make build-kadabra + return 0 + fi + return 0 +} + +_setup_env(){ + load '/usr/lib/bats/bats-support/load' + load '/usr/lib/bats/bats-assert/load' + + ROOT="$DIR/../.." + ABRA="$ROOT/abra" + KADABRA="$ROOT/kadabra" + + ABRA_DIR="$HOME/.abra_test" + + _build_abra + _build_kadabra +} + +_default_teardown(){ + rm -rf "$ABRA_DIR" +} diff --git a/tests/integration/recipe.bats b/tests/integration/recipe.bats new file mode 100644 index 00000000..c8d1bcb9 --- /dev/null +++ b/tests/integration/recipe.bats @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +setup() { + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + source "$DIR/helpers.sh" + _setup_env + + mkdir -p "$HOME/.abra_test/servers/example.com" + if ! grep -R -q "example.com" "$HOME/.docker"; then + docker context create --docker host=ssh://foo@example.com:222 example.com + fi +} + +@test "create new recipe" { + run $ABRA recipe new foobar + assert_success + assert_output --partial 'Your new foobar recipe has been created' + + run $ABRA app new foobar \ + --no-input \ + --server example.com \ + --domain foobar.example.com + assert_success + assert_output --partial 'A new foobar app has been created!' +} + +teardown() { + _default_teardown + + if grep -R -q "example.com" "$HOME/.docker"; then + docker context rm example.com + fi +}