feat: support force to overwrite existing project with --force/-f flag in hrp startproject

This commit is contained in:
debugtalk
2022-04-24 21:19:48 +08:00
parent 85476f27f2
commit ce60dba5f9
12 changed files with 32 additions and 26 deletions
+1 -1
View File
@@ -36,4 +36,4 @@ Copyright 2017 debugtalk
* [hrp run](hrp_run.md) - run API test with go engine * [hrp run](hrp_run.md) - run API test with go engine
* [hrp startproject](hrp_startproject.md) - create a scaffold project * [hrp startproject](hrp_startproject.md) - create a scaffold project
###### Auto generated by spf13/cobra on 22-Apr-2022 ###### Auto generated by spf13/cobra on 24-Apr-2022
+1 -1
View File
@@ -41,4 +41,4 @@ hrp boom [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [hrp](hrp.md) - Next-Generation API Testing Solution.
###### Auto generated by spf13/cobra on 22-Apr-2022 ###### Auto generated by spf13/cobra on 24-Apr-2022
+1 -1
View File
@@ -18,4 +18,4 @@ hrp convert $path... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [hrp](hrp.md) - Next-Generation API Testing Solution.
###### Auto generated by spf13/cobra on 22-Apr-2022 ###### Auto generated by spf13/cobra on 24-Apr-2022
+1 -1
View File
@@ -24,4 +24,4 @@ hrp har2case $har_path... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [hrp](hrp.md) - Next-Generation API Testing Solution.
###### Auto generated by spf13/cobra on 22-Apr-2022 ###### Auto generated by spf13/cobra on 24-Apr-2022
+1 -1
View File
@@ -16,4 +16,4 @@ hrp pytest $path ... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [hrp](hrp.md) - Next-Generation API Testing Solution.
###### Auto generated by spf13/cobra on 22-Apr-2022 ###### Auto generated by spf13/cobra on 24-Apr-2022
+1 -1
View File
@@ -34,4 +34,4 @@ hrp run $path... [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [hrp](hrp.md) - Next-Generation API Testing Solution.
###### Auto generated by spf13/cobra on 22-Apr-2022 ###### Auto generated by spf13/cobra on 24-Apr-2022
+2 -1
View File
@@ -9,6 +9,7 @@ hrp startproject $project_name [flags]
### Options ### Options
``` ```
-f, --force force to overwrite existing project
--go generate hashicorp go plugin --go generate hashicorp go plugin
-h, --help help for startproject -h, --help help for startproject
--ignore-plugin ignore function plugin --ignore-plugin ignore function plugin
@@ -19,4 +20,4 @@ hrp startproject $project_name [flags]
* [hrp](hrp.md) - Next-Generation API Testing Solution. * [hrp](hrp.md) - Next-Generation API Testing Solution.
###### Auto generated by spf13/cobra on 22-Apr-2022 ###### Auto generated by spf13/cobra on 24-Apr-2022
+1 -1
View File
@@ -2,4 +2,4 @@ module plugin
go 1.16 go 1.16
require github.com/httprunner/funplugin v0.4.2 // indirect require github.com/httprunner/funplugin v0.4.3 // indirect
+2 -2
View File
@@ -58,8 +58,8 @@ github.com/hashicorp/go-plugin v1.4.3 h1:DXmvivbWD5qdiBts9TpBC7BYL1Aia5sxbRgQB+v
github.com/hashicorp/go-plugin v1.4.3/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ= github.com/hashicorp/go-plugin v1.4.3/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/httprunner/funplugin v0.4.2 h1:iDeg3GVCKdimgZQ40xq0kxHqhL/DQmRxs3DRjzOpUuo= github.com/httprunner/funplugin v0.4.3 h1:mxdxQh54NZLQnK/FXZxpZV0rhqZQzckrWKEnBW5w2Vg=
github.com/httprunner/funplugin v0.4.2/go.mod h1:vPyeJIfbpGe0epZZtAV0wCn16gLY9+imSw/zfxq0Lcc= github.com/httprunner/funplugin v0.4.3/go.mod h1:vPyeJIfbpGe0epZZtAV0wCn16gLY9+imSw/zfxq0Lcc=
github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
+3 -1
View File
@@ -31,7 +31,7 @@ var scaffoldCmd = &cobra.Command{
pluginType = scaffold.Py // default pluginType = scaffold.Py // default
} }
err := scaffold.CreateScaffold(args[0], pluginType) err := scaffold.CreateScaffold(args[0], pluginType, force)
if err != nil { if err != nil {
log.Error().Err(err).Msg("create scaffold project failed") log.Error().Err(err).Msg("create scaffold project failed")
os.Exit(1) os.Exit(1)
@@ -45,10 +45,12 @@ var (
ignorePlugin bool ignorePlugin bool
genPythonPlugin bool genPythonPlugin bool
genGoPlugin bool genGoPlugin bool
force bool
) )
func init() { func init() {
rootCmd.AddCommand(scaffoldCmd) rootCmd.AddCommand(scaffoldCmd)
scaffoldCmd.Flags().BoolVarP(&force, "force", "f", false, "force to overwrite existing project")
scaffoldCmd.Flags().BoolVar(&genPythonPlugin, "py", true, "generate hashicorp python plugin") scaffoldCmd.Flags().BoolVar(&genPythonPlugin, "py", true, "generate hashicorp python plugin")
scaffoldCmd.Flags().BoolVar(&genGoPlugin, "go", false, "generate hashicorp go plugin") scaffoldCmd.Flags().BoolVar(&genGoPlugin, "go", false, "generate hashicorp go plugin")
scaffoldCmd.Flags().BoolVar(&ignorePlugin, "ignore-plugin", false, "ignore function plugin") scaffoldCmd.Flags().BoolVar(&ignorePlugin, "ignore-plugin", false, "ignore function plugin")
+3 -7
View File
@@ -1,28 +1,24 @@
package scaffold package scaffold
import ( import (
"os"
"testing" "testing"
) )
func TestGenDemoExamples(t *testing.T) { func TestGenDemoExamples(t *testing.T) {
dir := "../../../examples/demo-with-go-plugin" dir := "../../../examples/demo-with-go-plugin"
os.RemoveAll(dir) err := CreateScaffold(dir, Go, true)
err := CreateScaffold(dir, Go)
if err != nil { if err != nil {
t.Fatal() t.Fatal()
} }
dir = "../../../examples/demo-with-py-plugin" dir = "../../../examples/demo-with-py-plugin"
os.RemoveAll(dir) err = CreateScaffold(dir, Py, true)
err = CreateScaffold(dir, Py)
if err != nil { if err != nil {
t.Fatal() t.Fatal()
} }
dir = "../../../examples/demo-without-plugin" dir = "../../../examples/demo-without-plugin"
os.RemoveAll(dir) err = CreateScaffold(dir, Ignore, true)
err = CreateScaffold(dir, Ignore)
if err != nil { if err != nil {
t.Fatal() t.Fatal()
} }
+12 -5
View File
@@ -42,24 +42,31 @@ func CopyFile(templateFile, targetFile string) error {
return nil return nil
} }
func CreateScaffold(projectName string, pluginType PluginType) error { func CreateScaffold(projectName string, pluginType PluginType, force bool) error {
// report event // report event
sdk.SendEvent(sdk.EventTracking{ sdk.SendEvent(sdk.EventTracking{
Category: "Scaffold", Category: "Scaffold",
Action: "hrp startproject", Action: "hrp startproject",
}) })
log.Info().
Str("projectName", projectName).
Str("pluginType", string(pluginType)).
Bool("force", force).
Msg("create new scaffold project")
// check if projectName exists // check if projectName exists
if _, err := os.Stat(projectName); err == nil { if _, err := os.Stat(projectName); err == nil {
if !force {
log.Warn().Str("projectName", projectName). log.Warn().Str("projectName", projectName).
Msg("project name already exists, please specify a new one.") Msg("project name already exists, please specify a new one.")
return fmt.Errorf("project name already exists") return fmt.Errorf("project name already exists")
} }
log.Info(). log.Warn().Str("projectName", projectName).
Str("projectName", projectName). Msg("project name already exists, remove first !!!")
Str("pluginType", string(pluginType)). os.RemoveAll(projectName)
Msg("create new scaffold project") }
// create project folders // create project folders
if err := builtin.CreateFolder(projectName); err != nil { if err := builtin.CreateFolder(projectName); err != nil {