Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ci lint for mod registry and image #1

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions image/default_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,15 @@ func (d DefaultImageService) uploadLayers(repo string, layers []v1.Layer, blobs
Action: func(cxt progress.Context) error {
var file *os.File
defer func() {
//file compress failed, clean file
if err != nil {
_ = utils.CleanFile(file)
utils.CleanFile(file)
}
}()

if file, err = compress.Compress(filepath.Join(common.DefaultLayerDir, layer.Hash), "", nil); err != nil {
errCh <- err
return err
//flow.ShowMessage(shortHex+" "+err.Error(), compressBar)
}
// pass to next progress task
cxt.WithReader(file)
Expand Down
13 changes: 10 additions & 3 deletions image/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func GetClusterFileFromImageManifest(imageName string) string {
func GetClusterFileFromBaseImage(imageName string) string {
mountTarget, _ := utils.MkTmpdir()
mountUpper, _ := utils.MkTmpdir()
defer utils.CleanDirs(mountTarget, mountUpper)
defer func() {
_ = utils.CleanDirs(mountTarget, mountUpper)
justadogistaken marked this conversation as resolved.
Show resolved Hide resolved
}()

if err := NewImageService().PullIfNotExist(imageName); err != nil {
return ""
Expand All @@ -107,9 +109,14 @@ func GetClusterFileFromBaseImage(imageName string) string {
}

err = driver.Mount(mountTarget, mountUpper, layers...)
defer driver.Unmount(mountTarget)
clusterFile := filepath.Join(mountTarget, "etc", common.DefaultClusterFileName)
if err != nil {
return ""
}
defer func() {
_ = driver.Unmount(mountTarget)
}()

clusterFile := filepath.Join(mountTarget, "etc", common.DefaultClusterFileName)
data, err := ioutil.ReadFile(clusterFile)
if err != nil {
return ""
Expand Down
6 changes: 3 additions & 3 deletions registry/tokentransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func oauthFlow(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/oauth2/accesstoken") {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"access_token":"abcdef1234"}`))
_, _ = w.Write([]byte(`{"access_token":"abcdef1234"}`))
return
}
if strings.HasPrefix(r.URL.Path, "/oauth2/token") {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"token":"abcdef1234"}`))
_, _ = w.Write([]byte(`{"token":"abcdef1234"}`))
return
}
auth := r.Header.Get("authorization")
Expand All @@ -63,7 +63,7 @@ func oauthFlow(w http.ResponseWriter, r *http.Request) {
w.Header().Set("www-authenticate", `Bearer realm="`+authURI+`/oauth2/token",service="my.endpoint.here"`)
}
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}`))
_, _ = w.Write([]byte(`{"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":null}]}`))
return
}
w.WriteHeader(http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion utils/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Compress(src, newFolder string, existingFile *os.File) (file *os.File, err

defer func() {
if err != nil {
_ = utils.CleanFile(file)
utils.CleanFile(file)
}
}()

Expand Down
14 changes: 11 additions & 3 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,20 @@ func CopySingleFile(src, dst string) (int64, error) {
return nBytes, err
}

func CleanFile(file *os.File) (err error) {
func CleanFile(file *os.File) {
if file == nil {
return
}
err = file.Close()
return os.Remove(file.Name())
// the following operation won't failed regularly, if failed, log it
err := file.Close()
if err != nil {
logger.Warn(err)
}
err = os.Remove(file.Name())
if err != nil {
logger.Warn(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recommended way is to return the error, caller decision write log or not

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose os.Remove/Close ... won't return err regularly. So if error occurs, we log it.

}
return
}

func CleanDir(dir string) (err error) {
Expand Down