|  | 
|  | 1 | +// Copyright (C) 2021 CGI France | 
|  | 2 | +// | 
|  | 3 | +// This file is part of Cat Balancer. | 
|  | 4 | +// | 
|  | 5 | +// Cat Balancer is free software: you can redistribute it and/or modify | 
|  | 6 | +// it under the terms of the GNU General Public License as published by | 
|  | 7 | +// the Free Software Foundation, either version 3 of the License, or | 
|  | 8 | +// (at your option) any later version. | 
|  | 9 | +// | 
|  | 10 | +// Cat Balancer is distributed in the hope that it will be useful, | 
|  | 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 13 | +// GNU General Public License for more details. | 
|  | 14 | +// | 
|  | 15 | +// You should have received a copy of the GNU General Public License | 
|  | 16 | +// along with Cat Balancer.  If not, see <http://www.gnu.org/licenses/>. | 
|  | 17 | + | 
|  | 18 | +package main | 
|  | 19 | + | 
|  | 20 | +import ( | 
|  | 21 | +	"fmt" | 
|  | 22 | +	"io" | 
|  | 23 | +	"net" | 
|  | 24 | +	"os" | 
|  | 25 | + | 
|  | 26 | +	"github.com/rs/zerolog" | 
|  | 27 | +	"github.com/rs/zerolog/log" | 
|  | 28 | +	"github.com/spf13/cobra" | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | +// Provisioned by ldflags | 
|  | 32 | +// nolint: gochecknoglobals | 
|  | 33 | +var ( | 
|  | 34 | +	name      string | 
|  | 35 | +	version   string | 
|  | 36 | +	commit    string | 
|  | 37 | +	buildDate string | 
|  | 38 | +	builtBy   string | 
|  | 39 | +) | 
|  | 40 | + | 
|  | 41 | +func main() { | 
|  | 42 | +	// nolint: exhaustivestruct | 
|  | 43 | +	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) | 
|  | 44 | + | 
|  | 45 | +	var ( | 
|  | 46 | +		inAdrress  string | 
|  | 47 | +		outAddress string | 
|  | 48 | +		verbosity  string | 
|  | 49 | +	) | 
|  | 50 | + | 
|  | 51 | +	// nolint: exhaustivestruct | 
|  | 52 | +	rootCmd := &cobra.Command{ | 
|  | 53 | +		Use:     name, | 
|  | 54 | +		Example: fmt.Sprintf("%s --in myserver:1961", name), | 
|  | 55 | +		Short:   "Cat Pipe : pipe stdin or stdout to a cat balancer server", | 
|  | 56 | +		Version: fmt.Sprintf(`%v (commit=%v date=%v by=%v) | 
|  | 57 | +Copyright (C) 2021 CGI France | 
|  | 58 | +License GPLv3: GNU GPL version 3 <https://gnu.org/licenses/gpl.html>. | 
|  | 59 | +This is free software: you are free to change and redistribute it. | 
|  | 60 | +There is NO WARRANTY, to the extent permitted by law.`, version, commit, buildDate, builtBy), | 
|  | 61 | + | 
|  | 62 | +		RunE: func(cmd *cobra.Command, args []string) error { | 
|  | 63 | +			switch verbosity { | 
|  | 64 | +			case "trace", "5": | 
|  | 65 | +				zerolog.SetGlobalLevel(zerolog.TraceLevel) | 
|  | 66 | +				log.Info().Msg("Logger level set to trace") | 
|  | 67 | +			case "debug", "4": | 
|  | 68 | +				zerolog.SetGlobalLevel(zerolog.DebugLevel) | 
|  | 69 | +				log.Info().Msg("Logger level set to debug") | 
|  | 70 | +			case "info", "3": | 
|  | 71 | +				zerolog.SetGlobalLevel(zerolog.InfoLevel) | 
|  | 72 | +				log.Info().Msg("Logger level set to info") | 
|  | 73 | +			case "warn", "2": | 
|  | 74 | +				zerolog.SetGlobalLevel(zerolog.WarnLevel) | 
|  | 75 | +			case "error", "1": | 
|  | 76 | +				zerolog.SetGlobalLevel(zerolog.ErrorLevel) | 
|  | 77 | +			default: | 
|  | 78 | +				zerolog.SetGlobalLevel(zerolog.Disabled) | 
|  | 79 | +			} | 
|  | 80 | + | 
|  | 81 | +			return run(cmd, inAdrress, outAddress) | 
|  | 82 | +		}, | 
|  | 83 | +	} | 
|  | 84 | + | 
|  | 85 | +	rootCmd.PersistentFlags().StringVarP(&inAdrress, "in", "i", "", | 
|  | 86 | +		"input server's address (empty for stdin by default)") | 
|  | 87 | +	rootCmd.PersistentFlags().StringVarP(&outAddress, "out", "o", "", | 
|  | 88 | +		"output server's address (empty for stdout by default)") | 
|  | 89 | +	rootCmd.PersistentFlags(). | 
|  | 90 | +		StringVarP(&verbosity, | 
|  | 91 | +			"verbosity", | 
|  | 92 | +			"v", | 
|  | 93 | +			"info", | 
|  | 94 | +			"set level of log verbosity : none (0), error (1), warn (2), info (3), debug (4), trace (5)", | 
|  | 95 | +		) | 
|  | 96 | + | 
|  | 97 | +	if err := rootCmd.Execute(); err != nil { | 
|  | 98 | +		log.Err(err).Msg("Error when executing command") | 
|  | 99 | +		os.Exit(1) | 
|  | 100 | +	} | 
|  | 101 | +} | 
|  | 102 | + | 
|  | 103 | +func run(cmd *cobra.Command, in string, out string) error { | 
|  | 104 | +	streamIn := cmd.InOrStdin() | 
|  | 105 | +	streamOut := cmd.OutOrStdout() | 
|  | 106 | + | 
|  | 107 | +	if in != "" { | 
|  | 108 | +		conIn, err := net.Dial("tcp", in) | 
|  | 109 | +		if err != nil { | 
|  | 110 | +			return fmt.Errorf("%w", err) | 
|  | 111 | +		} | 
|  | 112 | +		defer conIn.Close() | 
|  | 113 | + | 
|  | 114 | +		streamIn = conIn | 
|  | 115 | +	} | 
|  | 116 | + | 
|  | 117 | +	if out != "" { | 
|  | 118 | +		conOut, err := net.Dial("tcp", out) | 
|  | 119 | +		if err != nil { | 
|  | 120 | +			return fmt.Errorf("%w", err) | 
|  | 121 | +		} | 
|  | 122 | +		defer conOut.Close() | 
|  | 123 | + | 
|  | 124 | +		streamOut = conOut | 
|  | 125 | +	} | 
|  | 126 | + | 
|  | 127 | +	_, err := io.Copy(streamOut, streamIn) | 
|  | 128 | +	if err != nil { | 
|  | 129 | +		return fmt.Errorf("%w", err) | 
|  | 130 | +	} | 
|  | 131 | + | 
|  | 132 | +	return nil | 
|  | 133 | +} | 
0 commit comments