|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "regexp" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/major-technology/cli/clients/api" |
| 11 | + "github.com/major-technology/cli/singletons" |
| 12 | + "github.com/major-technology/cli/ui" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +// MiddlewareError is a custom error type that includes a title and suggestion |
| 17 | +type MiddlewareError struct { |
| 18 | + Title string |
| 19 | + Suggestion string |
| 20 | + Err error |
| 21 | +} |
| 22 | + |
| 23 | +func (e *MiddlewareError) Error() string { |
| 24 | + return e.Title |
| 25 | +} |
| 26 | + |
| 27 | +func (e *MiddlewareError) Unwrap() error { |
| 28 | + return e.Err |
| 29 | +} |
| 30 | + |
| 31 | +// CommandCheck is a function that performs a check before a command is run |
| 32 | +type CommandCheck func(cmd *cobra.Command, args []string) error |
| 33 | + |
| 34 | +// Compose combines multiple checks into a single function compatible with Cobra's PreRunE |
| 35 | +func Compose(checks ...CommandCheck) func(cmd *cobra.Command, args []string) error { |
| 36 | + return func(cmd *cobra.Command, args []string) error { |
| 37 | + for _, check := range checks { |
| 38 | + if err := check(cmd, args); err != nil { |
| 39 | + // If it's our custom MiddlewareError, print nicely |
| 40 | + if mwErr, ok := err.(*MiddlewareError); ok { |
| 41 | + ui.PrintError(cmd, mwErr.Title, mwErr.Suggestion) |
| 42 | + cmd.SilenceErrors = true |
| 43 | + cmd.SilenceUsage = true |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + // Fallback: print using ui.PrintError for consistency or rely on CheckErr if relevant |
| 48 | + // For checks that didn't return MiddlewareError but are simple errors: |
| 49 | + ui.PrintError(cmd, err.Error(), "") |
| 50 | + cmd.SilenceErrors = true |
| 51 | + cmd.SilenceUsage = true |
| 52 | + return err |
| 53 | + } |
| 54 | + } |
| 55 | + return nil |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// CheckLogin checks if the user is logged in and the session is valid |
| 60 | +func CheckLogin(cmd *cobra.Command, args []string) error { |
| 61 | + client := singletons.GetAPIClient() |
| 62 | + |
| 63 | + // VerifyToken checks if the token exists and is valid by calling the API |
| 64 | + _, err := client.VerifyToken() |
| 65 | + if err != nil { |
| 66 | + // Try to determine the specific error |
| 67 | + title := "Authentication failed" |
| 68 | + suggestion := "Run major user login to authenticate" |
| 69 | + |
| 70 | + if api.IsTokenExpired(err) || strings.Contains(err.Error(), "expired") { |
| 71 | + title = "Your session has expired!" |
| 72 | + suggestion = "Run major user login to login again." |
| 73 | + } else if api.IsNoToken(err) || strings.Contains(err.Error(), "not logged in") || strings.Contains(err.Error(), "invalid") { |
| 74 | + title = "Not logged in!" |
| 75 | + suggestion = "Run major user login to get started." |
| 76 | + } |
| 77 | + |
| 78 | + return &MiddlewareError{ |
| 79 | + Title: title, |
| 80 | + Suggestion: suggestion, |
| 81 | + Err: err, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// CheckPnpmInstalled checks if pnpm is installed in the system path |
| 89 | +func CheckPnpmInstalled(cmd *cobra.Command, args []string) error { |
| 90 | + _, err := exec.LookPath("pnpm") |
| 91 | + if err != nil { |
| 92 | + return &MiddlewareError{ |
| 93 | + Title: "pnpm not found", |
| 94 | + Suggestion: "pnpm is required. Please install it: npm install -g pnpm", |
| 95 | + Err: err, |
| 96 | + } |
| 97 | + } |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// CheckNodeVersion checks if node is installed and meets the minimum version requirement |
| 102 | +func CheckNodeVersion(minVersion string) CommandCheck { |
| 103 | + return func(cmd *cobra.Command, args []string) error { |
| 104 | + path, err := exec.LookPath("node") |
| 105 | + if err != nil { |
| 106 | + return &MiddlewareError{ |
| 107 | + Title: "Node.js not found", |
| 108 | + Suggestion: "Node.js is required. Please install it.", |
| 109 | + Err: err, |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + cmdOut := exec.Command(path, "--version") |
| 114 | + output, err := cmdOut.Output() |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to check node version: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + versionStr := strings.TrimSpace(string(output)) |
| 120 | + // Remove 'v' prefix if present |
| 121 | + versionStr = strings.TrimPrefix(versionStr, "v") |
| 122 | + |
| 123 | + if !isVersionGTE(versionStr, minVersion) { |
| 124 | + return &MiddlewareError{ |
| 125 | + Title: fmt.Sprintf("Node.js version %s required", minVersion), |
| 126 | + Suggestion: fmt.Sprintf("You are running version %s. Please upgrade Node.js.", versionStr), |
| 127 | + Err: fmt.Errorf("node version %s is required, but found %s", minVersion, versionStr), |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// isVersionGTE returns true if v1 >= v2 |
| 136 | +// This is a simple implementation assuming semver format x.y.z |
| 137 | +func isVersionGTE(v1, v2 string) bool { |
| 138 | + parts1 := parseVersion(v1) |
| 139 | + parts2 := parseVersion(v2) |
| 140 | + |
| 141 | + for i := 0; i < 3; i++ { |
| 142 | + if parts1[i] > parts2[i] { |
| 143 | + return true |
| 144 | + } |
| 145 | + if parts1[i] < parts2[i] { |
| 146 | + return false |
| 147 | + } |
| 148 | + } |
| 149 | + return true |
| 150 | +} |
| 151 | + |
| 152 | +func parseVersion(v string) [3]int { |
| 153 | + var parts [3]int |
| 154 | + |
| 155 | + // Use regex to extract numbers |
| 156 | + re := regexp.MustCompile(`(\d+)\.(\d+)\.(\d+)`) |
| 157 | + matches := re.FindStringSubmatch(v) |
| 158 | + |
| 159 | + if len(matches) == 4 { |
| 160 | + for i := 1; i <= 3; i++ { |
| 161 | + val, _ := strconv.Atoi(matches[i]) |
| 162 | + parts[i-1] = val |
| 163 | + } |
| 164 | + } else { |
| 165 | + // Fallback for simpler versions like "18" or "18.1" |
| 166 | + split := strings.Split(v, ".") |
| 167 | + for i := 0; i < len(split) && i < 3; i++ { |
| 168 | + val, _ := strconv.Atoi(split[i]) |
| 169 | + parts[i] = val |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return parts |
| 174 | +} |
0 commit comments