refactor: refactoring of code to meet all go lint requirements

This commit is contained in:
Jonas Kaninda
2024-12-06 16:25:16 +01:00
parent 2bcfd3aacf
commit 793b04340e
10 changed files with 141 additions and 17 deletions

View File

@@ -55,12 +55,12 @@ func createClient(conf Config) (*azblob.Client, error) {
// Create the service URL
credential, err := azblob.NewSharedKeyCredential(conf.AccountName, conf.AccountKey)
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)
client, err := azblob.NewClientWithSharedKeyCredential(serviceURL, credential, nil)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
return nil, fmt.Errorf("failed to create client")
}
return client, nil
}

View File

@@ -82,14 +82,24 @@ func NewStorage(conf Config) (pkg.Storage, error) {
// Copy copies file to the remote server
func (s ftpStorage) Copy(fileName string) error {
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)
file, err := os.Open(filePath)
if err != nil {
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)
err = ftpClient.Stor(remoteFilePath, file)
@@ -104,21 +114,36 @@ func (s ftpStorage) Copy(fileName string) error {
func (s ftpStorage) CopyFrom(fileName string) error {
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)
r, err := ftpClient.Retr(remoteFilePath)
if err != nil {
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)
outFile, err := os.Create(localFilePath)
if err != nil {
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)
if err != nil {

View File

@@ -116,7 +116,12 @@ func copyFile(src, dst string) error {
if err != nil {
return err
}
defer in.Close()
defer func(in *os.File) {
err := in.Close()
if err != nil {
return
}
}(in)
out, err := os.Create(dst)
if err != nil {
@@ -125,7 +130,10 @@ func copyFile(src, dst string) error {
_, err = io.Copy(out, in)
if err != nil {
out.Close()
err := out.Close()
if err != nil {
return err
}
return err
}
return out.Close()

View File

@@ -45,7 +45,12 @@ func createFile(fileName, content string) ([]byte, error) {
fmt.Println("Error creating file:", 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
_, err = file.WriteString(content)

View File

@@ -94,7 +94,12 @@ func (s s3Storage) Copy(fileName string) error {
if err != nil {
return err
}
defer file.Close()
defer func(file *os.File) {
err := file.Close()
if err != nil {
return
}
}(file)
fileInfo, err := file.Stat()
if err != nil {
@@ -102,7 +107,10 @@ func (s s3Storage) Copy(fileName string) error {
}
objectKey := filepath.Join(s.RemotePath, fileName)
buffer := make([]byte, fileInfo.Size())
file.Read(buffer)
_, err = file.Read(buffer)
if err != nil {
return err
}
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
@@ -126,7 +134,13 @@ func (s s3Storage) CopyFrom(fileName string) error {
if err != nil {
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)

View File

@@ -121,7 +121,12 @@ func (s sshStorage) CopyFrom(fileName string) error {
if err != nil {
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))

View File

@@ -31,8 +31,8 @@ type Storage interface {
Name() string
}
type Backend struct {
//Local Path
// Local Path
LocalPath string
//Remote path or Destination path
// Remote path or Destination path
RemotePath string
}