Skip to content

Commit

Permalink
Apply wrapper (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
visill authored Oct 6, 2024
1 parent ffc32ae commit e2e23c0
Showing 1 changed file with 65 additions and 89 deletions.
154 changes: 65 additions & 89 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,69 @@ func listFunc(con net.Conn, instanceCnf *config.Instance, args []string) error {
return nil
}

func deleteFunc(con net.Conn, instanceCnf *config.Instance, args []string) error {
ylogger.Zero.Info().Msg("Execute delete command")

ylogger.Zero.Info().Str("name", args[0]).Msg("delete")
msg := message.NewDeleteMessage(args[0], segmentPort, segmentNum, confirm, garbage).Encode()
_, err := con.Write(msg)
if err != nil {
return err
}

ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed delete msg")

client := client.NewYClient(con)
protoReader := proc.NewProtoReader(client)

ansType, body, err := protoReader.ReadPacket()
if err != nil {
ylogger.Zero.Debug().Err(err).Msg("error while recieving answer")
return err
}

if ansType != message.MessageTypeReadyForQuery {
return fmt.Errorf("failed to delete, msg: %v", body)
}

return nil
}

func goolFunc(con net.Conn, instanceCnf *config.Instance, args []string) error {
msg := message.NewGoolMessage(args[0]).Encode()
_, err := con.Write(msg)
if err != nil {
ylogger.Zero.Debug().Err(err)
return err
}

ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed gool message")

ycl := client.NewYClient(con)
r := proc.NewProtoReader(ycl)

done := false
for {
if done {
break
}
tp, _, err := r.ReadPacket()
if err != nil {
return err
}

switch tp {
case message.MessageTypeReadyForQuery:
ylogger.Zero.Debug().Bytes("msg", msg).Msg("got RFQ message")
done = true
default:
return fmt.Errorf("incorrect gool")
}
}

return nil
}

var rootCmd = &cobra.Command{
Use: "",
Short: "",
Expand All @@ -236,44 +299,7 @@ var copyCmd = &cobra.Command{
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete",
RunE: func(cmd *cobra.Command, args []string) error {
ylogger.Zero.Info().Msg("Execute delete command")
err := config.LoadInstanceConfig(cfgPath)
if err != nil {
return err
}
instanceCnf := config.InstanceConfig()

con, err := net.Dial("unix", instanceCnf.SocketPath)
if err != nil {
return err
}
defer con.Close()

ylogger.Zero.Info().Str("name", args[0]).Msg("delete")
msg := message.NewDeleteMessage(args[0], segmentPort, segmentNum, confirm, garbage).Encode()
_, err = con.Write(msg)
if err != nil {
return err
}

ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed delete msg")

client := client.NewYClient(con)
protoReader := proc.NewProtoReader(client)

ansType, body, err := protoReader.ReadPacket()
if err != nil {
ylogger.Zero.Debug().Err(err).Msg("error while recieving answer")
return err
}

if ansType != message.MessageTypeReadyForQuery {
return fmt.Errorf("failed to delete, msg: %v", body)
}

return nil
},
RunE: Runner(deleteFunc),
}

var putCmd = &cobra.Command{
Expand All @@ -293,57 +319,7 @@ var listCmd = &cobra.Command{
var goolCmd = &cobra.Command{
Use: "gool",
Short: "gool",
RunE: func(cmd *cobra.Command, args []string) error {

err := config.LoadInstanceConfig(cfgPath)
if err != nil {
ylogger.Zero.Debug().Err(err)
return err
}

instanceCnf := config.InstanceConfig()

con, err := net.Dial("unix", instanceCnf.InterconnectSocketPath)

if err != nil {
ylogger.Zero.Debug().Err(err)
return err
}

defer con.Close()
msg := message.NewGoolMessage(args[0]).Encode()
_, err = con.Write(msg)
if err != nil {
ylogger.Zero.Debug().Err(err)
return err
}

ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed gool message")

ycl := client.NewYClient(con)
r := proc.NewProtoReader(ycl)

done := false
for {
if done {
break
}
tp, _, err := r.ReadPacket()
if err != nil {
return err
}

switch tp {
case message.MessageTypeReadyForQuery:
ylogger.Zero.Debug().Bytes("msg", msg).Msg("got RFQ message")
done = true
default:
return fmt.Errorf("incorrect gool")
}
}

return nil
},
RunE: Runner(goolFunc),
}

func init() {
Expand Down

0 comments on commit e2e23c0

Please sign in to comment.