forked from Elara6331/itd
Directly read/write files from ITD
This commit is contained in:
@@ -19,8 +19,9 @@
|
||||
package filesystem
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -38,33 +39,32 @@ var readCmd = &cobra.Command{
|
||||
log.Fatal().Msg("Command read requires two arguments")
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
var tmpFile *os.File
|
||||
var path string
|
||||
var err error
|
||||
if args[1] == "-" {
|
||||
tmpFile, err = ioutil.TempFile("/tmp", "itctl.*")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error creating temporary file")
|
||||
}
|
||||
path = tmpFile.Name()
|
||||
} else {
|
||||
path = args[1]
|
||||
}
|
||||
|
||||
client := viper.Get("client").(*api.Client)
|
||||
|
||||
data, err := client.ReadFile(args[0])
|
||||
err = client.ReadFile(path, args[0])
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error moving file or directory")
|
||||
log.Fatal().Err(err).Msg("Error reading remote file")
|
||||
}
|
||||
|
||||
var suffix string
|
||||
var out *os.File
|
||||
if args[1] == "-" {
|
||||
out = os.Stdout
|
||||
suffix = "\n"
|
||||
} else {
|
||||
out, err = os.Create(args[1])
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error opening local file")
|
||||
}
|
||||
io.Copy(os.Stdout, tmpFile)
|
||||
os.Stdout.WriteString("\n")
|
||||
tmpFile.Close()
|
||||
os.Remove(path)
|
||||
}
|
||||
|
||||
n, err := out.WriteString(data)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error writing to local file")
|
||||
}
|
||||
out.WriteString(suffix)
|
||||
|
||||
log.Info().Msgf("Read %d bytes in %s", n, time.Since(start))
|
||||
},
|
||||
}
|
||||
|
||||
|
@@ -20,8 +20,8 @@ package filesystem
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -39,31 +39,31 @@ var writeCmd = &cobra.Command{
|
||||
log.Fatal().Msg("Command write requires two arguments")
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
client := viper.Get("client").(*api.Client)
|
||||
|
||||
var in *os.File
|
||||
var tmpFile *os.File
|
||||
var path string
|
||||
var err error
|
||||
if args[0] == "-" {
|
||||
in = os.Stdin
|
||||
} else {
|
||||
fl, err := os.Open(args[0])
|
||||
tmpFile, err = ioutil.TempFile("/tmp", "itctl.*")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error opening local file")
|
||||
log.Fatal().Err(err).Msg("Error creating temporary file")
|
||||
}
|
||||
in = fl
|
||||
path = tmpFile.Name()
|
||||
} else {
|
||||
path = args[0]
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(in)
|
||||
client := viper.Get("client").(*api.Client)
|
||||
|
||||
if args[0] == "-" {
|
||||
io.Copy(tmpFile, os.Stdin)
|
||||
defer tmpFile.Close()
|
||||
defer os.Remove(path)
|
||||
}
|
||||
|
||||
err = client.WriteFile(path, args[1])
|
||||
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))
|
||||
},
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user