Skip to content

Commit 14449ee

Browse files
author
Youen Péron
authored
Merge pull request #1 from CGI-FR/feat-catp
feat: new `catp` command
2 parents 37bf30b + 0b19e89 commit 14449ee

File tree

4 files changed

+218
-2
lines changed

4 files changed

+218
-2
lines changed

.vscode/settings.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"titleBar.activeBackground": "#f9e64f",
1414
"titleBar.activeForeground": "#15202b",
1515
"titleBar.inactiveBackground": "#f9e64f99",
16-
"titleBar.inactiveForeground": "#15202b99"
16+
"titleBar.inactiveForeground": "#15202b99",
17+
"sash.hoverBorder": "#fbed80",
18+
"statusBarItem.remoteBackground": "#f9e64f",
19+
"statusBarItem.remoteForeground": "#15202b"
1720
},
1821
"peacock.remoteColor": "#f9e64f"
19-
}
22+
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Types of changes
1414
- `Fixed` for any bug fixes.
1515
- `Security` in case of vulnerabilities.
1616

17+
## [0.2.0] 2022-05-23
18+
19+
- `Added` Cat Pipe client.
20+
1721
## [0.1.0] 2021-09-24
1822

1923
- `Added` First official release.

cmd/catp/main.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

cmd/catp/main_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
// nolint: testpackage
19+
package main
20+
21+
import (
22+
"fmt"
23+
"strings"
24+
"sync"
25+
"testing"
26+
"time"
27+
28+
"github.com/cgi-fr/cat-balancer/pkg/balancer"
29+
"github.com/spf13/cobra"
30+
"github.com/stretchr/testify/assert"
31+
)
32+
33+
const (
34+
producerPort = 1691
35+
consumerPort = 1961
36+
)
37+
38+
func Test_run(t *testing.T) {
39+
t.Parallel()
40+
41+
go balancer.New("tcp", fmt.Sprintf(":%d", producerPort), "tcp", fmt.Sprintf(":%d", consumerPort)).Start()
42+
43+
time.Sleep(1 * time.Second)
44+
45+
wg := sync.WaitGroup{}
46+
47+
wg.Add(1)
48+
49+
go func() {
50+
reader := strings.NewReader("hello\n")
51+
// nolint: exhaustivestruct
52+
cmd := &cobra.Command{}
53+
cmd.SetIn(reader)
54+
55+
err := run(cmd, "", "127.0.0.1:1691")
56+
57+
assert.Nil(t, err)
58+
wg.Done()
59+
}()
60+
61+
wg.Add(1)
62+
63+
go func() {
64+
writer := strings.Builder{}
65+
// nolint: exhaustivestruct
66+
cmd := &cobra.Command{}
67+
cmd.SetOut(&writer)
68+
69+
err := run(cmd, "127.0.0.1:1961", "")
70+
assert.Nil(t, err)
71+
assert.Equal(t, "hello\n", writer.String())
72+
wg.Done()
73+
}()
74+
75+
wg.Wait()
76+
}

0 commit comments

Comments
 (0)