Skip to content

Commit 27ffe8a

Browse files
committed
INTERNAL: Move memcached connect to connect
1 parent 66e70d1 commit 27ffe8a

5 files changed

Lines changed: 62 additions & 31 deletions

File tree

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package memcached
1+
package cmd
22

33
import (
44
"bufio"
55
"bytes"
66
"fmt"
7+
"log"
78
"net"
89
"os"
910
"strings"
@@ -14,32 +15,30 @@ import (
1415
)
1516

1617
var connectCmd = &cobra.Command{
17-
Use: "connect ip:port [username:password]",
18-
Short: "Connect to a memcached server",
19-
Args: cobra.RangeArgs(1, 2),
18+
Use: "connect <addr> [username:password]",
19+
Args: cobra.RangeArgs(1, 2),
2020
Run: func(cmd *cobra.Command, args []string) {
2121
conn, err := net.Dial("tcp", args[0])
2222
if err != nil {
23-
fmt.Fprintln(os.Stderr, err)
24-
os.Exit(1)
23+
panic(err)
2524
}
2625
defer conn.Close()
2726

2827
if len(args) == 2 {
2928
username, password, _ := strings.Cut(args[1], ":")
3029
if err := authentication(conn, username, password); err != nil {
31-
fmt.Fprintln(os.Stderr, err)
32-
os.Exit(1)
30+
panic(err)
3331
}
3432
}
3533

34+
log.Printf("connected to %s\n", conn.RemoteAddr())
35+
3636
go func() {
3737
for {
3838
buf := make([]byte, 1024)
3939
n, err := conn.Read(buf)
4040
if err != nil {
41-
fmt.Fprintln(os.Stderr, err)
42-
os.Exit(1)
41+
panic(err)
4342
}
4443
fmt.Print(string(buf[:n]))
4544
}
@@ -49,15 +48,13 @@ var connectCmd = &cobra.Command{
4948
for {
5049
input, err := reader.ReadBytes('\n')
5150
if err != nil {
52-
fmt.Fprintln(os.Stderr, err)
53-
os.Exit(1)
51+
panic(err)
5452
}
5553
if len(input) > 1 && input[len(input)-2] != '\r' {
5654
input = append(input[:len(input)-1], '\r', '\n')
5755
}
5856
if _, err := conn.Write(input); err != nil {
59-
fmt.Fprintln(os.Stderr, err)
60-
os.Exit(1)
57+
panic(err)
6158
}
6259
}
6360
},
@@ -66,6 +63,29 @@ var connectCmd = &cobra.Command{
6663
func authentication(conn net.Conn, username, password string) error {
6764
reader := bufio.NewReader(conn)
6865

66+
command := "sasl mech\r\n"
67+
if _, err := conn.Write([]byte(command)); err != nil {
68+
return err
69+
}
70+
71+
line, err := reader.ReadBytes('\n')
72+
if err != nil {
73+
return err
74+
}
75+
76+
line = bytes.TrimSpace(line)
77+
responseCode := strings.Split(string(line), " ")[0]
78+
switch responseCode {
79+
case "SASL_MECH":
80+
if !bytes.Contains(line, []byte("SCRAM-SHA-256")) {
81+
return fmt.Errorf("SCRAM-SHA-256 not supported")
82+
}
83+
case "ERROR", "NOT_SUPPORTED":
84+
return nil
85+
default:
86+
return fmt.Errorf("unexpected response: %s", line)
87+
}
88+
6989
client := sasl.NewClient()
7090
mechanism, err := client.Mechanism("SCRAM-SHA-256")
7191
if err != nil {
@@ -87,7 +107,7 @@ func authentication(conn net.Conn, username, password string) error {
87107
}
88108

89109
clientFirstMsgString := clientFirstMsg.String()
90-
command := fmt.Sprintf("sasl auth SCRAM-SHA-256 %d\r\n%s\r\n",
110+
command = fmt.Sprintf("sasl auth SCRAM-SHA-256 %d\r\n%s\r\n",
91111
len(clientFirstMsgString), clientFirstMsgString)
92112

93113
for {
@@ -99,9 +119,9 @@ func authentication(conn net.Conn, username, password string) error {
99119
if err != nil {
100120
return err
101121
}
102-
line = bytes.TrimSpace(line)
103122

104123
var serverMsg string
124+
line = bytes.TrimSpace(line)
105125
responseCode := strings.Split(string(line), " ")[0]
106126
switch responseCode {
107127
case "SASL_OK":

cmd/memcached/memcached.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func init() {
3333
MemcachedCmd.AddCommand(addCmd)
3434
MemcachedCmd.AddCommand(removeCmd)
3535
MemcachedCmd.AddCommand(configCmd)
36-
MemcachedCmd.AddCommand(connectCmd)
3736
MemcachedCmd.AddCommand(listCmd)
3837
MemcachedCmd.AddCommand(startCmd)
3938
MemcachedCmd.AddCommand(stopCmd)

cmd/root.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ import (
55
"os"
66

77
"github.com/jam2in/arcusctl/cmd/acl"
8-
"github.com/jam2in/arcusctl/cmd/memcached"
9-
"github.com/jam2in/arcusctl/cmd/zookeeper"
108
"github.com/jam2in/arcusctl/internal"
119
"github.com/spf13/cobra"
1210
)
1311

1412
var rootCmd = &cobra.Command{
15-
Use: "arcusctl",
16-
Short: "Arcus CLI",
17-
Long: "Arcus CLI is a command line interface for managing Arcus",
13+
Use: "arcusctl",
1814
}
1915

2016
func init() {
2117
rootCmd.PersistentFlags().BoolVarP(&internal.Flags.Verbose, "verbose", "v", false, "")
2218
rootCmd.PersistentFlags().StringVar(&internal.Flags.ConfigFile, "config-file", "", "")
2319

2420
rootCmd.AddCommand(acl.RootCmd)
25-
rootCmd.AddCommand(zookeeper.ZookeeperCmd)
26-
rootCmd.AddCommand(memcached.MemcachedCmd)
21+
rootCmd.AddCommand(connectCmd)
22+
23+
// rootCmd.AddCommand(zookeeper.ZookeeperCmd)
24+
// rootCmd.AddCommand(memcached.MemcachedCmd)
2725

2826
cobra.OnInitialize(internal.InitConfig)
2927
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ require (
66
github.com/cybergarage/go-sasl v1.2.6
77
github.com/go-zookeeper/zk v1.0.4
88
github.com/spf13/cobra v1.9.1
9+
github.com/spf13/viper v1.21.0
10+
golang.org/x/crypto v0.42.0
911
golang.org/x/term v0.35.0
1012
)
1113

@@ -20,10 +22,8 @@ require (
2022
github.com/spf13/afero v1.15.0 // indirect
2123
github.com/spf13/cast v1.10.0 // indirect
2224
github.com/spf13/pflag v1.0.10 // indirect
23-
github.com/spf13/viper v1.21.0 // indirect
2425
github.com/subosito/gotenv v1.6.0 // indirect
2526
go.yaml.in/yaml/v3 v3.0.4 // indirect
26-
golang.org/x/crypto v0.42.0 // indirect
2727
golang.org/x/sys v0.36.0 // indirect
2828
golang.org/x/text v0.29.0 // indirect
2929
)

go.sum

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,30 @@ github.com/cybergarage/go-safecast v1.3.3 h1:ShXzs7PLDsomNeueg65IyPPEc+cw5QLd10+
33
github.com/cybergarage/go-safecast v1.3.3/go.mod h1:flFLUrI49kNK+U7ZeQDQxbaxxQyFVQGKpT7KYN3A4ew=
44
github.com/cybergarage/go-sasl v1.2.6 h1:O963Aa5S9vmUUH4wR2UQiBTilEc0UGysykPiZReSEAU=
55
github.com/cybergarage/go-sasl v1.2.6/go.mod h1:ForFfY1+iVolRK0wo/OweuD+x8z4y3Cg8tTNlxDcGF0=
6+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
9+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
610
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
711
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
812
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
913
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
1014
github.com/go-zookeeper/zk v1.0.4 h1:DPzxraQx7OrPyXq2phlGlNSIyWEsAox0RJmjTseMV6I=
1115
github.com/go-zookeeper/zk v1.0.4/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
16+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
17+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1218
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
1319
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
20+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
21+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
22+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
23+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1424
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
1525
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
26+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
27+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
28+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
29+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
1630
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1731
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
1832
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
@@ -24,27 +38,27 @@ github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
2438
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
2539
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
2640
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
27-
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
2841
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2942
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
3043
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
3144
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
3245
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
46+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
47+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
3348
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
3449
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
3550
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
3651
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
3752
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
3853
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
39-
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
40-
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
4154
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
4255
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
43-
golang.org/x/term v0.34.0 h1:O/2T7POpk0ZZ7MAzMeWFSg6S5IpWd/RXDlM9hgM3DR4=
44-
golang.org/x/term v0.34.0/go.mod h1:5jC53AEywhIVebHgPVeg0mj8OD3VO9OzclacVrqpaAw=
4556
golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ=
4657
golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA=
4758
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
4859
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
4960
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
61+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
62+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
63+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5064
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)