Skip to content

Commit 51e0725

Browse files
committed
fix linter errors
1 parent 69690af commit 51e0725

10 files changed

Lines changed: 180 additions & 155 deletions

File tree

src/api/v1/file_manager_impl.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package v1
22

33
import (
44
"encoding/json"
5-
. "github.com/makeopensource/leviathan/common"
6-
. "github.com/makeopensource/leviathan/service/file_manager"
5+
com "github.com/makeopensource/leviathan/common"
6+
fm "github.com/makeopensource/leviathan/service/file_manager"
77
"github.com/rs/zerolog/log"
88
"mime/multipart"
99
"net/http"
@@ -20,15 +20,15 @@ type FileManagerHandler struct {
2020
BasePath string
2121
UploadLabPath string
2222
UploadSubmissionPath string
23-
service FileManagerService
23+
service fm.FileManagerService
2424
}
2525

2626
func NewFileManagerHandler(basePath string) *FileManagerHandler {
2727
return &FileManagerHandler{
2828
BasePath: basePath,
2929
UploadLabPath: basePath + "/upload/lab",
3030
UploadSubmissionPath: basePath + "/upload/submission",
31-
service: FileManagerService{},
31+
service: fm.FileManagerService{},
3232
}
3333
}
3434

@@ -44,7 +44,6 @@ func (f *FileManagerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4444
}
4545

4646
w.WriteHeader(http.StatusMethodNotAllowed)
47-
return
4847
}
4948

