|
| 1 | +// Package profile provides profile-based configuration management for the Gram |
| 2 | +// CLI. |
| 3 | +package profile |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | +) |
| 12 | + |
| 13 | +// Config represents the profile configuration file structure. |
| 14 | +type Config struct { |
| 15 | + Current string `json:"current"` |
| 16 | + Profiles map[string]*Profile `json:"profiles"` |
| 17 | +} |
| 18 | + |
| 19 | +// Profile represents a single profile with authentication and project settings. |
| 20 | +type Profile struct { |
| 21 | + Secret string `json:"secret"` |
| 22 | + DefaultProjectSlug string `json:"defaultProjectSlug"` |
| 23 | + APIUrl string `json:"apiUrl"` |
| 24 | + Org any `json:"org"` |
| 25 | + Projects []any `json:"projects"` |
| 26 | +} |
| 27 | + |
| 28 | +// DefaultProfilePath returns the default path to the profile configuration file. |
| 29 | +func DefaultProfilePath() (string, error) { |
| 30 | + homeDir, err := os.UserHomeDir() |
| 31 | + if err != nil { |
| 32 | + return "", fmt.Errorf("failed to get user home directory: %w", err) |
| 33 | + } |
| 34 | + return filepath.Join(homeDir, ".gram", "profile.json"), nil |
| 35 | +} |
| 36 | + |
| 37 | +// Load reads the profile configuration from the specified path, or from |
| 38 | +// DefaultProfilePath() if path is empty, and returns the currently active |
| 39 | +// profile based on the "current" field. |
| 40 | +// |
| 41 | +// Returns (nil, nil) if the profile file doesn't exist. |
| 42 | +// Returns an error if the file is malformed or the current profile is invalid. |
| 43 | +func Load(path string) (*Profile, error) { |
| 44 | + var profilePath string |
| 45 | + if path != "" { |
| 46 | + profilePath = path |
| 47 | + } else { |
| 48 | + defaultPath, err := DefaultProfilePath() |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + profilePath = defaultPath |
| 53 | + } |
| 54 | + |
| 55 | + data, err := os.ReadFile(filepath.Clean(profilePath)) |
| 56 | + if err != nil { |
| 57 | + if os.IsNotExist(err) { |
| 58 | + return nil, nil |
| 59 | + } |
| 60 | + return nil, fmt.Errorf("failed to read profile file: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + var config Config |
| 64 | + if err := json.Unmarshal(data, &config); err != nil { |
| 65 | + return nil, fmt.Errorf("failed to parse profile file: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + if config.Current == "" { |
| 69 | + return nil, fmt.Errorf("profile file missing 'current' field") |
| 70 | + } |
| 71 | + |
| 72 | + profile, ok := config.Profiles[config.Current] |
| 73 | + if !ok { |
| 74 | + return nil, fmt.Errorf( |
| 75 | + "current profile '%s' not found in profiles", |
| 76 | + config.Current, |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + return profile, nil |
| 81 | +} |
| 82 | + |
| 83 | +type contextKey string |
| 84 | + |
| 85 | +const profileContextKey contextKey = "profile" |
| 86 | + |
| 87 | +// FromContext retrieves the loaded profile from the context. Returns nil if no |
| 88 | +// profile was loaded. |
| 89 | +func FromContext(ctx context.Context) *Profile { |
| 90 | + if prof, ok := ctx.Value(profileContextKey).(*Profile); ok { |
| 91 | + return prof |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// WithProfile adds the incoming profile to ctx. No-ops if prof is nil. |
| 97 | +func WithProfile(ctx context.Context, prof *Profile) context.Context { |
| 98 | + if prof != nil { |
| 99 | + return context.WithValue(ctx, profileContextKey, prof) |
| 100 | + } |
| 101 | + return ctx |
| 102 | +} |
0 commit comments