github.com/cloudwego/kitex@v0.9.0/tool/cmd/kitex/main.go (about)

     1  // Copyright 2021 CloudWeGo Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"flag"
    20  	"io/ioutil"
    21  	"os"
    22  	"os/exec"
    23  	"strings"
    24  
    25  	"github.com/cloudwego/kitex"
    26  	kargs "github.com/cloudwego/kitex/tool/cmd/kitex/args"
    27  	"github.com/cloudwego/kitex/tool/internal_pkg/log"
    28  	"github.com/cloudwego/kitex/tool/internal_pkg/pluginmode/protoc"
    29  	"github.com/cloudwego/kitex/tool/internal_pkg/pluginmode/thriftgo"
    30  )
    31  
    32  var args kargs.Arguments
    33  
    34  func init() {
    35  	var queryVersion bool
    36  	args.AddExtraFlag(&kargs.ExtraFlag{
    37  		Apply: func(f *flag.FlagSet) {
    38  			f.BoolVar(&queryVersion, "version", false,
    39  				"Show the version of kitex")
    40  		},
    41  		Check: func(a *kargs.Arguments) {
    42  			if queryVersion {
    43  				println(a.Version)
    44  				os.Exit(0)
    45  			}
    46  		},
    47  	})
    48  }
    49  
    50  func main() {
    51  	mode := os.Getenv(kargs.EnvPluginMode)
    52  	if len(os.Args) <= 1 && mode != "" {
    53  		// run as a plugin
    54  		switch mode {
    55  		case thriftgo.PluginName:
    56  			os.Exit(thriftgo.Run())
    57  		case protoc.PluginName:
    58  			os.Exit(protoc.Run())
    59  		}
    60  	}
    61  
    62  	// run as kitex
    63  	args.ParseArgs(kitex.Version)
    64  
    65  	out := new(bytes.Buffer)
    66  	cmd := args.BuildCmd(out)
    67  	err := cmd.Run()
    68  	if err != nil {
    69  		if args.Use != "" {
    70  			out := strings.TrimSpace(out.String())
    71  			if strings.HasSuffix(out, thriftgo.TheUseOptionMessage) {
    72  				goto NormalExit
    73  			}
    74  		}
    75  		os.Exit(1)
    76  	}
    77  NormalExit:
    78  	if args.IDLType == "thrift" {
    79  		cmd := "go mod edit -replace github.com/apache/thrift=github.com/apache/thrift@v0.13.0"
    80  		argv := strings.Split(cmd, " ")
    81  		err := exec.Command(argv[0], argv[1:]...).Run()
    82  
    83  		res := "Done"
    84  		if err != nil {
    85  			res = err.Error()
    86  		}
    87  		log.Warn("Adding apache/thrift@v0.13.0 to go.mod for generated code ..........", res)
    88  	}
    89  
    90  	// remove kitex.yaml generated from v0.4.4 which is renamed as kitex_info.yaml
    91  	if args.ServiceName != "" {
    92  		DeleteKitexYaml()
    93  	}
    94  
    95  	// If hessian option is java_extension, replace *java.Object to java.Object
    96  	if thriftgo.EnableJavaExtension(args.Config) {
    97  		if err = thriftgo.Hessian2PatchByReplace(args.Config, ""); err != nil {
    98  			log.Warn("replace java object fail, you can fix it then regenerate", err)
    99  		}
   100  	}
   101  }
   102  
   103  func DeleteKitexYaml() {
   104  	// try to read kitex.yaml
   105  	data, err := ioutil.ReadFile("kitex.yaml")
   106  	if err != nil {
   107  		if !os.IsNotExist(err) {
   108  			log.Warn("kitex.yaml, which is used to record tool info, is deprecated, it's renamed as kitex_info.yaml, you can delete it or ignore it.")
   109  		}
   110  		return
   111  	}
   112  	// if kitex.yaml exists, check content and delete it.
   113  	if strings.HasPrefix(string(data), "kitexinfo:") {
   114  		err = os.Remove("kitex.yaml")
   115  		if err != nil {
   116  			log.Warn("kitex.yaml, which is used to record tool info, is deprecated, it's renamed as kitex_info.yaml, you can delete it or ignore it.")
   117  		} else {
   118  			log.Warn("kitex.yaml, which is used to record tool info, is deprecated, it's renamed as kitex_info.yaml, so it's automatically deleted now.")
   119  		}
   120  	}
   121  }