-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #248 from depot/feat/create-projects
feat: add create project command stub
- Loading branch information
Showing
11 changed files
with
161 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Creates a depot project. | ||
package projects | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
corev1 "buf.build/gen/go/depot/api/protocolbuffers/go/depot/core/v1" | ||
"connectrpc.com/connect" | ||
"github.com/depot/cli/pkg/api" | ||
"github.com/depot/cli/pkg/helpers" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdCreate() *cobra.Command { | ||
var ( | ||
token string | ||
orgID string | ||
region string | ||
keepGigabytes int64 | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "create [flags] <project-name>", | ||
Aliases: []string{"c"}, | ||
Args: cobra.MaximumNArgs(1), | ||
Short: "Create depot project", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return fmt.Errorf("project name is required") | ||
} | ||
ctx := cmd.Context() | ||
projectName := args[0] | ||
|
||
token, err := helpers.ResolveToken(ctx, token) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if token == "" { | ||
return fmt.Errorf("missing API token, please run `depot login`") | ||
} | ||
|
||
projectClient := api.NewSDKProjectsClient() | ||
req := corev1.CreateProjectRequest{ | ||
Name: projectName, | ||
RegionId: region, | ||
CachePolicy: &corev1.CachePolicy{ | ||
KeepBytes: keepGigabytes * 1024 * 1024 * 1024, | ||
}, | ||
} | ||
if orgID != "" { | ||
req.OrganizationId = &orgID | ||
} | ||
|
||
res, err := projectClient.CreateProject(ctx, api.WithAuthentication(connect.NewRequest(&req), token)) | ||
if err != nil { | ||
return err | ||
} | ||
project := NewCreateResponse(res.Msg.GetProject()) | ||
buf, err := json.Marshal(project) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%s\n", buf) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.SortFlags = false | ||
flags.StringVar(&token, "token", "", "Depot token") | ||
flags.StringVarP(&orgID, "organization", "o", "", "Depot organization ID") | ||
flags.StringVar(®ion, "region", "us-east-1", "Build data will be stored in the chosen region") | ||
flags.Int64Var(&keepGigabytes, "cache-storage-policy", 50, "Build cache to keep per architecture in GB") | ||
|
||
return cmd | ||
} | ||
|
||
type CreateResponse struct { | ||
ProjectId string `json:"project_id,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
CachePolicy *corev1.CachePolicy `json:"cache_policy,omitempty"` | ||
} | ||
|
||
func NewCreateResponse(project *corev1.Project) *CreateResponse { | ||
return &CreateResponse{ | ||
ProjectId: project.GetProjectId(), | ||
Name: project.GetName(), | ||
CachePolicy: project.GetCachePolicy(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package projects | ||
|
||
import ( | ||
"github.com/depot/cli/pkg/cmd/list" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdProjects() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "projects", | ||
Aliases: []string{"p"}, | ||
Short: "Create or display depot project information", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
|
||
cmd.AddCommand(NewCmdCreate()) | ||
cmd.AddCommand(list.NewCmdProjects("list", "ls")) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
version: v1 | ||
deps: | ||
- buf.build/depot/api | ||
- buf.build/depot/buildkit | ||
breaking: | ||
use: | ||
|