11package cmd
22
33import (
4+ "errors"
5+ "fmt"
6+ "os"
47 "os/exec"
58 "strings"
69
@@ -13,11 +16,21 @@ func AuthCmd() *cobra.Command {
1316 cmd := & cobra.Command {
1417 Use : "auth" ,
1518 Short : "Manage authentication for the CLI" ,
19+ Long : `Authenticate with NVIDIA Cloud and configure the CLI to use your API key.` ,
20+ PersistentPreRun : func (cmd * cobra.Command , args []string ) {
21+ config .Init ()
22+ // Allow all 'auth' subcommands to run without authentication
23+ if cmd .Parent ().Name () != "auth" && ! config .IsAuthenticated () {
24+ fmt .Println ("You are not authenticated. Please run 'nvcf auth login' first." )
25+ os .Exit (1 )
26+ }
27+ },
1628 }
1729
1830 cmd .AddCommand (authLoginCmd ())
19- // cmd.AddCommand(authLogoutCmd())
20- // cmd.AddCommand(authStatusCmd())
31+ cmd .AddCommand (authLogoutCmd ())
32+ cmd .AddCommand (authStatusCmd ())
33+ cmd .AddCommand (authConfigureDockerCmd ())
2134
2235 return cmd
2336}
@@ -48,7 +61,12 @@ func authConfigureDockerCmd() *cobra.Command {
4861 output .Error (cmd , "NGC API key not found. Please run 'nvcf auth login' first." , nil )
4962 return
5063 }
51- // TODO: check for 'docker'
64+ // Check if Docker is installed
65+ _ , err := exec .LookPath ("docker" )
66+ if err != nil {
67+ output .Error (cmd , "Docker is not installed or not in the system PATH" , err )
68+ return
69+ }
5270 // TODO: check for existing nvcr.io config?
5371 dockerCmd := exec .Command ("docker" , "login" , "nvcr.io" , "-u" , "$oauthtoken" , "--password-stdin" )
5472 dockerCmd .Stdin = strings .NewReader (apiKey )
@@ -64,4 +82,31 @@ func authConfigureDockerCmd() *cobra.Command {
6482 }
6583}
6684
67- // Implement authLogoutCmd and authStatusCmd here
85+ func authStatusCmd () * cobra.Command {
86+ return & cobra.Command {
87+ Use : "status" ,
88+ Short : "Check the authentication status" ,
89+ Run : func (cmd * cobra.Command , args []string ) {
90+ if config .IsAuthenticated () {
91+ output .Success (cmd , "Authenticated" )
92+ } else {
93+ output .Error (cmd , "Not authenticated" , errors .New (":(" ))
94+ }
95+ },
96+ }
97+ }
98+
99+ func authLogoutCmd () * cobra.Command {
100+ return & cobra.Command {
101+ Use : "logout" ,
102+ Short : "Logout from NVIDIA Cloud" ,
103+ Run : func (cmd * cobra.Command , args []string ) {
104+ if ! config .IsAuthenticated () {
105+ output .Info (cmd , "You are currently not logged in" )
106+ return
107+ }
108+ config .ClearAPIKey ()
109+ output .Success (cmd , "Logged out successfully" )
110+ },
111+ }
112+ }
0 commit comments