Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,8 @@ func LoginCmd() *cobra.Command {

return nil
},
Run: func(cmd *cobra.Command, args []string) {
if err := loginMain(cmd, &opts); err != nil {
os.Exit(1)
}
RunE: func(cmd *cobra.Command, args []string) error {
return loginMain(cmd, &opts)
},
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/auth/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logout

import (
"errors"
"os"
"time"

"github.com/charmbracelet/huh"
Expand All @@ -18,10 +17,8 @@ func LogoutCmd() *cobra.Command {
Use: "logout",
Short: "Logout from Pangolin",
Long: "Logout and clear your session",
Run: func(cmd *cobra.Command, args []string) {
if err := logoutMain(cmd); err != nil {
os.Exit(1)
}
RunE: func(cmd *cobra.Command, args []string) error {
return logoutMain(cmd)
},
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/auth/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package status

import (
"fmt"
"os"
"strings"

"github.com/fosrl/cli/internal/api"
Expand All @@ -17,10 +16,8 @@ func StatusCmd() *cobra.Command {
Use: "status",
Short: "Check authentication status",
Long: "Check if you are logged in and view your account information",
Run: func(cmd *cobra.Command, args []string) {
if err := statusMain(cmd); err != nil {
os.Exit(1)
}
RunE: func(cmd *cobra.Command, args []string) error {
return statusMain(cmd)
},
}

Expand Down
27 changes: 11 additions & 16 deletions cmd/authdaemon/authdaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os/signal"
"syscall"

"github.com/fosrl/cli/internal/logger"
authdaemonpkg "github.com/fosrl/newt/authdaemon"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -51,8 +50,8 @@ func AuthDaemonCmd() *cobra.Command {
}
return nil
},
Run: func(c *cobra.Command, args []string) {
runAuthDaemon(opts)
RunE: func(c *cobra.Command, args []string) error {
return runAuthDaemon(opts)
},
}

Expand Down Expand Up @@ -84,12 +83,12 @@ func PrincipalsCmd() *cobra.Command {
}
return nil
},
Run: func(c *cobra.Command, args []string) {
RunE: func(c *cobra.Command, args []string) error {
path := opts.PrincipalsFile
if path == "" {
path = defaultPrincipalsPath
}
runPrincipals(path, opts.Username)
return runPrincipals(path, opts.Username)
},
}

Expand All @@ -100,19 +99,19 @@ func PrincipalsCmd() *cobra.Command {
return cmd
}

func runPrincipals(principalsPath, username string) {
func runPrincipals(principalsPath, username string) error {
list, err := authdaemonpkg.GetPrincipals(principalsPath, username)
if err != nil {
logger.Error("%v", err)
os.Exit(1)
return err
}
if len(list) == 0 {
fmt.Println("")
return
return nil
}
for _, principal := range list {
fmt.Println(principal)
}
return nil
}

func runAuthDaemon(opts struct {
Expand All @@ -121,7 +120,7 @@ func runAuthDaemon(opts struct {
PrincipalsFile string
CACertPath string
GenerateRandomPassword bool
}) {
}) error {
cfg := authdaemonpkg.Config{
Port: opts.Port,
PresharedKey: opts.PreSharedKey,
Expand All @@ -133,15 +132,11 @@ func runAuthDaemon(opts struct {

srv, err := authdaemonpkg.NewServer(cfg)
if err != nil {
logger.Error("%v", err)
os.Exit(1)
return err
}

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

if err := srv.Run(ctx); err != nil {
logger.Error("%v", err)
os.Exit(1)
}
return srv.Run(ctx)
}
7 changes: 2 additions & 5 deletions cmd/down/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
"errors"
"os"

"github.com/fosrl/cli/internal/config"
"github.com/fosrl/cli/internal/logger"
Expand All @@ -16,10 +15,8 @@ func ClientDownCmd() *cobra.Command {
Use: "client",
Short: "Stop the client connection",
Long: "Stop the currently running client connection",
Run: func(cmd *cobra.Command, args []string) {
if err := clientDownMain(cmd); err != nil {
os.Exit(1)
}
RunE: func(cmd *cobra.Command, args []string) error {
return clientDownMain(cmd)
},
}

Expand Down
6 changes: 2 additions & 4 deletions cmd/logs/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ func ClientLogsCmd() *cobra.Command {
Use: "client",
Short: "View client logs",
Long: "View client logs. Use -f to follow log output.",
Run: func(cmd *cobra.Command, args []string) {
if err := clientLogsMain(cmd, &opts); err != nil {
os.Exit(1)
}
RunE: func(cmd *cobra.Command, args []string) error {
return clientLogsMain(cmd, &opts)
},
}

Expand Down
53 changes: 30 additions & 23 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/fosrl/cli/cmd/auth/logout"
"github.com/fosrl/cli/cmd/authdaemon"
"github.com/fosrl/cli/cmd/down"
"github.com/fosrl/cli/cmd/list"
"github.com/fosrl/cli/cmd/logs"
selectcmd "github.com/fosrl/cli/cmd/select"
"github.com/fosrl/cli/cmd/ssh"
Expand Down Expand Up @@ -50,7 +49,6 @@ func RootCommand(initResources bool) (*cobra.Command, error) {
}
cmd.AddCommand(apply.ApplyCommand())
cmd.AddCommand(selectcmd.SelectCmd())
cmd.AddCommand(list.ListCmd())

// Platform-specific commands - nil on unsupported platforms
if upCmd := up.UpCmd(); upCmd != nil {
Expand Down Expand Up @@ -117,16 +115,15 @@ func RootCommand(initResources bool) (*cobra.Command, error) {
}

func mainCommandPreRun(cmd *cobra.Command, args []string) error {
cfg := config.ConfigFromContext(cmd.Context())

// Skip init/update check for version and update commands
// Check both the command name and if it's one of these specific commands
cmdName := cmd.Name()
if cmdName == "version" || cmdName == "update" {
if shouldSkipRuntimeInit(cmd) {
return nil
}

ensureRuntimeDirs(cfg)
cfg := config.ConfigFromContext(cmd.Context())

if err := ensureRuntimeDirs(cfg); err != nil {
return err
}

// Check for updates asynchronously
if !cfg.DisableUpdateCheck {
Expand All @@ -140,34 +137,44 @@ func mainCommandPreRun(cmd *cobra.Command, args []string) error {
return nil
}

// Make sure all required directories exist once
// before executing any subcommands.
func ensureRuntimeDirs(cfg *config.Config) {
// shouldSkipRuntimeInit returns true for commands that must not touch runtime
// directories or emit diagnostics to stdout (for example shell completion).
func shouldSkipRuntimeInit(cmd *cobra.Command) bool {
for c := cmd; c != nil; c = c.Parent() {
switch c.Name() {
case "completion", "version", "update":
return true
}
}
return false
}

// Make sure all required directories exist once before executing subcommands.
func ensureRuntimeDirs(cfg *config.Config) error {
configDir, err := config.GetPangolinConfigDir()
if err != nil {
logger.Warning("failed to create pangolin configuration directory: %v", err)
} else {
err = os.MkdirAll(configDir, 0o755)
if err != nil {
logger.Warning("failed to create %s: %v", configDir, err)
}
return fmt.Errorf("failed to create pangolin configuration directory: %w", err)
}

if err := os.MkdirAll(configDir, 0o755); err != nil {
return fmt.Errorf("failed to create %s: %w", configDir, err)
}

if cfg.LogFile != "" {
logPathDirname := filepath.Dir(cfg.LogFile)

err = os.MkdirAll(logPathDirname, 0o755)
if err != nil {
logger.Warning("failed to create %s: %v", logPathDirname, err)
if err := os.MkdirAll(logPathDirname, 0o755); err != nil {
return fmt.Errorf("failed to create %s: %w", logPathDirname, err)
}
}

return nil
}

// Execute is called by main.go
func Execute() {
cmd, err := RootCommand(true)
if err != nil {
logger.Error("%v", err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/select/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package account
import (
"errors"
"fmt"
"os"
"strings"

"github.com/charmbracelet/huh"
Expand All @@ -27,10 +26,8 @@ func AccountCmd() *cobra.Command {
Use: "account",
Short: "Select an account",
Long: "List your logged-in accounts and select active one",
Run: func(cmd *cobra.Command, args []string) {
if err := accountMain(cmd, &opts); err != nil {
os.Exit(1)
}
RunE: func(cmd *cobra.Command, args []string) error {
return accountMain(cmd, &opts)
},
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/select/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org

import (
"fmt"
"os"

"github.com/fosrl/cli/internal/api"
"github.com/fosrl/cli/internal/config"
Expand All @@ -24,10 +23,8 @@ func OrgCmd() *cobra.Command {
Use: "org",
Short: "Select an organization",
Long: "List your organizations and select one to use",
Run: func(cmd *cobra.Command, args []string) {
if err := orgMain(cmd, &opts); err != nil {
os.Exit(1)
}
RunE: func(cmd *cobra.Command, args []string) error {
return orgMain(cmd, &opts)
},
}

Expand Down
Loading
Loading