Skip to content

Commit

Permalink
fixed lint errors pt2
Browse files Browse the repository at this point in the history
  • Loading branch information
tqfonger committed Jun 20, 2023
1 parent dfb7410 commit b6b86d6
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions pkg/cmd/background/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type BackgroundStore interface {
func DisableAutoStop(s BackgroundStore, workspaceID string) error {
isStoppable := false
_, err := s.ModifyWorkspace(workspaceID, &store.ModifyWorkspaceRequest{
IsStoppable: &isStoppable,
IsStoppable: &isStoppable,
})
if err != nil {
return breverrors.WrapAndTrace(err)
Expand All @@ -42,7 +42,7 @@ func DisableAutoStop(s BackgroundStore, workspaceID string) error {
func EnableAutoStop(s BackgroundStore, workspaceID string) error {
isStoppable := true
_, err := s.ModifyWorkspace(workspaceID, &store.ModifyWorkspaceRequest{
IsStoppable: &isStoppable,
IsStoppable: &isStoppable,
})
if err != nil {
return breverrors.WrapAndTrace(err)
Expand Down Expand Up @@ -73,7 +73,6 @@ func NewCmdBackground(t *terminal.Terminal, s BackgroundStore) *cobra.Command {
checkProgress()
return nil
}

// Join the args into a command string
command := ""
if len(args) > 0 {
Expand All @@ -82,38 +81,26 @@ func NewCmdBackground(t *terminal.Terminal, s BackgroundStore) *cobra.Command {
for i := 1; i < len(args); i++ {
command += " " + args[i]
}

// Create logs directory if it doesn't exist
logsDir := os.Getenv("HOME") + "/brev-background-logs"
err = os.MkdirAll(logsDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}

wsID, err := s.GetCurrentWorkspaceID()
if err == nil && wsID != "" {
// Disable auto stop
err = DisableAutoStop(s, wsID)
if err != nil {
log.Fatal(err)
}
}

checkAutoStop(s)
// Run the command in the background using nohup
c := exec.Command("nohup", "bash", "-c", command+">"+logsDir+"/log.txt 2>&1 &") // #nosec G204
err = c.Start()
if err != nil {
log.Fatal(err)
}

// Write logs
logFile, err := os.OpenFile(logsDir+"/log.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o666) //nolint: gosec // TODO
if err != nil {
log.Fatal(err)
}

defer logFile.Close() //nolint: gosec,errcheck // TODO

defer logFile.Close() //nolint: gosec,errcheck // TODO
logFile.WriteString(time.Now().Format("2006-01-02 15:04:05") + ": Command \"" + command + "\" was run in the background.\n") //nolint: errcheck,gosec // TODO

// Write process details to data file
Expand All @@ -134,31 +121,46 @@ func NewCmdBackground(t *terminal.Terminal, s BackgroundStore) *cobra.Command {
}
}()
}

// Call analytics for open
userID := ""
user, err := s.GetCurrentUser()
if err != nil {
userID = ""
} else {
userID = user.ID
}
data := analytics.EventData{
EventName: "Brev background",
UserID: userID,
}
_ = analytics.TrackEvent(data)
_ = pushBackgroundAnalytics(s)

t.Vprintf("Command \"%s\" has been run in the background. Check %s for logs.\n", command, logsDir)
return nil
},
}
cmd.Flags().Bool("stop", false, "Stop the workspace after the command is finished")
cmd.Flags().Bool("progress", false, "Show progress of the background commands")

return cmd
}

func checkAutoStop(s BackgroundStore) {
wsID, err := s.GetCurrentWorkspaceID()
if err == nil && wsID != "" {
// Disable auto stop
err = DisableAutoStop(s, wsID)
if err != nil {
log.Fatal(err)
}
}
}

func pushBackgroundAnalytics(s BackgroundStore) error {
// Call analytics for open
userID := ""
user, err := s.GetCurrentUser()
if err != nil {
userID = ""
} else {
userID = user.ID
}
data := analytics.EventData{
EventName: "Brev background",
UserID: userID,
}
err = analytics.TrackEvent(data)
return breverrors.WrapAndTrace(err)
}

type Process struct {
ID int `json:"id"`
Command string `json:"command"`
Expand Down

0 comments on commit b6b86d6

Please sign in to comment.