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

dev/decrement-operation init #22

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
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
Binary file modified bin/ccmc
Binary file not shown.
22 changes: 22 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ to quickly create a Cobra application.`,
},
}

var decrementCmd = &cobra.Command{
Use: "decr",
Args: cobra.ExactArgs(2),
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
value, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println("Error, the decrement value must be an integer")
return
}

client.SendDecrCommand(host, port, args[0], value)
},
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
Expand All @@ -86,4 +107,5 @@ func init() {
// Commands
rootCmd.AddCommand(deleteCmd)
rootCmd.AddCommand(incrementCmd)
rootCmd.AddCommand(decrementCmd)
}
48 changes: 48 additions & 0 deletions internal/app/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,51 @@ func SendIncrCommand(host string, port int, key string, value int) {
}
}
}

func SendDecrCommand(host string, port int, key string, value int) {
client, err := initClient(host, port)
if err != nil {
fmt.Println("Error connecting to the server: ", err)
return
}

defer client.conn.Close()

buffCmd, err := serialization.SerializeIncrDecrCommand("decr", key, value)
if err != nil {
fmt.Println("Error serializing data: ", err)
return
}

if _, err = client.conn.Write(buffCmd.Bytes()); err != nil {
fmt.Println("Error sending data to server: ", err)
return
}

reader := bufio.NewReader(client.conn)

for {
message, err := reader.ReadString('\n')
if err == io.EOF {
return
}

if err != nil {
fmt.Println("Error reading data from the server: ", err)
return
}

switch message {
case "NOT_FOUND\r\n":
fmt.Println("An item with this key has not been found")
return
case "CLIENT_ERROR\r\n":
fmt.Println("Cannot increment or decrement non-numeric value")
return
default:
v := message[0 : len(message)-2]
fmt.Println(v)
return
}
}
}