Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brought to a single function style in main (using the Runner wrapper function), removed repetitive code #64

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading