Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ishandhanani committed Sep 25, 2024
1 parent 6fe4dcb commit 8402ff5
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 64 deletions.
73 changes: 30 additions & 43 deletions cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,45 @@ func authLoginCmd() *cobra.Command {
return &cobra.Command{
Use: "login",
Short: "Authenticate with NVIDIA Cloud",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
apiKey := output.Prompt("Enter your NVIDIA Cloud API key: ", true)

err := config.SetAPIKey(apiKey)
if err != nil {
output.Error(cmd, "Error saving API key", err)
return
return output.Error(cmd, "Error saving API key", err)
}

// Use the API key to get the first org
client := api.NewClient(apiKey)
orgsInfo := map[string]interface{}{}
err = client.Get(cmd.Context(), "/v2/orgs", nil, &orgsInfo)
if err != nil {
output.Error(cmd, "Failed to fetch organization information", err)
return
return output.Error(cmd, "Failed to fetch organization information", err)
}

organizations, ok := orgsInfo["organizations"].([]interface{})
if !ok || len(organizations) == 0 {
output.Error(cmd, "No organizations found", nil)
return
return output.Error(cmd, "No organizations found", nil)
}

firstOrg, ok := organizations[0].(map[string]interface{})
if !ok {
output.Error(cmd, "Failed to parse organization information", nil)
return
return output.Error(cmd, "Failed to parse organization information", nil)
}

orgID, ok := firstOrg["name"].(string)
if !ok {
output.Error(cmd, "Organization ID not found", nil)
return
return output.Error(cmd, "Organization ID not found", nil)
}

err = config.SetOrgID(orgID)
if err != nil {
output.Error(cmd, "Error saving Org ID", err)
return
return output.Error(cmd, "Error saving Org ID", err)
}

output.PrintASCIIArt(cmd)
output.Success(cmd, fmt.Sprintf("Authentication successful. You are now authenticated with organization ID: %s", orgID))
return nil
},
}
}
Expand All @@ -98,29 +93,27 @@ func authConfigureDockerCmd() *cobra.Command {
return &cobra.Command{
Use: "configure-docker",
Short: "Configure Docker to use NGC API key for nvcr.io",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
apiKey := config.GetAPIKey()
if apiKey == "" {
output.Error(cmd, "NGC API key not found. Please run 'nvcf auth login' first.", nil)
return
return output.Error(cmd, "NGC API key not found. Please run 'nvcf auth login' first.", nil)
}
// Check if Docker is installed
_, err := exec.LookPath("docker")
if err != nil {
output.Error(cmd, "Docker is not installed or not in the system PATH", err)
return
return output.Error(cmd, "Docker is not installed or not in the system PATH", err)
}
// TODO: check for existing nvcr.io config?
dockerCmd := exec.Command("docker", "login", "nvcr.io", "-u", "$oauthtoken", "--password-stdin")
dockerCmd.Stdin = strings.NewReader(apiKey)
out, err := dockerCmd.CombinedOutput()
if err != nil {
output.Error(cmd, "Failed to configure Docker", err)
cmd.Println(string(out))
return
return output.Error(cmd, "Failed to configure Docker", err)
}
output.Success(cmd, "Docker configured successfully for nvcr.io")
cmd.Println(string(out))
return nil
},
}
}
Expand Down Expand Up @@ -191,23 +184,21 @@ func authWhoAmICmd() *cobra.Command {
return &cobra.Command{
Use: "whoami",
Short: "Display information about the authenticated user",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
client := api.NewClient(config.GetAPIKey())
whoamiInfo := map[string]any{}
err := client.Get(cmd.Context(), whoamiURL, nil, &whoamiInfo)
if err != nil {
output.Error(cmd, "Failed to fetch user information", err)
return
return output.Error(cmd, "Failed to fetch user information", err)
}

jsonMode, _ := cmd.Flags().GetBool("json")
if jsonMode {
err = json.NewEncoder(cmd.OutOrStdout()).Encode(whoamiInfo)
if err != nil {
output.Error(cmd, "Failed to encode user information", err)
return
return output.Error(cmd, "Failed to encode user information", err)
}
return
return nil
}
userInfo, _ := whoamiInfo["user"].(map[string]any)
table := tablewriter.NewWriter(cmd.OutOrStdout())
Expand All @@ -218,6 +209,7 @@ func authWhoAmICmd() *cobra.Command {
userInfo["name"].(string),
})
table.Render()
return nil
},
}
}
Expand All @@ -226,27 +218,24 @@ func authOrgsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "orgs",
Short: "Display organization and team information for the authenticated user",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
client := api.NewClient(config.GetAPIKey())
userInfo := map[string]interface{}{}
err := client.Get(cmd.Context(), "/v2/users/me", nil, &userInfo)
if err != nil {
output.Error(cmd, "Failed to fetch user information", err)
return
return output.Error(cmd, "Failed to fetch user information", err)
}
jsonMode, _ := cmd.Flags().GetBool("json")
if jsonMode {
err = json.NewEncoder(cmd.OutOrStdout()).Encode(userInfo)
if err != nil {
output.Error(cmd, "Failed to encode user information", err)
return
return output.Error(cmd, "Failed to encode user information", err)
}
return
return nil
}
userRoles, ok := userInfo["userRoles"].([]interface{})
if !ok {
output.Error(cmd, "Failed to parse user roles information", nil)
return
return output.Error(cmd, "Failed to parse user roles information", nil)
}
type OrgTeamInfo struct {
OrgName string
Expand Down Expand Up @@ -304,6 +293,7 @@ func authOrgsCmd() *cobra.Command {
}
}
table.Render()
return nil
},
}

