@@ -22,15 +22,15 @@ type createOptions struct {
2222 showStatus bool
2323}
2424
25- func newCreateCmd () * cobra.Command {
25+ func newCreateCmd (app * App ) * cobra.Command {
2626 opts := & createOptions {}
2727
2828 createCmd := & cobra.Command {
2929 Use : "create" ,
3030 Short : "Create a codespace" ,
3131 Args : noArgsConstraint ,
3232 RunE : func (cmd * cobra.Command , args []string ) error {
33- return create ( opts )
33+ return app . Create ( cmd . Context (), opts )
3434 },
3535 }
3636
@@ -42,12 +42,10 @@ func newCreateCmd() *cobra.Command {
4242 return createCmd
4343}
4444
45- func create (opts * createOptions ) error {
46- ctx := context .Background ()
47- apiClient := api .New (GithubToken )
48- locationCh := getLocation (ctx , apiClient )
49- userCh := getUser (ctx , apiClient )
50- log := output .NewLogger (os .Stdout , os .Stderr , false )
45+ // Create creates a new Codespace
46+ func (a * App ) Create (ctx context.Context , opts * createOptions ) error {
47+ locationCh := getLocation (ctx , a .apiClient )
48+ userCh := getUser (ctx , a .apiClient )
5149
5250 repo , err := getRepoName (opts .repo )
5351 if err != nil {
@@ -58,7 +56,7 @@ func create(opts *createOptions) error {
5856 return fmt .Errorf ("error getting branch name: %w" , err )
5957 }
6058
61- repository , err := apiClient .GetRepository (ctx , repo )
59+ repository , err := a . apiClient .GetRepository (ctx , repo )
6260 if err != nil {
6361 return fmt .Errorf ("error getting repository: %w" , err )
6462 }
@@ -73,34 +71,34 @@ func create(opts *createOptions) error {
7371 return fmt .Errorf ("error getting codespace user: %w" , userResult .Err )
7472 }
7573
76- machine , err := getMachineName (ctx , opts .machine , userResult .User , repository , branch , locationResult .Location , apiClient )
74+ machine , err := getMachineName (ctx , opts .machine , userResult .User , repository , branch , locationResult .Location , a . apiClient )
7775 if err != nil {
7876 return fmt .Errorf ("error getting machine type: %w" , err )
7977 }
8078 if machine == "" {
8179 return errors .New ("there are no available machine types for this repository" )
8280 }
8381
84- log .Print ("Creating your codespace..." )
85- codespace , err := apiClient .CreateCodespace (ctx , log , & api.CreateCodespaceParams {
82+ a . logger .Print ("Creating your codespace..." )
83+ codespace , err := a . apiClient .CreateCodespace (ctx , a . logger , & api.CreateCodespaceParams {
8684 User : userResult .User .Login ,
8785 RepositoryID : repository .ID ,
8886 Branch : branch ,
8987 Machine : machine ,
9088 Location : locationResult .Location ,
9189 })
92- log .Print ("\n " )
90+ a . logger .Print ("\n " )
9391 if err != nil {
9492 return fmt .Errorf ("error creating codespace: %w" , err )
9593 }
9694
9795 if opts .showStatus {
98- if err := showStatus (ctx , log , apiClient , userResult .User , codespace ); err != nil {
96+ if err := showStatus (ctx , a . logger , a . apiClient , userResult .User , codespace ); err != nil {
9997 return fmt .Errorf ("show status: %w" , err )
10098 }
10199 }
102100
103- log .Printf ("Codespace created: " )
101+ a . logger .Printf ("Codespace created: " )
104102
105103 fmt .Fprintln (os .Stdout , codespace .Name )
106104
@@ -110,7 +108,7 @@ func create(opts *createOptions) error {
110108// showStatus polls the codespace for a list of post create states and their status. It will keep polling
111109// until all states have finished. Once all states have finished, we poll once more to check if any new
112110// states have been introduced and stop polling otherwise.
113- func showStatus (ctx context.Context , log * output.Logger , apiClient * api. API , user * api.User , codespace * api.Codespace ) error {
111+ func showStatus (ctx context.Context , log * output.Logger , apiClient apiClient , user * api.User , codespace * api.Codespace ) error {
114112 var lastState codespaces.PostCreateState
115113 var breakNextState bool
116114
@@ -177,7 +175,7 @@ type getUserResult struct {
177175}
178176
179177// getUser fetches the user record associated with the GITHUB_TOKEN
180- func getUser (ctx context.Context , apiClient * api. API ) <- chan getUserResult {
178+ func getUser (ctx context.Context , apiClient apiClient ) <- chan getUserResult {
181179 ch := make (chan getUserResult , 1 )
182180 go func () {
183181 user , err := apiClient .GetUser (ctx )
@@ -192,7 +190,7 @@ type locationResult struct {
192190}
193191
194192// getLocation fetches the closest Codespace datacenter region/location to the user.
195- func getLocation (ctx context.Context , apiClient * api. API ) <- chan locationResult {
193+ func getLocation (ctx context.Context , apiClient apiClient ) <- chan locationResult {
196194 ch := make (chan locationResult , 1 )
197195 go func () {
198196 location , err := apiClient .GetCodespaceRegionLocation (ctx )
@@ -236,7 +234,7 @@ func getBranchName(branch string) (string, error) {
236234}
237235
238236// getMachineName prompts the user to select the machine type, or validates the machine if non-empty.
239- func getMachineName (ctx context.Context , machine string , user * api.User , repo * api.Repository , branch , location string , apiClient * api. API ) (string , error ) {
237+ func getMachineName (ctx context.Context , machine string , user * api.User , repo * api.Repository , branch , location string , apiClient apiClient ) (string , error ) {
240238 skus , err := apiClient .GetCodespacesSKUs (ctx , user , repo , branch , location )
241239 if err != nil {
242240 return "" , fmt .Errorf ("error requesting machine instance types: %w" , err )
0 commit comments