Skip to content

Commit 22c3216

Browse files
authored
Merge pull request #1 from brevdev/implement-auth-cmd
auth: add auth commands
2 parents 5ec1e37 + 7a97a0b commit 22c3216

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nvcf

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ GOFILES=$(shell find . -name '*.go' -not -path "./vendor/*")
1212
# Linting
1313
GOLINT=golangci-lint
1414

15-
.PHONY: all build clean test vet lint fmt quickbuild
15+
.PHONY: all build clean test vet lint fmt q
1616

1717
all: build
1818

@@ -48,6 +48,6 @@ build-all:
4848
GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME)-darwin-amd64
4949
GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME)-windows-amd64.exe
5050

51-
quickbuild:
51+
q:
5252
make clean
5353
make build

cmd/auth.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cmd
22

33
import (
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+
}

config/config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ type Config struct {
1313
var cfg Config
1414

1515
func Init() {
16-
configDir, err := os.UserConfigDir()
16+
homeDir, err := os.UserHomeDir()
1717
if err != nil {
1818
panic(err)
1919
}
2020

2121
// TODO: consider more robust config loading here
22-
configPath := filepath.Join(configDir, "nvcf", "config.json")
22+
configPath := filepath.Join(homeDir, ".nvcf", "config.json")
2323
data, err := os.ReadFile(configPath)
2424
if err == nil {
2525
json.Unmarshal(data, &cfg)
@@ -51,23 +51,23 @@ func IsAuthenticated() bool {
5151
return cfg.APIKey != ""
5252
}
5353

54-
// todo: consider reaching for more secret-specific storage.
54+
// save to ~/.nvcf/config.json
5555
func saveConfig() error {
56-
configDir, err := os.UserConfigDir()
56+
homeDir, err := os.UserHomeDir()
5757
if err != nil {
5858
return err
5959
}
6060

61-
configPath := filepath.Join(configDir, "nvcf", "config.json")
61+
configPath := filepath.Join(homeDir, ".nvcf", "config.json")
6262
data, err := json.MarshalIndent(cfg, "", " ")
6363
if err != nil {
6464
return err
6565
}
6666

67-
err = os.MkdirAll(filepath.Dir(configPath), 0755)
67+
err = os.MkdirAll(filepath.Dir(configPath), 0700)
6868
if err != nil {
6969
return err
7070
}
7171

72-
return os.WriteFile(configPath, data, 0644)
72+
return os.WriteFile(configPath, data, 0600)
7373
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626
// rootCmd.AddCommand(cmd.DeploymentCmd())
2727
// rootCmd.AddCommand(cmd.InvokeCmd())
2828
// rootCmd.AddCommand(cmd.AssetCmd())
29-
// rootCmd.AddCommand(cmd.AuthCmd())
29+
rootCmd.AddCommand(cmd.AuthCmd())
3030
// rootCmd.AddCommand(cmd.QueueCmd())
3131
// rootCmd.AddCommand(cmd.ClusterGroupCmd())
3232
// rootCmd.AddCommand(cmd.ConfigCmd())

0 commit comments

Comments
 (0)