From 7e7d7e906eb04c8c935c5218741d200ff15e712c Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Fri, 25 Oct 2024 16:03:45 +0200 Subject: [PATCH] Implement list-groups command. --- galenectl/galenectl.go | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/galenectl/galenectl.go b/galenectl/galenectl.go index aee2de0..95e2c1e 100644 --- a/galenectl/galenectl.go +++ b/galenectl/galenectl.go @@ -75,6 +75,10 @@ var commands = map[string]command{ command: updateUserCmd, description: "change a user's permissions", }, + "list-groups": { + command: listGroupsCmd, + description: "list groups", + }, } func main() { @@ -296,6 +300,26 @@ func setAuthorization(req *http.Request) { } } +func getJSON(url string, value any) error { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return err + } + setAuthorization(req) + + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode >= 300 { + return fmt.Errorf("%v %v", resp.StatusCode, resp.Status) + } + + decoder := json.NewDecoder(resp.Body) + return decoder.Decode(value) +} + func putJSON(url string, value any, overwrite bool) error { j, err := json.Marshal(value) if err != nil { @@ -774,3 +798,26 @@ func deleteUserCmd(cmdname string, args []string) { log.Fatalf("Delete user: %v", err) } } + +func listGroupsCmd(cmdname string, args []string) { + cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) + setUsage(cmd, cmdname, + "%v [option...] %v\n", + os.Args[0], cmdname, + ) + cmd.Parse(args) + + u, err := url.JoinPath(serverURL, "/galene-api/v0/.groups/") + if err != nil { + log.Fatalf("Build URL: %v", err) + } + + var groups []string + err = getJSON(u, &groups) + if err != nil { + log.Fatalf("Get groups: %v", err) + } + for _, g := range groups { + fmt.Println(g) + } +}