HW7 is completed
parent
feb9196574
commit
7091b48675
|
@ -2,14 +2,75 @@ package main
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/mitchellh/ioprogress"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnsupportedFile = errors.New("unsupported file")
|
||||
ErrThisIsDirectory = errors.New("this is directory")
|
||||
ErrOffsetExceedsFileSize = errors.New("offset exceeds file size")
|
||||
ErrNoInFile = errors.New("не указан исходный файл или он не существует")
|
||||
)
|
||||
|
||||
func Copy(fromPath string, toPath string, offset, limit int64) error {
|
||||
// Place your code here
|
||||
fIn, err := os.Open(fromPath)
|
||||
defer func() {
|
||||
if fIn.Close() != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
return ErrNoInFile
|
||||
}
|
||||
|
||||
fInInfo, err := fIn.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fInMode := fInInfo.Mode()
|
||||
if fInMode.IsDir() {
|
||||
return ErrThisIsDirectory
|
||||
}
|
||||
if !fInMode.IsRegular() {
|
||||
return ErrUnsupportedFile
|
||||
}
|
||||
|
||||
fInSize := fInInfo.Size()
|
||||
|
||||
switch {
|
||||
case offset >= fInSize:
|
||||
return ErrOffsetExceedsFileSize
|
||||
case limit == 0:
|
||||
limit = fInSize - offset
|
||||
case fInSize < limit+offset:
|
||||
limit = fInSize - offset
|
||||
default:
|
||||
}
|
||||
|
||||
fOut, err := os.Create(toPath)
|
||||
defer func() {
|
||||
if fOut.Close() != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pg := &ioprogress.Reader{
|
||||
Reader: fIn,
|
||||
Size: limit,
|
||||
DrawFunc: ioprogress.DrawTerminalf(os.Stdout, ioprogress.DrawTextFormatBar(100)),
|
||||
}
|
||||
|
||||
if _, err = fIn.Seek(offset, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = io.CopyN(fOut, pg, limit); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,27 @@
|
|||
package main
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
// Place your code here
|
||||
t.Run("Не существует исходный файл", func(t *testing.T) {
|
||||
err := Copy("testdata/novalid.txt", "out.txt", 0, 0)
|
||||
require.Equal(t, err, ErrNoInFile)
|
||||
})
|
||||
|
||||
t.Run("Исходный файл является папкой", func(t *testing.T) {
|
||||
err := Copy("testdata", "out.txt", 0, 0)
|
||||
|
||||
require.Equal(t, err, ErrThisIsDirectory)
|
||||
})
|
||||
|
||||
t.Run("Офсет больше размера исходного файла", func(t *testing.T) {
|
||||
err := Copy("testdata/input.txt", "out.txt", 8000, 0)
|
||||
|
||||
require.Equal(t, err, ErrOffsetExceedsFileSize)
|
||||
})
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +1,10 @@
|
|||
module github.com/fixme_my_friend/hw07_file_copying
|
||||
module github.com/tiburon-777/HW_OTUS/hw07_file_copying
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/cheggaaa/pb/v3 v3.0.4
|
||||
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e
|
||||
github.com/sirupsen/logrus v1.6.0
|
||||
github.com/stretchr/testify v1.6.1
|
||||
)
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
|
||||
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
|
||||
github.com/cheggaaa/pb v1.0.28 h1:kWGpdAcSp3MxMU9CCHOwz/8V0kCHN4+9yQm2MzWuI98=
|
||||
github.com/cheggaaa/pb v2.0.7+incompatible h1:gLKifR1UkZ/kLkda5gC0K6c8g+jU2sINPtBeOiNlMhU=
|
||||
github.com/cheggaaa/pb/v3 v3.0.4 h1:QZEPYOj2ix6d5oEg63fbHmpolrnNiwjUsk+h74Yt4bM=
|
||||
github.com/cheggaaa/pb/v3 v3.0.4/go.mod h1:7rgWxLrAUcFMkvJuv09+DYi7mMUYi8nO9iOWcvGJPfw=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
|
||||
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
|
||||
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
|
||||
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e h1:Qa6dnn8DlasdXRnacluu8HzPts0S1I9zvvUPDbBnXFI=
|
||||
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e/go.mod h1:waEya8ee1Ro/lgxpVhkJI4BVASzkm3UZqkx/cFJiYHM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
|
||||
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
|
||||
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -18,5 +18,7 @@ func init() {
|
|||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
// Place your code here
|
||||
if Copy(from, to, offset, limit) != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue