From 040a24b5a095d1d497e291d7a25a2ef5ed69eb7e Mon Sep 17 00:00:00 2001 From: CHIKAMATSU Naohiro Date: Tue, 21 Mar 2023 23:05:25 +0900 Subject: [PATCH] Delete internal file package. use gorky/file pacakage instead --- cmd/export.go | 4 +-- cmd/export_test.go | 2 +- cmd/import.go | 2 +- cmd/remove.go | 2 +- cmd/remove_test.go | 2 +- cmd/root_test.go | 2 +- go.mod | 2 +- go.sum | 6 ++-- internal/assets/asset.go | 2 +- internal/completion/completion.go | 2 +- internal/config/config.go | 34 ++++++++++++++++-- internal/file/file.go | 57 ------------------------------- 12 files changed, 45 insertions(+), 72 deletions(-) delete mode 100644 internal/file/file.go diff --git a/cmd/export.go b/cmd/export.go index 76500a2..bfcb346 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -4,8 +4,8 @@ import ( "fmt" "os" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/config" - "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/goutil" "github.com/nao1215/gup/internal/print" "github.com/spf13/cobra" @@ -32,7 +32,7 @@ installation according to the contents of gup.conf.`, return cmd } -func export(cmd *cobra.Command, args []string) int { +func export(cmd *cobra.Command, _ []string) int { if err := goutil.CanUseGoCmd(); err != nil { print.Err(fmt.Errorf("%s: %w", "you didn't install golang", err)) return 1 diff --git a/cmd/export_test.go b/cmd/export_test.go index 598cfec..20cec73 100644 --- a/cmd/export_test.go +++ b/cmd/export_test.go @@ -12,8 +12,8 @@ import ( "github.com/adrg/xdg" "github.com/google/go-cmp/cmp" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/config" - "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/goutil" "github.com/nao1215/gup/internal/print" "github.com/spf13/cobra" diff --git a/cmd/import.go b/cmd/import.go index f690dbb..8ad65cf 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -4,8 +4,8 @@ import ( "fmt" "runtime" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/config" - "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/print" "github.com/spf13/cobra" ) diff --git a/cmd/remove.go b/cmd/remove.go index a500159..ff4a0a4 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -7,7 +7,7 @@ import ( "runtime" "strings" - "github.com/nao1215/gup/internal/file" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/goutil" "github.com/nao1215/gup/internal/print" "github.com/spf13/cobra" diff --git a/cmd/remove_test.go b/cmd/remove_test.go index ef112bb..5733415 100644 --- a/cmd/remove_test.go +++ b/cmd/remove_test.go @@ -11,7 +11,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" - "github.com/nao1215/gup/internal/file" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/print" "github.com/spf13/cobra" ) diff --git a/cmd/root_test.go b/cmd/root_test.go index de56547..012f3d8 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -11,9 +11,9 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/cmdinfo" "github.com/nao1215/gup/internal/config" - "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/goutil" "github.com/nao1215/gup/internal/print" ) diff --git a/go.mod b/go.mod index 325c71c..f7dbf36 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/gen2brain/beeep v0.0.0-20220909211152-5a9ec94374f6 github.com/google/go-cmp v0.5.9 github.com/mattn/go-colorable v0.1.13 - github.com/nao1215/gorky v0.2.0 + github.com/nao1215/gorky v0.2.1 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.6.1 golang.org/x/sync v0.1.0 diff --git a/go.sum b/go.sum index 616f84e..c48dddc 100644 --- a/go.sum +++ b/go.sum @@ -21,8 +21,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/nao1215/gorky v0.2.0 h1:dGhccwANGFbA2wryX0fkRe3O7h+Pov63hNQxSmKypgA= -github.com/nao1215/gorky v0.2.0/go.mod h1:RHHEeEhkdrCuNEduNB2i4RCgkJWiAD6gK+AZq0Kyx44= +github.com/nao1215/gorky v0.2.1 h1:kxXYhCNBbtGru9CCSYx+QC0JZfZJ1csY3uLbb5n2WKA= +github.com/nao1215/gorky v0.2.1/go.mod h1:fJNLiXzn3YkteARC8xghfHjkt+C5xtHOaRgmVnJEMOs= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ= github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -37,7 +37,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk= github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= diff --git a/internal/assets/asset.go b/internal/assets/asset.go index f4f5b73..2dbf55e 100644 --- a/internal/assets/asset.go +++ b/internal/assets/asset.go @@ -6,8 +6,8 @@ import ( "os" "path/filepath" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/config" - "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/print" ) diff --git a/internal/completion/completion.go b/internal/completion/completion.go index dda835d..e1c354f 100644 --- a/internal/completion/completion.go +++ b/internal/completion/completion.go @@ -8,8 +8,8 @@ import ( "runtime" "strings" + "github.com/nao1215/gorky/file" "github.com/nao1215/gup/internal/cmdinfo" - "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/print" "github.com/spf13/cobra" ) diff --git a/internal/config/config.go b/internal/config/config.go index 16dd80a..31eadb1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,8 +2,10 @@ package config import ( + "bufio" "errors" "fmt" + "io" "os" "path/filepath" "regexp" @@ -11,7 +13,6 @@ import ( "github.com/adrg/xdg" "github.com/nao1215/gup/internal/cmdinfo" - "github.com/nao1215/gup/internal/file" "github.com/nao1215/gup/internal/goutil" ) @@ -31,7 +32,7 @@ func DirPath() string { // ReadConfFile return contents of configuration-file (package information) func ReadConfFile(path string) ([]goutil.Package, error) { - contents, err := file.ReadFileToList(path) + contents, err := readFileToList(path) if err != nil { return nil, fmt.Errorf("can't read %s: %w", path, err) } @@ -86,3 +87,32 @@ func deleteComment(line string) string { r := regexp.MustCompile(`#./*`) return r.ReplaceAllString(line, "") } + +// readFileToList convert file content to string list. +func readFileToList(path string) ([]string, error) { + var strList []string + f, err := os.Open(filepath.Clean(path)) + if err != nil { + return nil, err + } + defer func() { + if closeErr := f.Close(); closeErr != nil { + // TODO: If use go 1.20, rewrite like this. + // err = errors.Join(err, closeErr) + err = closeErr // overwrite error + } + }() + + r := bufio.NewReader(f) + for { + line, err := r.ReadString('\n') + if err != nil && err != io.EOF { + return nil, err + } + if err == io.EOF && len(line) == 0 { + break + } + strList = append(strList, line) + } + return strList, nil +} diff --git a/internal/file/file.go b/internal/file/file.go deleted file mode 100644 index c591ee9..0000000 --- a/internal/file/file.go +++ /dev/null @@ -1,57 +0,0 @@ -package file - -import ( - "bufio" - "io" - "io/fs" - "os" - "path/filepath" -) - -const ( - // FileModeCreatingDir is used for creating directory - FileModeCreatingDir fs.FileMode = 0750 - // FileModeCreatingFile is used for creating directory - FileModeCreatingFile fs.FileMode = 0600 -) - -// ReadFileToList convert file content to string list. -func ReadFileToList(path string) ([]string, error) { - var strList []string - f, err := os.Open(filepath.Clean(path)) - if err != nil { - return nil, err - } - defer func() { - if closeErr := f.Close(); closeErr != nil { - // TODO: If use go 1.20, rewrite like this. - // err = errors.Join(err, closeErr) - err = closeErr // overwrite error - } - }() - - r := bufio.NewReader(f) - for { - line, err := r.ReadString('\n') - if err != nil && err != io.EOF { - return nil, err - } - if err == io.EOF && len(line) == 0 { - break - } - strList = append(strList, line) - } - return strList, nil -} - -// IsFile reports whether the path exists and is a file. -func IsFile(path string) bool { - stat, err := os.Stat(path) - return (err == nil) && (!stat.IsDir()) -} - -// IsDir reports whether the path exists and is a directory. -func IsDir(path string) bool { - stat, err := os.Stat(path) - return (err == nil) && (stat.IsDir()) -}