mirror of
https://github.com/jkaninda/go-storage.git
synced 2025-12-06 08:39:39 +01:00
Merge pull request #2 from jkaninda/develop
refactor: refactoring of code to meet all go lint requirements
This commit is contained in:
23
.github/workflows/lint.yml
vendored
Normal file
23
.github/workflows/lint.yml
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
name: Lint
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: Run on Ubuntu
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Clone the code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: '~1.22'
|
||||||
|
|
||||||
|
- name: Run linter
|
||||||
|
uses: golangci/golangci-lint-action@v6
|
||||||
|
with:
|
||||||
|
version: v1.61
|
||||||
44
.golangci.yml
Normal file
44
.golangci.yml
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
run:
|
||||||
|
timeout: 5m
|
||||||
|
allow-parallel-runners: true
|
||||||
|
|
||||||
|
issues:
|
||||||
|
# don't skip warning about doc comments
|
||||||
|
# don't exclude the default set of lint
|
||||||
|
exclude-use-default: false
|
||||||
|
# restore some of the defaults
|
||||||
|
# (fill in the rest as needed)
|
||||||
|
exclude-rules:
|
||||||
|
- path: "internal/*"
|
||||||
|
linters:
|
||||||
|
- dupl
|
||||||
|
- lll
|
||||||
|
- goimports
|
||||||
|
linters:
|
||||||
|
disable-all: true
|
||||||
|
enable:
|
||||||
|
- dupl
|
||||||
|
- errcheck
|
||||||
|
- copyloopvar
|
||||||
|
- ginkgolinter
|
||||||
|
- goconst
|
||||||
|
- gocyclo
|
||||||
|
- gofmt
|
||||||
|
- gosimple
|
||||||
|
- govet
|
||||||
|
- ineffassign
|
||||||
|
# - lll
|
||||||
|
- misspell
|
||||||
|
- nakedret
|
||||||
|
- prealloc
|
||||||
|
- revive
|
||||||
|
- staticcheck
|
||||||
|
- typecheck
|
||||||
|
- unconvert
|
||||||
|
- unparam
|
||||||
|
- unused
|
||||||
|
|
||||||
|
linters-settings:
|
||||||
|
revive:
|
||||||
|
rules:
|
||||||
|
- name: comment-spacings
|
||||||
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module github.com/jkaninda/go-storage
|
module github.com/jkaninda/go-storage
|
||||||
|
|
||||||
go 1.21.0
|
go 1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.5.0
|
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.5.0
|
||||||
|
|||||||
@@ -55,12 +55,12 @@ func createClient(conf Config) (*azblob.Client, error) {
|
|||||||
// Create the service URL
|
// Create the service URL
|
||||||
credential, err := azblob.NewSharedKeyCredential(conf.AccountName, conf.AccountKey)
|
credential, err := azblob.NewSharedKeyCredential(conf.AccountName, conf.AccountKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create credential: %v", err)
|
return nil, fmt.Errorf("failed to create service URL")
|
||||||
}
|
}
|
||||||
serviceURL := fmt.Sprintf("https://%s.blob.core.windows.net/", conf.AccountName)
|
serviceURL := fmt.Sprintf("https://%s.blob.core.windows.net/", conf.AccountName)
|
||||||
client, err := azblob.NewClientWithSharedKeyCredential(serviceURL, credential, nil)
|
client, err := azblob.NewClientWithSharedKeyCredential(serviceURL, credential, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create client: %v", err)
|
return nil, fmt.Errorf("failed to create client")
|
||||||
}
|
}
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,14 +82,24 @@ func NewStorage(conf Config) (pkg.Storage, error) {
|
|||||||
// Copy copies file to the remote server
|
// Copy copies file to the remote server
|
||||||
func (s ftpStorage) Copy(fileName string) error {
|
func (s ftpStorage) Copy(fileName string) error {
|
||||||
ftpClient := s.client
|
ftpClient := s.client
|
||||||
defer ftpClient.Quit()
|
defer func(ftpClient *ftp.ServerConn) {
|
||||||
|
err := ftpClient.Quit()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(ftpClient)
|
||||||
|
|
||||||
filePath := filepath.Join(s.LocalPath, fileName)
|
filePath := filepath.Join(s.LocalPath, fileName)
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open file %s: %w", fileName, err)
|
return fmt.Errorf("failed to open file %s: %w", fileName, err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func(file *os.File) {
|
||||||
|
err := file.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(file)
|
||||||
|
|
||||||
remoteFilePath := filepath.Join(s.RemotePath, fileName)
|
remoteFilePath := filepath.Join(s.RemotePath, fileName)
|
||||||
err = ftpClient.Stor(remoteFilePath, file)
|
err = ftpClient.Stor(remoteFilePath, file)
|
||||||
@@ -104,21 +114,36 @@ func (s ftpStorage) Copy(fileName string) error {
|
|||||||
func (s ftpStorage) CopyFrom(fileName string) error {
|
func (s ftpStorage) CopyFrom(fileName string) error {
|
||||||
ftpClient := s.client
|
ftpClient := s.client
|
||||||
|
|
||||||
defer ftpClient.Quit()
|
defer func(ftpClient *ftp.ServerConn) {
|
||||||
|
err := ftpClient.Quit()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(ftpClient)
|
||||||
|
|
||||||
remoteFilePath := filepath.Join(s.RemotePath, fileName)
|
remoteFilePath := filepath.Join(s.RemotePath, fileName)
|
||||||
r, err := ftpClient.Retr(remoteFilePath)
|
r, err := ftpClient.Retr(remoteFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to retrieve file %s: %w", fileName, err)
|
return fmt.Errorf("failed to retrieve file %s: %w", fileName, err)
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer func(r *ftp.Response) {
|
||||||
|
err := r.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(r)
|
||||||
|
|
||||||
localFilePath := filepath.Join(s.LocalPath, fileName)
|
localFilePath := filepath.Join(s.LocalPath, fileName)
|
||||||
outFile, err := os.Create(localFilePath)
|
outFile, err := os.Create(localFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create local file %s: %w", fileName, err)
|
return fmt.Errorf("failed to create local file %s: %w", fileName, err)
|
||||||
}
|
}
|
||||||
defer outFile.Close()
|
defer func(outFile *os.File) {
|
||||||
|
err := outFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(outFile)
|
||||||
|
|
||||||
_, err = io.Copy(outFile, r)
|
_, err = io.Copy(outFile, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -116,7 +116,12 @@ func copyFile(src, dst string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer in.Close()
|
defer func(in *os.File) {
|
||||||
|
err := in.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(in)
|
||||||
|
|
||||||
out, err := os.Create(dst)
|
out, err := os.Create(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -125,7 +130,10 @@ func copyFile(src, dst string) error {
|
|||||||
|
|
||||||
_, err = io.Copy(out, in)
|
_, err = io.Copy(out, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
out.Close()
|
err := out.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return out.Close()
|
return out.Close()
|
||||||
|
|||||||
@@ -45,7 +45,12 @@ func createFile(fileName, content string) ([]byte, error) {
|
|||||||
fmt.Println("Error creating file:", err)
|
fmt.Println("Error creating file:", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func(file *os.File) {
|
||||||
|
err := file.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(file)
|
||||||
|
|
||||||
// Write the message to the file
|
// Write the message to the file
|
||||||
_, err = file.WriteString(content)
|
_, err = file.WriteString(content)
|
||||||
|
|||||||
20
pkg/s3/s3.go
20
pkg/s3/s3.go
@@ -94,7 +94,12 @@ func (s s3Storage) Copy(fileName string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func(file *os.File) {
|
||||||
|
err := file.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(file)
|
||||||
|
|
||||||
fileInfo, err := file.Stat()
|
fileInfo, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -102,7 +107,10 @@ func (s s3Storage) Copy(fileName string) error {
|
|||||||
}
|
}
|
||||||
objectKey := filepath.Join(s.RemotePath, fileName)
|
objectKey := filepath.Join(s.RemotePath, fileName)
|
||||||
buffer := make([]byte, fileInfo.Size())
|
buffer := make([]byte, fileInfo.Size())
|
||||||
file.Read(buffer)
|
_, err = file.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
fileBytes := bytes.NewReader(buffer)
|
fileBytes := bytes.NewReader(buffer)
|
||||||
fileType := http.DetectContentType(buffer)
|
fileType := http.DetectContentType(buffer)
|
||||||
|
|
||||||
@@ -126,7 +134,13 @@ func (s s3Storage) CopyFrom(fileName string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func(file *os.File) {
|
||||||
|
err := file.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error closing file: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(file)
|
||||||
|
|
||||||
objectKey := filepath.Join(s.RemotePath, fileName)
|
objectKey := filepath.Join(s.RemotePath, fileName)
|
||||||
|
|
||||||
|
|||||||
@@ -121,7 +121,12 @@ func (s sshStorage) CopyFrom(fileName string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("couldn't open the output file")
|
return errors.New("couldn't open the output file")
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func(file *os.File) {
|
||||||
|
err := file.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}(file)
|
||||||
|
|
||||||
err = client.CopyFromRemote(context.Background(), file, filepath.Join(s.RemotePath, fileName))
|
err = client.CopyFromRemote(context.Background(), file, filepath.Join(s.RemotePath, fileName))
|
||||||
|
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ type Storage interface {
|
|||||||
Name() string
|
Name() string
|
||||||
}
|
}
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
//Local Path
|
// Local Path
|
||||||
LocalPath string
|
LocalPath string
|
||||||
//Remote path or Destination path
|
// Remote path or Destination path
|
||||||
RemotePath string
|
RemotePath string
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user