22package start
33
44import (
5- "bufio"
65 "fmt"
76 "net/url"
8- "os"
97 "path/filepath"
108 "strings"
119 "time"
1210
1311 "github.com/brevdev/brev-cli/pkg/cmd/completions"
14- "github.com/brevdev/brev-cli/pkg/cmd/util"
12+ cmdutil "github.com/brevdev/brev-cli/pkg/cmd/util"
1513 "github.com/brevdev/brev-cli/pkg/config"
1614 "github.com/brevdev/brev-cli/pkg/entity"
15+ breverrors "github.com/brevdev/brev-cli/pkg/errors"
1716 "github.com/brevdev/brev-cli/pkg/featureflag"
1817 "github.com/brevdev/brev-cli/pkg/instancetypes"
1918 "github.com/brevdev/brev-cli/pkg/mergeshells"
2019 "github.com/brevdev/brev-cli/pkg/store"
2120 "github.com/brevdev/brev-cli/pkg/terminal"
22- allutil "github.com/brevdev/brev-cli/pkg/util"
21+ "github.com/brevdev/brev-cli/pkg/util"
2322 "github.com/spf13/cobra"
24-
25- breverrors "github.com/brevdev/brev-cli/pkg/errors"
2623)
2724
2825var (
3633)
3734
3835type StartStore interface {
39- util .GetWorkspaceByNameOrIDErrStore
36+ cmdutil .GetWorkspaceByNameOrIDErrStore
4037 GetWorkspaces (organizationID string , options * store.GetWorkspacesOptions ) ([]entity.Workspace , error )
4138 GetActiveOrganizationOrDefault () (* entity.Organization , error )
4239 GetCurrentUser () (* entity.User , error )
@@ -68,8 +65,8 @@ func NewCmdStart(t *terminal.Terminal, startStore StartStore, noLoginStartStore
6865 Example : startExample ,
6966 ValidArgsFunction : completions .GetAllWorkspaceNameCompletionHandler (noLoginStartStore , t ),
7067 RunE : func (cmd * cobra.Command , args []string ) error {
71- piped := isStdoutPiped ()
72- names , stdinPiped := getInstanceNamesFromStdin (args )
68+ piped := cmdutil . IsStdoutPiped ()
69+ names , stdinPiped := cmdutil . GetInstanceNamesWithPipeInfo (args )
7370
7471 if gpu != "" {
7572 isValid := instancetypes .ValidateInstanceType (gpu )
@@ -162,7 +159,7 @@ func runStartWorkspace(t *terminal.Terminal, options StartOptions, startStore St
162159}
163160
164161func maybeStartWithLocalPath (options StartOptions , user * entity.User , t * terminal.Terminal , startStore StartStore ) (bool , error ) {
165- if allutil .DoesPathExist (options .RepoOrPathOrNameOrID ) {
162+ if util .DoesPathExist (options .RepoOrPathOrNameOrID ) {
166163 err := startWorkspaceFromPath (user , t , options , startStore )
167164 if err != nil {
168165 return false , breverrors .WrapAndTrace (err )
@@ -194,7 +191,7 @@ func maybeStartStoppedOrJoin(t *terminal.Terminal, user *entity.User, options St
194191 return false , breverrors .NewValidationError (fmt .Sprintf ("workspace with id/name %s is a failed workspace" , options .RepoOrPathOrNameOrID ))
195192 }
196193 }
197- if allutil .DoesPathExist (options .RepoOrPathOrNameOrID ) {
194+ if util .DoesPathExist (options .RepoOrPathOrNameOrID ) {
198195 t .Print (t .Yellow (fmt .Sprintf ("Warning: local path found and instance name/id found %s. Using instance name/id. If you meant to specify a local path change directory and try again." , options .RepoOrPathOrNameOrID )))
199196 }
200197 errr := startStopppedWorkspace (& userWorkspaces [0 ], startStore , t , options )
@@ -215,7 +212,7 @@ func maybeStartStoppedOrJoin(t *terminal.Terminal, user *entity.User, options St
215212}
216213
217214func maybeStartFromGitURL (t * terminal.Terminal , user * entity.User , options StartOptions , startStore StartStore ) (bool , error ) {
218- if allutil .IsGitURL (options .RepoOrPathOrNameOrID ) { // todo this is function is not complete, some cloneable urls are not identified
215+ if util .IsGitURL (options .RepoOrPathOrNameOrID ) { // todo this is function is not complete, some cloneable urls are not identified
219216 err := createNewWorkspaceFromGit (user , t , options .SetupScript , options , startStore )
220217 if err != nil {
221218 return true , breverrors .WrapAndTrace (err )
@@ -237,7 +234,7 @@ func maybeStartEmpty(t *terminal.Terminal, user *entity.User, options StartOptio
237234}
238235
239236func startWorkspaceFromPath (user * entity.User , t * terminal.Terminal , options StartOptions , startStore StartStore ) error {
240- pathExists := allutil .DoesPathExist (options .RepoOrPathOrNameOrID )
237+ pathExists := util .DoesPathExist (options .RepoOrPathOrNameOrID )
241238 if ! pathExists {
242239 return fmt .Errorf ("Path: %s does not exist" , options .RepoOrPathOrNameOrID )
243240 }
@@ -267,7 +264,7 @@ func startWorkspaceFromPath(user *entity.User, t *terminal.Terminal, options Sta
267264 if options .RepoOrPathOrNameOrID == "." {
268265 localSetupPath = filepath .Join (".brev" , "setup.sh" )
269266 }
270- if ! allutil .DoesPathExist (localSetupPath ) {
267+ if ! util .DoesPathExist (localSetupPath ) {
271268 fmt .Println (strings .Join ([]string {"Generating setup script at" , localSetupPath }, "\n " ))
272269 mergeshells .ImportPath (t , options .RepoOrPathOrNameOrID , startStore )
273270 fmt .Println ("setup script generated." )
@@ -679,35 +676,6 @@ func pollUntil(t *terminal.Terminal, wsid string, state string, startStore Start
679676 return nil
680677}
681678
682- // isStdoutPiped returns true if stdout is being piped to another command
683- func isStdoutPiped () bool {
684- stat , _ := os .Stdout .Stat ()
685- return (stat .Mode () & os .ModeCharDevice ) == 0
686- }
687-
688- // getInstanceNamesFromStdin returns instance names from args and stdin if piped
689- // Returns the names and whether stdin was piped
690- func getInstanceNamesFromStdin (args []string ) ([]string , bool ) {
691- var names []string
692- names = append (names , args ... )
693-
694- // Check if stdin is piped
695- stat , _ := os .Stdin .Stat ()
696- stdinPiped := (stat .Mode () & os .ModeCharDevice ) == 0
697-
698- if stdinPiped {
699- scanner := bufio .NewScanner (os .Stdin )
700- for scanner .Scan () {
701- name := strings .TrimSpace (scanner .Text ())
702- if name != "" {
703- names = append (names , name )
704- }
705- }
706- }
707-
708- return names , stdinPiped
709- }
710-
711679// runBatchStart handles starting multiple instances when stdin is piped
712680func runBatchStart (t * terminal.Terminal , names []string , org , setupScript , setupRepo , setupPath , cpu , gpu string , piped bool , startStore StartStore ) error {
713681 var startedNames []string
0 commit comments