Add filesystem to itctl

This commit is contained in:
Elara 2021-11-23 14:14:45 -08:00
parent 49a7a51527
commit 082fdc02f4
9 changed files with 383 additions and 1 deletions

View File

@ -84,7 +84,7 @@ func (c *Client) WriteFile(path, data string) error {
_, err := c.request(types.Request{ _, err := c.request(types.Request{
Type: types.ReqTypeFS, Type: types.ReqTypeFS,
Data: types.ReqDataFS{ Data: types.ReqDataFS{
Type: types.FSTypeRead, Type: types.FSTypeWrite,
Files: []string{path}, Files: []string{path},
Data: data, Data: data,
}, },

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <path...>",
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <old> <new>",
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <remote path> <local path | "-">`,
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <path...>",
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <local path | "-"> <remote path>`,
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)
}

View File

@ -25,6 +25,7 @@ import (
"go.arsenm.dev/itd/cmd/itctl/root" "go.arsenm.dev/itd/cmd/itctl/root"
_ "go.arsenm.dev/itd/cmd/itctl/set" _ "go.arsenm.dev/itd/cmd/itctl/set"
_ "go.arsenm.dev/itd/cmd/itctl/watch" _ "go.arsenm.dev/itd/cmd/itctl/watch"
_ "go.arsenm.dev/itd/cmd/itctl/filesystem"
"os" "os"