refactor: open hz code for being called

This commit is contained in:
fgy 2022-11-17 19:05:41 +08:00
parent d6b85fec12
commit 277a7384a8
62 changed files with 121 additions and 103 deletions

View File

@ -9,6 +9,6 @@ header:
paths-ignore:
- pkg/common/testdata/**
- cmd/hz/internal/protobuf/api
- cmd/hz/protobuf/api
comment: on-failure

View File

@ -19,13 +19,17 @@ package app
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/config"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/config"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/protobuf"
"github.com/cloudwego/hertz/cmd/hz/thrift"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
"github.com/urfave/cli/v2"
)
@ -40,12 +44,12 @@ func New(c *cli.Context) error {
setLogVerbose(args.Verbose)
logs.Debugf("args: %#v\n", args)
err = generateLayout(args)
err = GenerateLayout(args)
if err != nil {
return cli.Exit(err, meta.GenerateLayoutError)
}
err = triggerPlugin(args)
err = TriggerPlugin(args)
if err != nil {
return cli.Exit(err, meta.PluginError)
}
@ -67,7 +71,7 @@ func Update(c *cli.Context) error {
setLogVerbose(args.Verbose)
logs.Debugf("Args: %#v\n", args)
err = triggerPlugin(args)
err = TriggerPlugin(args)
if err != nil {
return cli.Exit(err, meta.PluginError)
}
@ -88,7 +92,7 @@ func Model(c *cli.Context) error {
setLogVerbose(args.Verbose)
logs.Debugf("Args: %#v\n", args)
err = triggerPlugin(args)
err = TriggerPlugin(args)
if err != nil {
return cli.Exit(err, meta.PluginError)
}
@ -96,6 +100,21 @@ func Model(c *cli.Context) error {
return nil
}
func PluginMode() {
pluginName := filepath.Base(os.Args[0])
if util.IsWindows() {
pluginName = strings.TrimSuffix(pluginName, ".exe")
}
switch pluginName {
case meta.ThriftPluginName:
plugin := new(thrift.Plugin)
os.Exit(plugin.Run())
case meta.ProtocPluginName:
plugin := new(protobuf.Plugin)
os.Exit(plugin.Run())
}
}
func Init() *cli.App {
// flags
verboseFlag := cli.BoolFlag{Name: "verbose,vv", Usage: "turn on verbose mode", Destination: &globalArgs.Verbose}
@ -233,7 +252,7 @@ func setLogVerbose(verbose bool) {
}
}
func generateLayout(args *config.Argument) error {
func GenerateLayout(args *config.Argument) error {
lg := &generator.LayoutGenerator{
TemplateGenerator: generator.TemplateGenerator{
OutputDir: args.OutDir,
@ -292,7 +311,7 @@ func generateLayout(args *config.Argument) error {
return nil
}
func triggerPlugin(args *config.Argument) error {
func TriggerPlugin(args *config.Argument) error {
if len(args.IdlPaths) == 0 {
return nil
}

View File

@ -22,8 +22,8 @@ import (
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/urfave/cli/v2"
)

View File

@ -24,9 +24,9 @@ import (
"strings"
"syscall"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
)
func lookupTool(idlType string) (string, error) {

20
cmd/hz/doc.go Normal file
View File

@ -0,0 +1,20 @@
// Copyright 2022 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Package "github.com/cloudwego/hertz/cmd/hz" contains packages for building the hz command line tool.
// APIs exported by packages under this directory do not promise any backward
// compatibility, so please do not rely on them.
package main

View File

@ -22,7 +22,7 @@ import (
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/util"
)
type File struct {

View File

@ -23,9 +23,9 @@ import (
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
)
type HttpMethod struct {

View File

@ -23,7 +23,7 @@ import (
"io/ioutil"
"reflect"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/util"
"gopkg.in/yaml.v2"
)

View File

@ -22,10 +22,10 @@ import (
"strings"
"text/template"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model/golang"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/generator/model/golang"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
)
//---------------------------------Backend----------------------------------

View File

@ -20,8 +20,8 @@ import (
"testing"
"text/template"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/meta"
)
type StringValue struct {

View File

@ -24,9 +24,9 @@ import (
"reflect"
"text/template"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"gopkg.in/yaml.v2"
)

View File

@ -25,7 +25,7 @@ import (
"sort"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/util"
)
type Router struct {

View File

@ -25,9 +25,9 @@ import (
"strings"
"text/template"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
)
var DefaultDelimiters = [2]string{"{{", "}}"}

View File

@ -18,20 +18,14 @@ package main
import (
"os"
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/app"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/protobuf"
"github.com/cloudwego/hertz/cmd/hz/internal/thrift"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/app"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
)
func main() {
// run in plugin mode
pluginMode()
app.PluginMode()
// run in normal mode
Run()
@ -48,18 +42,3 @@ func Run() {
logs.Errorf("%v\n", err)
}
}
func pluginMode() {
pluginName := filepath.Base(os.Args[0])
if util.IsWindows() {
pluginName = strings.TrimSuffix(pluginName, ".exe")
}
switch pluginName {
case meta.ThriftPluginName:
plugin := new(thrift.Plugin)
os.Exit(plugin.Run())
case meta.ProtocPluginName:
plugin := new(protobuf.Plugin)
os.Exit(plugin.Run())
}
}

View File

@ -23,7 +23,7 @@ import (
"path/filepath"
"regexp"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
gv "github.com/hashicorp/go-version"
)

View File

@ -21,11 +21,11 @@ import (
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/protobuf/api"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/protobuf/api"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/types/descriptorpb"
)

View File

@ -54,12 +54,12 @@ import (
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/config"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/config"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
gengo "google.golang.org/protobuf/cmd/protoc-gen-go/internal_gengo"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"

View File

@ -21,7 +21,7 @@ import (
"strings"
"testing"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/meta"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)

View File

@ -20,8 +20,8 @@ import (
"fmt"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/types/descriptorpb"
)

View File

@ -22,11 +22,11 @@ import (
"strconv"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/config"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/protobuf/api"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/config"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/protobuf/api"
"github.com/cloudwego/hertz/cmd/hz/util"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl"

View File

@ -19,10 +19,10 @@ package thrift
import (
"fmt"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
"github.com/cloudwego/thriftgo/generator/golang/styles"
"github.com/cloudwego/thriftgo/parser"
)

View File

@ -23,12 +23,12 @@ import (
"path/filepath"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/config"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/config"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
"github.com/cloudwego/thriftgo/generator/golang/styles"
thriftgo_plugin "github.com/cloudwego/thriftgo/plugin"
)

View File

@ -20,9 +20,9 @@ import (
"io/ioutil"
"testing"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/thriftgo/plugin"
)

View File

@ -20,8 +20,8 @@ import (
"fmt"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/thriftgo/parser"
)

View File

@ -21,7 +21,7 @@ import (
"strings"
"testing"
"github.com/cloudwego/hertz/cmd/hz/internal/config"
"github.com/cloudwego/hertz/cmd/hz/config"
"github.com/cloudwego/thriftgo/plugin"
)

View File

@ -22,10 +22,10 @@ import (
"strconv"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/config"
"github.com/cloudwego/hertz/cmd/hz/internal/generator"
"github.com/cloudwego/hertz/cmd/hz/internal/generator/model"
"github.com/cloudwego/hertz/cmd/hz/internal/util"
"github.com/cloudwego/hertz/cmd/hz/config"
"github.com/cloudwego/hertz/cmd/hz/generator"
"github.com/cloudwego/hertz/cmd/hz/generator/model"
"github.com/cloudwego/hertz/cmd/hz/util"
"github.com/cloudwego/thriftgo/parser"
)

View File

@ -25,7 +25,7 @@ import (
"strconv"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
)
func CopyStringSlice(from, to *[]string) {

View File

@ -27,7 +27,7 @@ import (
"regexp"
"strings"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/meta"
)
func GetGOPATH() (gopath string, err error) {

View File

@ -24,8 +24,8 @@ import (
"strings"
"time"
"github.com/cloudwego/hertz/cmd/hz/internal/meta"
"github.com/cloudwego/hertz/cmd/hz/internal/util/logs"
"github.com/cloudwego/hertz/cmd/hz/meta"
"github.com/cloudwego/hertz/cmd/hz/util/logs"
gv "github.com/hashicorp/go-version"
)