11package autostop
22
33import (
4+ "github.com/hashicorp/go-multierror"
5+ "github.com/samber/lo"
6+ "github.com/samber/mo"
47 "github.com/spf13/cobra"
58
69 "github.com/brevdev/brev-cli/pkg/entity"
@@ -22,24 +25,23 @@ func NewCmdautostop(t *terminal.Terminal, store autostopStore) *cobra.Command {
2225 Long : long ,
2326 Example : example ,
2427 RunE : func (cmd * cobra.Command , args []string ) error {
25- err := Runautostop (
26- runAutostopArgs {
27- t : t ,
28- args : args ,
29- store : store ,
30- },
31- )
32- if err != nil {
33- return breverrors .WrapAndTrace (err )
34- }
35- return nil
28+ return breverrors .WrapAndTrace (
29+ Runautostop (
30+ runAutostopArgs {
31+ t : t ,
32+ args : args ,
33+ store : store ,
34+ },
35+ ))
3636 },
3737 }
3838 return cmd
3939}
4040
4141type autostopStore interface {
4242 AutoStopWorkspace (workspaceID string ) (* entity.Workspace , error )
43+ GetActiveOrganizationOrDefault () (* entity.Organization , error )
44+ GetWorkspaceByNameOrID (orgID string , nameOrID string ) ([]entity.Workspace , error )
4345}
4446
4547type runAutostopArgs struct {
@@ -49,6 +51,28 @@ type runAutostopArgs struct {
4951}
5052
5153func Runautostop (args runAutostopArgs ) error {
52- _ , err := args .store .AutoStopWorkspace (args .args [0 ])
53- return breverrors .WrapAndTrace (err )
54+ org , err := args .store .GetActiveOrganizationOrDefault ()
55+ if err != nil {
56+ return breverrors .WrapAndTrace (err )
57+ }
58+ envs , err := args .store .GetWorkspaceByNameOrID (org .ID , args .args [0 ])
59+ if err != nil {
60+ return breverrors .WrapAndTrace (err )
61+ }
62+
63+ asResults := lo .Map (
64+ envs ,
65+ func (env entity.Workspace , _ int ) mo.Result [* entity.Workspace ] {
66+ return mo .TupleToResult (args .store .AutoStopWorkspace (env .ID ))
67+ },
68+ )
69+ return breverrors .WrapAndTrace (
70+ lo .Reduce (
71+ asResults ,
72+ func (acc error , res mo.Result [* entity.Workspace ], _ int ) error {
73+ return multierror .Append (acc , res .Error ())
74+ },
75+ nil ,
76+ ),
77+ )
5478}
0 commit comments