package main import ( "context" "encoding/json" "flag" "fmt" "io" "log" "net/http" "os" "time" "github.com/google/subcommands" ) type whoamiCmd struct { json bool } func (whoamiCmd) Name() string { return "whoami" } func (whoamiCmd) Synopsis() string { return "show information about currently logged in user" } func (whoamiCmd) Usage() string { return `wasmc whoami [options] $ wasmc whoami $ wasmc whoami -json Returns information about the currently logged in user. Flags: ` } func (w *whoamiCmd) SetFlags(fs *flag.FlagSet) { fs.BoolVar(&w.json, "json", false, "if set, dump information in json") } func (w *whoamiCmd) Execute(ctx context.Context, fs *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { req, err := http.NewRequest(http.MethodGet, *apiServer+"/api/whoami", nil) if err != nil { panic(err) } withAPI(req) resp, err := http.DefaultClient.Do(req) if err != nil { log.Printf("error fetching data: %v", err) return subcommands.ExitFailure } defer resp.Body.Close() if w.json { io.Copy(os.Stdout, resp.Body) return subcommands.ExitSuccess } type apiResp struct { ID int `json:"ID"` CreatedAt time.Time `json:"CreatedAt"` UpdatedAt time.Time `json:"UpdatedAt"` Username string `json:"Username"` Email string `json:"Email"` IsAdmin bool `json:"IsAdmin"` CanCreateHandlers bool `json:"CanCreateHandlers"` } var result apiResp err = json.NewDecoder(resp.Body).Decode(&result) if err != nil { panic(err) } fmt.Printf("Username: %s\nEmail: %s\nIs Admin: %v\nCan Create Handlers: %v\n", result.Username, result.Email, result.IsAdmin, result.CanCreateHandlers) return subcommands.ExitSuccess }