fix(session): real getMyProfiles shape {myprofiles:[...]} + active-profile detection [#767]
https://projects.knownelement.com/issues/767#note-4191
This commit is contained in:
@@ -3,6 +3,7 @@ package cli
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -19,6 +20,10 @@ func marshalIndent(v any) ([]byte, error) { return json.MarshalIndent(v, "", "
|
||||
|
||||
// cmdWhoami lists the session's profiles. It NEVER switches the active
|
||||
// profile (agent mode's MGLPI_PROFILE_ID is deliberately not applied).
|
||||
// Active-profile detection comes from GetActiveProfile; when that
|
||||
// endpoint is missing or permission-blocked (404/403), whoami degrades
|
||||
// gracefully to all-inactive rather than erroring — other failures
|
||||
// (5xx, unreachable) still surface as API errors.
|
||||
func cmdWhoami(args []string, stdout, stderr io.Writer) int {
|
||||
fs, cfgPath, out := newFlags("whoami", stderr)
|
||||
pos, err := parseArgs(fs, args)
|
||||
@@ -33,6 +38,16 @@ func cmdWhoami(args []string, stdout, stderr io.Writer) int {
|
||||
if err != nil {
|
||||
return apiErr(stderr, err)
|
||||
}
|
||||
activeID, aerr := c.GetActiveProfile(context.Background())
|
||||
degraded := aerr != nil && (errors.Is(aerr, glpi.ErrNotFound) || errors.Is(aerr, glpi.ErrAuth))
|
||||
if aerr != nil && !degraded {
|
||||
return apiErr(stderr, aerr)
|
||||
}
|
||||
if !degraded {
|
||||
for i := range profs {
|
||||
profs[i].IsActive = profs[i].ID == activeID
|
||||
}
|
||||
}
|
||||
if *out == "json" {
|
||||
emitJSON(stdout, map[string]any{"profiles": profs})
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user