Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func checkForUpdate(ctx context.Context, currentVersion string) (*version.Releas
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
retcode := 0
defer func() { os.Exit(retcode) }()

// start update check process in the background
ctx := context.Background()
updateCtx, updateCancel := context.WithCancel(ctx)
Expand All @@ -118,7 +121,8 @@ func Execute() {
// run command
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
retcode = 1
return
}

updateCancel() // if the update checker hasn't completed by now, abort it
Expand Down
39 changes: 29 additions & 10 deletions state/user_cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state

import (
"errors"
"io"
"os"
"path/filepath"
Expand All @@ -17,28 +18,37 @@ func WriteToCache(name string, data []byte) error {
}

err = os.Mkdir(path, os.FileMode(0o700))

if err != nil {
if !os.IsExist(err) {
return err
}
if err != nil && !os.IsExist(err) {
return err
}

filename := filepath.Join(path, name)
logrus.WithFields(logrus.Fields{"filename": filename}).Debug("writing to user cache")

file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
err = file.Chmod(os.FileMode(0o600))

err = file.Chmod(os.FileMode(0o600))
if err != nil {
file.Close() // Ensure file is closed even if chmod fails
return err
}

_, err = file.Write(data)
if err != nil {
file.Close() // Ensure file is closed even if writing fails
return err
}

return err
// Close the file and check for errors
err = file.Close()
if err != nil {
return errors.New("error closing file: " + err.Error())
}

return nil
}

func ReadFromCache(name string) ([]byte, error) {
Expand All @@ -53,9 +63,18 @@ func ReadFromCache(name string) ([]byte, error) {
if err != nil {
return nil, err
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
file.Close()
return nil, err
}

err = file.Close()
if err != nil {
return nil, errors.New("error closing file: " + err.Error())
}

return io.ReadAll(file)
return data, nil
}

func ClearCache() error {
Expand Down