예제 #1
0
func run(appEnv *appEnv, dirPath string, outDirPath string, compileOptions *protoeasy.CompileOptions) error {
	compiler := protoeasy.DefaultClientCompiler
	if appEnv.Address != "" {
		clientConn, err := grpc.Dial(appEnv.Address, grpc.WithInsecure())
		if err != nil {
			return err
		}
		compiler = protoeasy.NewClientCompiler(
			protoeasy.NewAPIClient(
				clientConn,
			),
			protoeasy.CompilerOptions{},
		)
	}

	commands, err := compiler.Compile(dirPath, outDirPath, compileOptions)
	if err != nil {
		if desc := grpc.ErrorDesc(err); desc != "" {
			err = errors.New(desc)
		}
		if errString := strings.TrimSpace(err.Error()); errString != "" {
			protolion.Errorln(errString)
		}
		return errors.New("")
	}
	for _, command := range commands {
		if len(command.Arg) > 0 {
			protolion.Infof("\n%s\n", strings.Join(command.Arg, " \\\n\t"))
		}
	}
	return nil
}
예제 #2
0
파일: main.go 프로젝트: sr/protoeasy
func do(appEnvObj interface{}) error {
	appEnv := appEnvObj.(*appEnv)
	compileOptions := &protoeasy.CompileOptions{}
	options := &options{}

	rootCmd := &cobra.Command{
		Use: fmt.Sprintf("%s directory", os.Args[0]),
		RunE: func(_ *cobra.Command, args []string) error {
			if len(args) != 1 {
				return fmt.Errorf("must pass one argument, the directory, but passed %d arguments", len(args))
			}
			if err := optionsToCompileOptions(options, compileOptions); err != nil {
				return err
			}
			dirPath := args[0]
			outDirPath := args[0]
			if options.OutDirPath != "" {
				outDirPath = options.OutDirPath
			}

			compiler := protoeasy.DefaultServerCompiler
			if appEnv.Address != "" {
				clientConn, err := grpc.Dial(appEnv.Address, grpc.WithInsecure())
				if err != nil {
					return err
				}
				compiler = protoeasy.NewClientCompiler(
					protoeasy.NewAPIClient(
						clientConn,
					),
					protoeasy.CompilerOptions{},
				)
			}

			commands, err := compiler.Compile(dirPath, outDirPath, compileOptions)
			if err != nil {
				return err
			}
			logCommands(commands)
			return nil
		},
	}
	bindCompileOptions(rootCmd.Flags(), compileOptions)
	bindOptions(rootCmd.Flags(), options)

	return rootCmd.Execute()
}