Expand All @@ -315,34 +305,31 @@ func authOrgIDCmd() *cobra.Command {
return &cobra.Command{
Use: "org-id",
Short: "Display the name of the first organization",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
client := api.NewClient(config.GetAPIKey())
orgsInfo := map[string]interface{}{}
err := client.Get(cmd.Context(), "/v2/orgs", nil, &orgsInfo)
if err != nil {
output.Error(cmd, "Failed to fetch organization information", err)
return
return output.Error(cmd, "Failed to fetch organization information", err)
}

organizations, ok := orgsInfo["organizations"].([]interface{})
if !ok || len(organizations) == 0 {
output.Error(cmd, "No organizations found", nil)
return
return output.Error(cmd, "No organizations found", nil)
}

firstOrg, ok := organizations[0].(map[string]interface{})
if !ok {
output.Error(cmd, "Failed to parse organization information", nil)
return
return output.Error(cmd, "Failed to parse organization information", nil)
}

name, ok := firstOrg["name"].(string)
if !ok {
output.Error(cmd, "Organization name not found", nil)
return
return output.Error(cmd, "Organization name not found", nil)
}

fmt.Println(name)
return nil
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/function/function_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ func prepareFunctionVersionParamsFromFile(fnImage string, fn FunctionDef) nvcf.F
func createAndDeployFunctionVersionFromFile(cmd *cobra.Command, client *api.Client, existingFunctionID string, params nvcf.FunctionVersionNewParams, deploy bool, gpu, instanceType, backend string, maxInstances, minInstances, maxRequestConcurrency int64) error {
resp, err := client.Functions.Versions.New(cmd.Context(), existingFunctionID, params)
if err != nil {
output.Error(cmd, "error creating function version", err)
return nil
return output.Error(cmd, "error creating function version", err)
}

output.Success(cmd, fmt.Sprintf("Version %s created for function %s", resp.Function.VersionID, existingFunctionID))
Expand Down Expand Up @@ -466,7 +465,6 @@ func createAndDeployFunctionFromFile(cmd *cobra.Command, client *api.Client, par
func WaitForDeployment(cmd *cobra.Command, client *api.Client, functionID, versionID string) error {
spinner := output.NewSpinner("Waiting for deployment to complete...")
output.StartSpinner(spinner)
defer output.StopSpinner(spinner)

err := timeout.DoWithTimeout(func(ctx context.Context) error {
for ctx.Err() == nil {
Expand All @@ -493,6 +491,8 @@ func WaitForDeployment(cmd *cobra.Command, client *api.Client, functionID, versi
return output.Error(cmd, "Error waiting for deployment", err)
}

output.StopSpinner(spinner)

output.Success(cmd, fmt.Sprintf("\nFunction deployed (ID: %s, Version: %s)", functionID, versionID))
return nil
}
4 changes: 2 additions & 2 deletions cmd/function/function_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func functionDeleteCmd() *cobra.Command {
RunE: runFunctionDelete,
}
cmd.Flags().String("version-id", "", "The ID of the version")
cmd.Flags().Bool("all", false, "Delete all versions of the function")
cmd.Flags().Bool("force", false, "Forcefully delete a deployed function")
cmd.Flags().BoolP("all", "a", false, "Delete all versions of the function")
cmd.Flags().BoolP("force", "f", false, "Forcefully delete a deployed function")
return cmd
}

Expand Down
32 changes: 23 additions & 9 deletions cmd/function/function_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ func functionDeployCmd() *cobra.Command {
Long: `Deploy an existing NVCF function. If you want to deploy a specific version, use the --version-id flag.`,
Example: "nvcf function deploy fid --version-id vid --gpu A100 --instance-type g5.4xlarge",
Args: cobra.ExactArgs(1),
RunE: runFunctionDeploy,
PreRunE: func(cmd *cobra.Command, args []string) error {
requiredFlags := []string{"gpu", "instance-type", "backend"}
for _, flag := range requiredFlags {
if err := cmd.MarkFlagRequired(flag); err != nil {
return err
}
}
return nil
},
RunE: runFunctionDeploy,
}

cmd.Flags().String("version-id", "", "The ID of the version to deploy")
Expand All @@ -32,10 +41,6 @@ func functionDeployCmd() *cobra.Command {
cmd.Flags().Int64("max-request-concurrency", 1, "Maximum number of concurrent requests")
cmd.Flags().BoolP("detached", "d", false, "Detach from the deployment and return to the prompt")

cmd.MarkFlagRequired("gpu")
cmd.MarkFlagRequired("instance-type")
cmd.MarkFlagRequired("backend")

return cmd
}

Expand Down Expand Up @@ -64,9 +69,18 @@ func runFunctionDeploy(cmd *cobra.Command, args []string) error {
}
}

gpu, _ := cmd.Flags().GetString("gpu")
instanceType, _ := cmd.Flags().GetString("instance-type")
backend, _ := cmd.Flags().GetString("backend")
gpu, err := cmd.Flags().GetString("gpu")
if err != nil {
return output.Error(cmd, "Error getting gpu", err)
}
instanceType, err := cmd.Flags().GetString("instance-type")
if err != nil {
return output.Error(cmd, "Error getting instance-type", err)
}
backend, err := cmd.Flags().GetString("backend")
if err != nil {
return output.Error(cmd, "Error getting backend", err)
}
minInstances, _ := cmd.Flags().GetInt64("min-instances")
maxInstances, _ := cmd.Flags().GetInt64("max-instances")
maxRequestConcurrency, _ := cmd.Flags().GetInt64("max-request-concurrency")
Expand All @@ -83,7 +97,7 @@ func runFunctionDeploy(cmd *cobra.Command, args []string) error {
}}),
}

_, err := client.FunctionDeployment.Functions.Versions.InitiateDeployment(
_, err = client.FunctionDeployment.Functions.Versions.InitiateDeployment(
cmd.Context(),
functionId,
versionId,
Expand Down
5 changes: 4 additions & 1 deletion cmd/function/function_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ func runFunctionStop(cmd *cobra.Command, args []string) error {
}
if all {
for _, version := range deployedVersionsToStop {
client.FunctionDeployment.Functions.Versions.DeleteDeployment(cmd.Context(), functionId, version, nvcf.FunctionDeploymentFunctionVersionDeleteDeploymentParams{
_, err = client.FunctionDeployment.Functions.Versions.DeleteDeployment(cmd.Context(), functionId, version, nvcf.FunctionDeploymentFunctionVersionDeleteDeploymentParams{
Graceful: nvcf.Bool(force),
})
if err != nil {
return output.Error(cmd, fmt.Sprintf("Error stopping function %s version %s", functionId, version), err)
}
output.Success(cmd, fmt.Sprintf("Function %s version %s stopped successfully", functionId, version))
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions cmd/function/function_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runFunctionWatch(cmd *cobra.Command, args []string) error {
functionID := args[0]
versions, err := client.Functions.Versions.List(cmd.Context(), functionID)
if err != nil {
output.Error(cmd, "Error getting function", err)
_ = output.Error(cmd, "Error getting function", err)
return
}
functions = &nvcf.ListFunctionsResponse{
Expand All @@ -74,7 +74,7 @@ func runFunctionWatch(cmd *cobra.Command, args []string) error {
Visibility: nvcf.F(visibilityParams),
})
if err != nil {
output.Error(cmd, "Error listing functions", err)
_ = output.Error(cmd, "Error listing functions", err)
return
}
}
Expand Down
20 changes: 16 additions & 4 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func StopSpinner(s *spinner.Spinner) {

func Functions(cmd *cobra.Command, functions []nvcf.ListFunctionsResponseFunction) {
if isJSON(cmd) {
printJSON(cmd, functions)
err := printJSON(cmd, functions)
if err != nil {
return
}
} else {
printFunctionsTable(cmd, functions)
}
Expand All @@ -90,7 +93,10 @@ func printFunctionsTable(cmd *cobra.Command, functions []nvcf.ListFunctionsRespo

func SingleFunction(cmd *cobra.Command, fn nvcf.FunctionResponseFunction) {
if isJSON(cmd) {
printJSON(cmd, fn)
err := printJSON(cmd, fn)
if err != nil {
return
}
} else {
printSingleFunctionTable(cmd, fn)
}
Expand Down Expand Up @@ -127,7 +133,10 @@ func printDeploymentsTable(cmd *cobra.Command, deployments []nvcf.DeploymentResp

func SingleDeployment(cmd *cobra.Command, deployment nvcf.DeploymentResponse) {
if isJSON(cmd) {
printJSON(cmd, deployment)
err := printJSON(cmd, deployment)
if err != nil {
return
}
} else {
printSingleDeploymentTable(cmd, deployment)
}
Expand All @@ -143,7 +152,10 @@ func printSingleDeploymentTable(cmd *cobra.Command, deployment nvcf.DeploymentRe

func GPUs(cmd *cobra.Command, clusterGroups []nvcf.ClusterGroupsResponseClusterGroup) {
if isJSON(cmd) {
printJSON(cmd, clusterGroups)
err := printJSON(cmd, clusterGroups)
if err != nil {
return
}
} else {
printGPUsTable(cmd, clusterGroups)
}
Expand Down

0 comments on commit 8402ff5

Please sign in to comment.