5049
func (f *FileManagerHandler) UploadLabData(w http.ResponseWriter, r *http.Request) {
@@ -58,12 +57,12 @@ func (f *FileManagerHandler) UploadLabData(w http.ResponseWriter, r *http.Reques
5857
if err != nil {
5958
http.Error(
6059
w,
61-
ErrLog("Failed to get dockerfile in form", err, log.Error()).Error(),
60+
com.ErrLog("Failed to get dockerfile in form", err, log.Error()).Error(),
6261
http.StatusBadRequest,
6362
)
6463
return
6564
}
66-
defer CloseFile(dockerFile)
65+
defer com.CloseFile(dockerFile)
6766

6867
jobFiles, ok := r.MultipartForm.File[LabFilesKey]
6968
if !ok || len(jobFiles) == 0 {
@@ -76,9 +75,9 @@ func (f *FileManagerHandler) UploadLabData(w http.ResponseWriter, r *http.Reques
7675
http.Error(w, err.Error(), http.StatusBadRequest)
7776
return
7877
}
79-
defer func(files []*FileInfo) {
78+
defer func(files []*fm.FileInfo) {
8079
for _, file := range files {
81-
CloseFile(file.Reader)
80+
com.CloseFile(file.Reader)
8281
}
8382
}(fileInfos)
8483

@@ -88,7 +87,7 @@ func (f *FileManagerHandler) UploadLabData(w http.ResponseWriter, r *http.Reques
8887
return
8988
}
9089

91-
sendResponse(w, err, folderID)
90+
sendResponse(w, folderID)
9291
}
9392

9493
func (f *FileManagerHandler) UploadSubmissionData(w http.ResponseWriter, r *http.Request) {
@@ -109,9 +108,9 @@ func (f *FileManagerHandler) UploadSubmissionData(w http.ResponseWriter, r *http
109108
http.Error(w, err.Error(), http.StatusBadRequest)
110109
return
111110
}
112-
defer func(files []*FileInfo) {
111+
defer func(files []*fm.FileInfo) {
113112
for _, file := range files {
114-
CloseFile(file.Reader)
113+
com.CloseFile(file.Reader)
115114
}
116115
}(fileInfos)
117116

@@ -121,11 +120,11 @@ func (f *FileManagerHandler) UploadSubmissionData(w http.ResponseWriter, r *http
121120
return
122121
}
123122

124-
sendResponse(w, err, folderID)
123+
sendResponse(w, folderID)
125124
}
126125

127-
func sendResponse(w http.ResponseWriter, err error, folderID string) {
128-
jsonData, err := toJson(folderID, err)
126+
func sendResponse(w http.ResponseWriter, folderID string) {
127+
jsonData, err := toJson(folderID)
129128
if err != nil {
130129
http.Error(w, err.Error(), http.StatusInternalServerError)
131130
return
@@ -135,32 +134,32 @@ func sendResponse(w http.ResponseWriter, err error, folderID string) {
135134
if err != nil {
136135
http.Error(
137136
w,
138-
ErrLog("Failed to write response", err, log.Error()).Error(),
137+
com.ErrLog("Failed to write response", err, log.Error()).Error(),
139138
http.StatusInternalServerError,
140139
)
141140
return
142141
}
143142
}
144143

145-
func toJson(folderID string, err error) ([]byte, error) {
144+
func toJson(folderID string) ([]byte, error) {
146145
resultMap := map[string]string{
147146
"folderId": folderID,
148147
}
149148
jsonData, err := json.Marshal(resultMap)
150149
if err != nil {
151-
return nil, ErrLog("Failed to marshal json", err, log.Error())
150+
return nil, com.ErrLog("Failed to marshal json", err, log.Error())
152151
}
153152
return jsonData, nil
154153
}
155154

156-
func mapToFileInfo(jobFiles []*multipart.FileHeader) ([]*FileInfo, error) {
157-
var fileInfos []*FileInfo
155+
func mapToFileInfo(jobFiles []*multipart.FileHeader) ([]*fm.FileInfo, error) {
156+
var fileInfos []*fm.FileInfo
158157
for _, jobFile := range jobFiles {
159158
file, err := jobFile.Open()
160159
if err != nil {
161-
return fileInfos, ErrLog("unable to open file: "+err.Error(), err, log.Error())
160+
return fileInfos, com.ErrLog("unable to open file: "+err.Error(), err, log.Error())
162161
}
163-
fileInfos = append(fileInfos, &FileInfo{
162+
fileInfos = append(fileInfos, &fm.FileInfo{
164163
Reader: file,
165164
Filename: jobFile.Filename,
166165
})

src/common/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func FileConsoleLogger() zerolog.Logger {
5252
//
5353
// This hides implementation details from users while ensuring full error information is available for debugging.
5454
func ErrLog(message string, err error, eventLevel *zerolog.Event) error {
55-
eventLevel.Err(err).Msgf(message)
56-
return fmt.Errorf(message)
55+
eventLevel.Err(err).Msg(message)
56+
return fmt.Errorf("%s", message)
5757
}
5858

5959
func ConsoleLogger() zerolog.Logger {

src/common/utils.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import (
1111

1212
func FileExists(filename string) bool {
1313
_, err := os.Stat(filename)
14-
if os.IsNotExist(err) {
15-
return false
16-
}
17-
return true
14+
return !os.IsNotExist(err)
1815
}
1916

2017
func CloseFile(file io.ReadCloser) {

src/models/job_error.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package models
2+
3+
type JobError interface {
4+
// Reason will be displayed to the end user, providing a user-friendly message.
5+
Reason() string
6+
// Err err parameter holds the underlying error, used for debugging purposes.
7+
Err() error
8+
// ErrStr returns string from the error, if nil return empty string
9+
ErrStr() string
10+
}
11+
12+
// JErr implements JobError
13+
type JErr struct {
14+
reason string
15+
err error
16+
}
17+
18+
func JError(reason string, err error) JErr {
19+
return JErr{reason: reason, err: err}
20+
}
21+
22+
func (err JErr) Reason() string {
23+
return err.reason
24+
}
25+
26+
func (err JErr) Err() error {
27+
return err.err
28+
}
29+
30+
func (err JErr) ErrStr() string {
31+
if err.err != nil {
32+
return err.err.Error()
33+
}
34+
return ""
35+
}

src/service/docker/docker_utils_ssh.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"crypto/x509"
1010
"encoding/pem"
1111
"fmt"
12-
. "github.com/makeopensource/leviathan/common"
12+
com "github.com/makeopensource/leviathan/common"
1313
"github.com/makeopensource/leviathan/models"
1414
"github.com/rs/zerolog/log"
1515
"github.com/spf13/viper"
@@ -70,7 +70,7 @@ func saveHostKey(machine models.MachineOptions) func(hostname string, remote net
7070
}
7171

7272
func writeMachineToConfigFile(machine models.MachineOptions) {
73-
machineKey := fmt.Sprintf("%s.%s", ClientsSSH.ConfigKey, machine.Name())
73+
machineKey := fmt.Sprintf("%s.%s", com.ClientsSSH.ConfigKey, machine.Name())
7474
viper.Set(machineKey, machine)
7575
err := viper.WriteConfig()
7676
if err != nil {
@@ -109,7 +109,7 @@ func GenerateKeyPair() (privateKey []byte, publicKey []byte, err error) {
109109
//
110110
// the generated keys can be found in common.SSHConfigFolder
111111
func initKeyPairFile() {
112-
basePath := SSHConfigFolder.GetStr()
112+
basePath := com.SSHConfigFolder.GetStr()
113113
privateKeyPath := fmt.Sprintf("%s/%s", basePath, "id_rsa")
114114
publicKeyPath := fmt.Sprintf("%s/%s", basePath, "id_rsa.pub")
115115

@@ -120,7 +120,7 @@ func initKeyPairFile() {
120120
Str("private_key_file", privateKeyPath).
121121
Str("public_key_file", publicKeyPath)
122122

123-
if FileExists(privateKeyPath) && FileExists(publicKeyPath) {
123+
if com.FileExists(privateKeyPath) && com.FileExists(publicKeyPath) {
124124
logF.Msg("found existing keys... skipping generation")
125125
return
126126
}
@@ -143,7 +143,7 @@ func initKeyPairFile() {
143143
func LoadPrivateKey() ([]byte, error) {
144144
return os.ReadFile(fmt.Sprintf(
145145
"%s/%s",
146-
SSHConfigFolder.GetStr(),
146+
com.SSHConfigFolder.GetStr(),
147147
"id_rsa",
148148
))
149149
}

src/service/file_manager/file_manager_service.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package file_manager
33
import (
44
"fmt"
55
"github.com/google/uuid"
6-
. "github.com/makeopensource/leviathan/common"
6+
com "github.com/makeopensource/leviathan/common"
77
"github.com/rs/zerolog/log"
88
"io"
99
"os"
@@ -36,7 +36,7 @@ func (f *FileManagerService) CreateTmpLabFolder(dockerfile io.Reader, jobFiles .
3636
jobDataDir := filepath.Join(basePath, JobDataFolderName)
3737
err = os.MkdirAll(jobDataDir, os.ModePerm)
3838
if err != nil {
39-
return "", ErrLog("unable to create job data folder", err, log.Error())
39+
return "", com.ErrLog("unable to create job data folder", err, log.Error())
4040
}
4141

4242
if err = f.SaveFile(basePath, DockerfileName, dockerfile); err != nil {
@@ -71,14 +71,14 @@ func (f *FileManagerService) CreateSubmissionFolder(jobFiles ...*FileInfo) (stri
7171
func (f *FileManagerService) createBaseFolder() (string, string, error) {
7272
folderUUID, err := uuid.NewUUID()
7373
if err != nil {
74-
return "", "", ErrLog("Unable to generate uuid", err, log.Error())
74+
return "", "", com.ErrLog("Unable to generate uuid", err, log.Error())
7575
}
7676
stringUuid := folderUUID.String()
77-
basePath := filepath.Join(TmpUploadFolder.GetStr(), stringUuid)
77+
basePath := filepath.Join(com.TmpUploadFolder.GetStr(), stringUuid)
7878

79-
err = os.Mkdir(basePath, DefaultFilePerm)
79+
err = os.Mkdir(basePath, com.DefaultFilePerm)
8080
if err != nil {
81-
return "", "", ErrLog("Unable to create tmp folder", err, log.Error())
81+
return "", "", com.ErrLog("Unable to create tmp folder", err, log.Error())
8282
}
8383

8484
return folderUUID.String(), basePath, nil
@@ -90,7 +90,7 @@ func (f *FileManagerService) SaveFile(basePath string, filename string, file io.
9090
dst, err := os.Create(fPath)
9191

9292
if err != nil {
93-
return ErrLog(
93+
return com.ErrLog(
9494
"Failed to create destination file",
9595
err,
9696
log.Error(),
@@ -106,7 +106,7 @@ func (f *FileManagerService) SaveFile(basePath string, filename string, file io.
106106
// Copy the file contents
107107
written, err := io.Copy(dst, file)
108108
if err != nil {
109-
return ErrLog(
109+
return com.ErrLog(
110110
"Failed to write file",
111111
err,
112112
log.Error(),
@@ -122,14 +122,14 @@ func (f *FileManagerService) SaveFile(basePath string, filename string, file io.
122122
}
123123

124124
func (f *FileManagerService) DeleteFolder(folderUuid string) {
125-
basePath := filepath.Join(TmpUploadFolder.GetStr(), folderUuid)
125+
basePath := filepath.Join(com.TmpUploadFolder.GetStr(), folderUuid)
126126
if err := os.RemoveAll(basePath); err != nil {
127127
log.Warn().Err(err).Msgf("failed to delete tmp folder %s", folderUuid)
128128
}
129129
}
130130

131131
func (f *FileManagerService) GetLabFilePaths(folderUuid string) (basePath string, err error) {
132-
basePath = filepath.Join(TmpUploadFolder.GetStr(), folderUuid)
132+
basePath = filepath.Join(com.TmpUploadFolder.GetStr(), folderUuid)
133133
jobData := filepath.Join(basePath, JobDataFolderName)
134134
dockerFile := filepath.Join(basePath, DockerfileName)
135135

@@ -144,12 +144,12 @@ func (f *FileManagerService) GetLabFilePaths(folderUuid string) (basePath string
144144
}
145145

146146
func (f *FileManagerService) GetSubmissionPath(uuid string) (string, error) {
147-
path := filepath.Join(TmpUploadFolder.GetStr(), uuid)
147+
path := filepath.Join(com.TmpUploadFolder.GetStr(), uuid)
148148
return f.checkFolder(path)
149149
}
150150

151151
func (f *FileManagerService) checkFolder(path string) (jobData string, err error) {
152-
if !FileExists(path) {
152+
if !com.FileExists(path) {
153153
return "", fmt.Errorf("could not find path")
154154
}
155155
return path, err

0 commit comments

Comments
 (0)