feat: --ssh/--force for recipe fetch
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
See toolshed/organising#546
This commit is contained in:
parent
b3ab95750e
commit
229e8eb9da
@ -1,11 +1,15 @@
|
|||||||
package recipe
|
package recipe
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"coopcloud.tech/abra/cli/internal"
|
"coopcloud.tech/abra/cli/internal"
|
||||||
"coopcloud.tech/abra/pkg/autocomplete"
|
"coopcloud.tech/abra/pkg/autocomplete"
|
||||||
"coopcloud.tech/abra/pkg/formatter"
|
"coopcloud.tech/abra/pkg/formatter"
|
||||||
"coopcloud.tech/abra/pkg/log"
|
"coopcloud.tech/abra/pkg/log"
|
||||||
"coopcloud.tech/abra/pkg/recipe"
|
"coopcloud.tech/abra/pkg/recipe"
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
gitCfg "github.com/go-git/go-git/v5/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,7 +17,16 @@ var RecipeFetchCommand = &cobra.Command{
|
|||||||
Use: "fetch [recipe | --all] [flags]",
|
Use: "fetch [recipe | --all] [flags]",
|
||||||
Aliases: []string{"f"},
|
Aliases: []string{"f"},
|
||||||
Short: "Clone recipe(s) locally",
|
Short: "Clone recipe(s) locally",
|
||||||
|
Long: `Using "--force/-f" Git syncs an existing recipe. It does not erase unstaged changes.`,
|
||||||
Args: cobra.RangeArgs(0, 1),
|
Args: cobra.RangeArgs(0, 1),
|
||||||
|
Example: ` # fetch from recipe catalogue
|
||||||
|
abra recipe fetch gitea
|
||||||
|
|
||||||
|
# fetch from remote recipe
|
||||||
|
abra recipe fetch git.foo.org/recipes/myrecipe
|
||||||
|
|
||||||
|
# fetch with ssh remote for hacking
|
||||||
|
abra recipe fetch gitea --ssh`,
|
||||||
ValidArgsFunction: func(
|
ValidArgsFunction: func(
|
||||||
cmd *cobra.Command,
|
cmd *cobra.Command,
|
||||||
args []string,
|
args []string,
|
||||||
@ -36,10 +49,39 @@ var RecipeFetchCommand = &cobra.Command{
|
|||||||
|
|
||||||
ensureCtx := internal.GetEnsureContext()
|
ensureCtx := internal.GetEnsureContext()
|
||||||
if recipeName != "" {
|
if recipeName != "" {
|
||||||
r := internal.ValidateRecipe(args, cmd.Name())
|
r := recipe.Get(recipeName)
|
||||||
if err := r.Ensure(ensureCtx); err != nil {
|
if _, err := os.Stat(r.Dir); !os.IsNotExist(err) {
|
||||||
log.Fatal(err)
|
if !force {
|
||||||
|
log.Warnf("%s is already fetched", r.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r = internal.ValidateRecipe(args, cmd.Name())
|
||||||
|
|
||||||
|
if sshRemote {
|
||||||
|
if r.SSHURL == "" {
|
||||||
|
log.Warnf("unable to discover SSH remote for %s", r.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, err := git.PlainOpen(r.Dir)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("unable to open %s: %s", r.Dir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = repo.DeleteRemote("origin"); err != nil {
|
||||||
|
log.Fatalf("unable to remove default remote in %s: %s", r.Dir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := repo.CreateRemote(&gitCfg.RemoteConfig{
|
||||||
|
Name: "origin",
|
||||||
|
URLs: []string{r.SSHURL},
|
||||||
|
}); err != nil {
|
||||||
|
log.Fatalf("unable to set SSH remote in %s: %s", r.Dir, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,6 +103,8 @@ var RecipeFetchCommand = &cobra.Command{
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
fetchAllRecipes bool
|
fetchAllRecipes bool
|
||||||
|
sshRemote bool
|
||||||
|
force bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -71,4 +115,20 @@ func init() {
|
|||||||
false,
|
false,
|
||||||
"fetch all recipes",
|
"fetch all recipes",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
RecipeFetchCommand.Flags().BoolVarP(
|
||||||
|
&sshRemote,
|
||||||
|
"ssh",
|
||||||
|
"s",
|
||||||
|
false,
|
||||||
|
"automatically set ssh remote",
|
||||||
|
)
|
||||||
|
|
||||||
|
RecipeFetchCommand.Flags().BoolVarP(
|
||||||
|
&force,
|
||||||
|
"force",
|
||||||
|
"f",
|
||||||
|
false,
|
||||||
|
"force re-fetch",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,16 @@ setup() {
|
|||||||
_common_setup
|
_common_setup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
teardown(){
|
||||||
|
run rm -rf "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
assert_success
|
||||||
|
assert_not_exists "$ABRA_DIR/recipes/$TEST_RECIPE"
|
||||||
|
|
||||||
|
run rm -rf "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"
|
||||||
|
assert_success
|
||||||
|
assert_not_exists "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"
|
||||||
|
}
|
||||||
|
|
||||||
# bats test_tags=slow
|
# bats test_tags=slow
|
||||||
@test "recipe fetch all" {
|
@test "recipe fetch all" {
|
||||||
run rm -rf "$ABRA_DIR/recipes/matrix-synapse"
|
run rm -rf "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
@ -35,3 +45,81 @@ setup() {
|
|||||||
run $ABRA recipe fetch matrix-synapse --all
|
run $ABRA recipe fetch matrix-synapse --all
|
||||||
assert_failure
|
assert_failure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "do not refetch without --force" {
|
||||||
|
run $ABRA recipe fetch matrix-synapse
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
|
||||||
|
run $ABRA recipe fetch matrix-synapse
|
||||||
|
assert_output --partial "already fetched"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "refetch with --force" {
|
||||||
|
run $ABRA recipe fetch matrix-synapse
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
|
||||||
|
run $ABRA recipe fetch matrix-synapse --force
|
||||||
|
assert_success
|
||||||
|
refute_output --partial "already fetched"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "refetch with --force does not erase unstaged changes" {
|
||||||
|
run $ABRA recipe fetch matrix-synapse
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
|
||||||
|
run bash -c "echo foo >> $ABRA_DIR/recipes/matrix-synapse/foo"
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse/foo"
|
||||||
|
|
||||||
|
run $ABRA recipe fetch matrix-synapse --force
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse/foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "fetch with --ssh" {
|
||||||
|
run $ABRA recipe fetch matrix-synapse --ssh
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
|
||||||
|
run git -C "$ABRA_DIR/recipes/matrix-synapse" remote -v
|
||||||
|
assert_success
|
||||||
|
assert_output --partial "ssh://"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "re-fetch with --ssh/--force" {
|
||||||
|
run $ABRA recipe fetch matrix-synapse
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
|
||||||
|
run git -C "$ABRA_DIR/recipes/matrix-synapse" remote -v
|
||||||
|
assert_success
|
||||||
|
assert_output --partial "https://"
|
||||||
|
|
||||||
|
run $ABRA recipe fetch matrix-synapse --ssh --force
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/matrix-synapse"
|
||||||
|
|
||||||
|
run git -C "$ABRA_DIR/recipes/matrix-synapse" remote -v
|
||||||
|
assert_success
|
||||||
|
assert_output --partial "ssh://"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "fetch remote recipe" {
|
||||||
|
run $ABRA recipe fetch git.coopcloud.tech/coop-cloud/matrix-synapse
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "remote recipe do not refetch without --force" {
|
||||||
|
run $ABRA recipe fetch git.coopcloud.tech/coop-cloud/matrix-synapse
|
||||||
|
assert_success
|
||||||
|
assert_exists "$ABRA_DIR/recipes/git_coopcloud_tech_coop-cloud_matrix-synapse"
|
||||||
|
|
||||||
|
run $ABRA recipe fetch git.coopcloud.tech/coop-cloud/matrix-synapse
|
||||||
|
assert_success
|
||||||
|
assert_output --partial "already fetched"
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user