From 099b0cd8497d0c0005f4ff6331508262c425160a Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Tue, 23 Nov 2021 14:14:45 -0800 Subject: [PATCH] Add filesystem to itctl --- api/fs.go | 2 +- cmd/itctl/filesystem/filesystem.go | 35 +++++++++++++++ cmd/itctl/filesystem/list.go | 56 +++++++++++++++++++++++ cmd/itctl/filesystem/mkdir.go | 49 ++++++++++++++++++++ cmd/itctl/filesystem/move.go | 50 +++++++++++++++++++++ cmd/itctl/filesystem/read.go | 69 ++++++++++++++++++++++++++++ cmd/itctl/filesystem/remove.go | 50 +++++++++++++++++++++ cmd/itctl/filesystem/write.go | 72 ++++++++++++++++++++++++++++++ cmd/itctl/main.go | 1 + 9 files changed, 383 insertions(+), 1 deletion(-) create mode 100644 cmd/itctl/filesystem/filesystem.go create mode 100644 cmd/itctl/filesystem/list.go create mode 100644 cmd/itctl/filesystem/mkdir.go create mode 100644 cmd/itctl/filesystem/move.go create mode 100644 cmd/itctl/filesystem/read.go create mode 100644 cmd/itctl/filesystem/remove.go create mode 100644 cmd/itctl/filesystem/write.go diff --git a/api/fs.go b/api/fs.go index edb4eb0..82dd2c1 100644 --- a/api/fs.go +++ b/api/fs.go @@ -84,7 +84,7 @@ func (c *Client) WriteFile(path, data string) error { _, err := c.request(types.Request{ Type: types.ReqTypeFS, Data: types.ReqDataFS{ - Type: types.FSTypeRead, + Type: types.FSTypeWrite, Files: []string{path}, Data: data, }, diff --git a/cmd/itctl/filesystem/filesystem.go b/cmd/itctl/filesystem/filesystem.go new file mode 100644 index 0000000..6206918 --- /dev/null +++ b/cmd/itctl/filesystem/filesystem.go @@ -0,0 +1,35 @@ +/* + * itd uses bluetooth low energy to communicate with InfiniTime devices + * Copyright (C) 2021 Arsen Musayelyan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package filesystem + +import ( + "github.com/spf13/cobra" + "go.arsenm.dev/itd/cmd/itctl/root" +) + +// filesystemCmd represents the get command +var filesystemCmd = &cobra.Command{ + Use: "filesystem", + Aliases: []string{"fs"}, + Short: "Perform filesystem operations on the PineTime", +} + +func init() { + root.RootCmd.AddCommand(filesystemCmd) +} diff --git a/cmd/itctl/filesystem/list.go b/cmd/itctl/filesystem/list.go new file mode 100644 index 0000000..258f4ce --- /dev/null +++ b/cmd/itctl/filesystem/list.go @@ -0,0 +1,56 @@ +/* + * itd uses bluetooth low energy to communicate with InfiniTime devices + * Copyright (C) 2021 Arsen Musayelyan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package filesystem + +import ( + "fmt" + + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.arsenm.dev/itd/api" +) + +// listCmd represents the heart command +var listCmd = &cobra.Command{ + Use: "list [path]", + Aliases: []string{"ls"}, + Short: "List a directory", + Run: func(cmd *cobra.Command, args []string) { + dirPath := "/" + if len(args) > 0 { + dirPath = args[0] + } + + client := viper.Get("client").(*api.Client) + + listing, err := client.ReadDir(dirPath) + if err != nil { + log.Fatal().Err(err).Msg("Error getting directory listing") + } + + for _, entry := range listing { + fmt.Println(entry) + } + }, +} + +func init() { + filesystemCmd.AddCommand(listCmd) +} diff --git a/cmd/itctl/filesystem/mkdir.go b/cmd/itctl/filesystem/mkdir.go new file mode 100644 index 0000000..5a6c056 --- /dev/null +++ b/cmd/itctl/filesystem/mkdir.go @@ -0,0 +1,49 @@ +/* + * itd uses bluetooth low energy to communicate with InfiniTime devices + * Copyright (C) 2021 Arsen Musayelyan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package filesystem + +import ( + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.arsenm.dev/itd/api" +) + +// heartCmd represents the heart command +var mkdirCmd = &cobra.Command{ + Use: "mkdir ", + Short: "Create a new directory", + Run: func(cmd *cobra.Command, args []string) { + if len(args) < 1 { + cmd.Usage() + log.Fatal().Msg("Command mkdir requires one or more arguments") + } + + client := viper.Get("client").(*api.Client) + + err := client.Mkdir(args...) + if err != nil { + log.Fatal().Err(err).Msg("Error creating directory") + } + }, +} + +func init() { + filesystemCmd.AddCommand(mkdirCmd) +} diff --git a/cmd/itctl/filesystem/move.go b/cmd/itctl/filesystem/move.go new file mode 100644 index 0000000..688594c --- /dev/null +++ b/cmd/itctl/filesystem/move.go @@ -0,0 +1,50 @@ +/* + * itd uses bluetooth low energy to communicate with InfiniTime devices + * Copyright (C) 2021 Arsen Musayelyan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package filesystem + +import ( + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.arsenm.dev/itd/api" +) + +// heartCmd represents the heart command +var moveCmd = &cobra.Command{ + Use: "move ", + Aliases: []string{"mv"}, + Short: "Move a file or directory", + Run: func(cmd *cobra.Command, args []string) { + if len(args) != 2 { + cmd.Usage() + log.Fatal().Msg("Command move requires two arguments") + } + + client := viper.Get("client").(*api.Client) + + err := client.Rename(args[0], args[1]) + if err != nil { + log.Fatal().Err(err).Msg("Error moving file or directory") + } + }, +} + +func init() { + filesystemCmd.AddCommand(moveCmd) +} diff --git a/cmd/itctl/filesystem/read.go b/cmd/itctl/filesystem/read.go new file mode 100644 index 0000000..5bece4c --- /dev/null +++ b/cmd/itctl/filesystem/read.go @@ -0,0 +1,69 @@ +/* + * itd uses bluetooth low energy to communicate with InfiniTime devices + * Copyright (C) 2021 Arsen Musayelyan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package filesystem + +import ( + "os" + "time" + + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.arsenm.dev/itd/api" +) + +// heartCmd represents the heart command +var readCmd = &cobra.Command{ + Use: `read `, + Short: "Read a file from InfiniTime", + Run: func(cmd *cobra.Command, args []string) { + if len(args) != 2 { + cmd.Usage() + log.Fatal().Msg("Command read requires two arguments") + } + + start := time.Now() + client := viper.Get("client").(*api.Client) + + data, err := client.ReadFile(args[0]) + if err != nil { + log.Fatal().Err(err).Msg("Error moving file or directory") + } + + var out *os.File + if args[1] == "-" { + out = os.Stdout + } else { + out, err = os.Create(args[1]) + if err != nil { + log.Fatal().Err(err).Msg("Error opening local file") + } + } + + n, err := out.WriteString(data) + if err != nil { + log.Fatal().Err(err).Msg("Error writing to local file") + } + log.Info().Msgf("Read %d bytes in %s", n, time.Since(start)) + }, +} + +func init() { + filesystemCmd.AddCommand(readCmd) +} diff --git a/cmd/itctl/filesystem/remove.go b/cmd/itctl/filesystem/remove.go new file mode 100644 index 0000000..600633d --- /dev/null +++ b/cmd/itctl/filesystem/remove.go @@ -0,0 +1,50 @@ +/* + * itd uses bluetooth low energy to communicate with InfiniTime devices + * Copyright (C) 2021 Arsen Musayelyan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package filesystem + +import ( + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.arsenm.dev/itd/api" +) + +// heartCmd represents the heart command +var removeCmd = &cobra.Command{ + Use: "remove ", + Aliases: []string{"rm"}, + Short: "Create a new directory", + Run: func(cmd *cobra.Command, args []string) { + if len(args) < 1 { + cmd.Usage() + log.Fatal().Msg("Command mkdir requires one or more arguments") + } + + client := viper.Get("client").(*api.Client) + + err := client.Remove(args...) + if err != nil { + log.Fatal().Err(err).Msg("Error removing file or directory") + } + }, +} + +func init() { + filesystemCmd.AddCommand(removeCmd) +} diff --git a/cmd/itctl/filesystem/write.go b/cmd/itctl/filesystem/write.go new file mode 100644 index 0000000..c73f32f --- /dev/null +++ b/cmd/itctl/filesystem/write.go @@ -0,0 +1,72 @@ +/* + * itd uses bluetooth low energy to communicate with InfiniTime devices + * Copyright (C) 2021 Arsen Musayelyan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package filesystem + +import ( + "io" + "os" + "time" + + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.arsenm.dev/itd/api" +) + +// heartCmd represents the heart command +var writeCmd = &cobra.Command{ + Use: `write `, + Short: "Write a file to InfiniTime", + Run: func(cmd *cobra.Command, args []string) { + if len(args) != 2 { + cmd.Usage() + log.Fatal().Msg("Command write requires two arguments") + } + + start := time.Now() + client := viper.Get("client").(*api.Client) + + var in *os.File + if args[0] == "-" { + in = os.Stdin + } else { + fl, err := os.Open(args[0]) + if err != nil { + log.Fatal().Err(err).Msg("Error opening local file") + } + in = fl + } + + data, err := io.ReadAll(in) + if err != nil { + log.Fatal().Err(err).Msg("Error moving file or directory") + } + + err = client.WriteFile(args[1], string(data)) + if err != nil { + log.Fatal().Err(err).Msg("Error writing to remote file") + } + + log.Info().Msgf("Wrote %d bytes in %s", len(data), time.Since(start)) + }, +} + +func init() { + filesystemCmd.AddCommand(writeCmd) +} diff --git a/cmd/itctl/main.go b/cmd/itctl/main.go index f63e12e..cc88f7d 100644 --- a/cmd/itctl/main.go +++ b/cmd/itctl/main.go @@ -25,6 +25,7 @@ import ( "go.arsenm.dev/itd/cmd/itctl/root" _ "go.arsenm.dev/itd/cmd/itctl/set" _ "go.arsenm.dev/itd/cmd/itctl/watch" + _ "go.arsenm.dev/itd/cmd/itctl/filesystem" "os"