Skip to content

Commit

Permalink
Refactoring (#45)
Browse files Browse the repository at this point in the history
* Refactoring + Filestorage

* Naming

* Remove filestorage

* Persistency

* ArgCheck

* cobra usage
  • Loading branch information
visill authored Aug 4, 2024
1 parent 39c3761 commit edeb821
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 315 deletions.
313 changes: 143 additions & 170 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ var decrypt bool
var encrypt bool
var offset uint64

var rootCmd = &cobra.Command{
Use: "",
Short: "",
}
// TODOV
func Runner(f func(net.Conn, *config.Instance, []string) error) func(*cobra.Command, []string) error {

var catCmd = &cobra.Command{
Use: "cat",
Short: "cat",
RunE: func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {

err := config.LoadInstanceConfig(cfgPath)
if err != nil {
Expand All @@ -47,211 +42,189 @@ var catCmd = &cobra.Command{
}

defer con.Close()
msg := message.NewCatMessage(args[0], decrypt, offset).Encode()
_, err = con.Write(msg)
if err != nil {
return err
}

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

_, err = io.Copy(os.Stdout, con)
if err != nil {
return err
}

return nil
},
return f(con, instanceCnf, args)
}
}

var copyCmd = &cobra.Command{
Use: "copy",
Short: "copy",
RunE: func(cmd *cobra.Command, args []string) error {
ylogger.Zero.Info().Msg("Execute copy 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("copy")
msg := message.NewCopyMessage(args[0], oldCfgPath, encrypt, decrypt).Encode()
_, err = con.Write(msg)
if err != nil {
return err
}

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

client := client.NewYClient(con)
protoReader := proc.NewProtoReader(client)
func catFunc(con net.Conn, instanceCnf *config.Instance, args []string) error {
msg := message.NewCatMessage(args[0], decrypt, offset).Encode()
_, err := con.Write(msg)
if err != nil {
return err
}

ansType, body, err := protoReader.ReadPacket()
if err != nil {
ylogger.Zero.Debug().Err(err).Msg("error while ans")
return err
}
ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed cat message")

if ansType != message.MessageTypeReadyForQuery {
return fmt.Errorf("failed to copy, msg: %v", body)
}
_, err = io.Copy(os.Stdout, con)
if err != nil {
return err
}

return nil
},
return nil
}

var putCmd = &cobra.Command{
Use: "put",
Short: "put",
RunE: func(cmd *cobra.Command, args []string) error {

err := config.LoadInstanceConfig(cfgPath)
if err != nil {
return err
}

instanceCnf := config.InstanceConfig()
func copyFunc(con net.Conn, instanceCnf *config.Instance, args []string) error {
ylogger.Zero.Info().Msg("Execute copy command")
ylogger.Zero.Info().Str("name", args[0]).Msg("copy")
msg := message.NewCopyMessage(args[0], oldCfgPath, encrypt, decrypt).Encode()
_, err := con.Write(msg)
if err != nil {
return err
}

con, err := net.Dial("unix", instanceCnf.SocketPath)
ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed copy msg")

if err != nil {
return err
}
client := client.NewYClient(con)
protoReader := proc.NewProtoReader(client)

ycl := client.NewYClient(con)
r := proc.NewProtoReader(ycl)
ansType, body, err := protoReader.ReadPacket()
if err != nil {
ylogger.Zero.Debug().Err(err).Msg("error while ans")
return err
}

defer con.Close()
msg := message.NewPutMessage(args[0], encrypt).Encode()
_, err = con.Write(msg)
if err != nil {
return err
}
if ansType != message.MessageTypeReadyForQuery {
return fmt.Errorf("failed to copy, msg: %v", body)
}
return nil
}

ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed put message")
func putFunc(con net.Conn, instanceCnf *config.Instance, args []string) error {
ycl := client.NewYClient(con)
r := proc.NewProtoReader(ycl)

const SZ = 65536
chunk := make([]byte, SZ)
for {
n, err := os.Stdin.Read(chunk)
if n > 0 {
msg := message.NewCopyDataMessage()
msg.Sz = uint64(n)
msg.Data = make([]byte, msg.Sz)
copy(msg.Data, chunk[:n])
msg := message.NewPutMessage(args[0], encrypt).Encode()
_, err := con.Write(msg)
if err != nil {
return err
}

nwr, err := con.Write(msg.Encode())
if err != nil {
return err
}
ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed put message")

ylogger.Zero.Debug().Int("len", nwr).Msg("written copy data msg")
}
const SZ = 65536
chunk := make([]byte, SZ)
for {
n, err := os.Stdin.Read(chunk)
if n > 0 {
msg := message.NewCopyDataMessage()
msg.Sz = uint64(n)
msg.Data = make([]byte, msg.Sz)
copy(msg.Data, chunk[:n])

if err == nil {
continue
}
if err == io.EOF {
break
} else {
nwr, err := con.Write(msg.Encode())
if err != nil {
return err
}
}

ylogger.Zero.Debug().Msg("send command complete msg")

msg = message.NewCommandCompleteMessage().Encode()
_, err = con.Write(msg)
if err != nil {
return err
ylogger.Zero.Debug().Int("len", nwr).Msg("written copy data msg")
}

tp, _, err := r.ReadPacket()
if err != nil {
if err == nil {
continue
}
if err == io.EOF {
break
} else {
return err
}
}

if tp == message.MessageTypeReadyForQuery {
// ok
ylogger.Zero.Debug().Msg("send command complete msg")

ylogger.Zero.Debug().Msg("got rfq")
} else {
return fmt.Errorf("failed to get rfq")
}
msg = message.NewCommandCompleteMessage().Encode()
_, err = con.Write(msg)
if err != nil {
return err
}

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

if tp == message.MessageTypeReadyForQuery {
ylogger.Zero.Debug().Msg("got rfq")
return nil
},
} else {
return fmt.Errorf("failed to get rfq")
}
}

var listCmd = &cobra.Command{
Use: "list",
Short: "list",
RunE: func(cmd *cobra.Command, args []string) error {

err := config.LoadInstanceConfig(cfgPath)
if err != nil {
return err
}
func listFunc(con net.Conn, instanceCnf *config.Instance, args []string) error {
msg := message.NewListMessage(args[0]).Encode()
_, err := con.Write(msg)
if err != nil {
return err
}

instanceCnf := config.InstanceConfig()
ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed list message")

con, err := net.Dial("unix", instanceCnf.SocketPath)
ycl := client.NewYClient(con)
r := proc.NewProtoReader(ycl)

done := false
res := make([]*storage.ObjectInfo, 0)
for {
if done {
break
}
tp, body, err := r.ReadPacket()
if err != nil {
return err
}

defer con.Close()
msg := message.NewListMessage(args[0]).Encode()
_, err = con.Write(msg)
if err != nil {
return err
switch tp {
case message.MessageTypeObjectMeta:
meta := message.ObjectInfoMessage{}
meta.Decode(body)

res = append(res, meta.Content...)
break
case message.MessageTypeReadyForQuery:
done = true
break
default:
return fmt.Errorf("Incorrect message type: %s", tp.String())
}
}

ylogger.Zero.Debug().Bytes("msg", msg).Msg("constructed list message")
for _, meta := range res {
fmt.Printf("Object: {Name: \"%s\", size: %d}\n", meta.Path, meta.Size)
}
return nil
}

ycl := client.NewYClient(con)
r := proc.NewProtoReader(ycl)
var rootCmd = &cobra.Command{
Use: "",
Short: "",
}

done := false
res := make([]*storage.S3ObjectMeta, 0)
for {
if done {
break
}
tp, body, err := r.ReadPacket()
if err != nil {
return err
}
var catCmd = &cobra.Command{
Use: "cat",
Short: "cat",
Args: cobra.ExactArgs(1),
RunE: Runner(catFunc),
}

switch tp {
case message.MessageTypeObjectMeta:
meta := message.ObjectMetaMessage{}
meta.Decode(body)

res = append(res, meta.Content...)
break
case message.MessageTypeReadyForQuery:
done = true
break
default:
return fmt.Errorf("Incorrect message type: %s", tp.String())
}
}
var copyCmd = &cobra.Command{
Use: "copy",
Short: "copy",
Args: cobra.ExactArgs(1),
RunE: Runner(copyFunc),
}

for _, meta := range res {
fmt.Printf("Object: {Name: \"%s\", size: %d}\n", meta.Path, meta.Size)
}
var putCmd = &cobra.Command{
Use: "put",
Short: "put",
Args: cobra.ExactArgs(1),
RunE: Runner(putFunc),
}

return nil
},
var listCmd = &cobra.Command{
Use: "list",
Short: "list",
Args: cobra.ExactArgs(1),
RunE: Runner(listFunc),
}

func init() {
Expand Down
Loading

0 comments on commit edeb821

Please sign in to comment.