1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-13 20:25:57 +01:00

Implement list-groups command.

This commit is contained in:
Juliusz Chroboczek 2024-10-25 16:03:45 +02:00
parent fdac5f5bde
commit 7e7d7e906e

View file

@ -75,6 +75,10 @@ var commands = map[string]command{
command: updateUserCmd, command: updateUserCmd,
description: "change a user's permissions", description: "change a user's permissions",
}, },
"list-groups": {
command: listGroupsCmd,
description: "list groups",
},
} }
func main() { 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 { func putJSON(url string, value any, overwrite bool) error {
j, err := json.Marshal(value) j, err := json.Marshal(value)
if err != nil { if err != nil {
@ -774,3 +798,26 @@ func deleteUserCmd(cmdname string, args []string) {
log.Fatalf("Delete user: %v", err) 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)
}
}