github.com/greenpau/go-authcrunch@v1.0.50/cmd/authdbctl/realm.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"github.com/urfave/cli/v2"
    21  	"net/http"
    22  	"os"
    23  )
    24  
    25  func listRealms(c *cli.Context) error {
    26  	wr := new(wrapper)
    27  	if err := wr.configure(c); err != nil {
    28  		return err
    29  	}
    30  	wr.logger.Debug("listing realms")
    31  
    32  	var reqData = []byte(`{
    33  		"name": "",
    34  		"job": "leader"
    35  	}`)
    36  
    37  	req, _ := http.NewRequest(http.MethodPost, wr.config.BaseURL+"/api/realms", bytes.NewBuffer(reqData))
    38  	req.Header.Set("Content-Type", "application/json; charset=UTF-8")
    39  	req.Header.Set("Authorization", "access_token="+wr.config.token)
    40  	respBody, _, err := wr.browser.Do(req)
    41  	if err != nil {
    42  		return fmt.Errorf("failed listing realms: %v", err)
    43  	}
    44  	fmt.Fprintf(os.Stdout, "%s\n", respBody)
    45  
    46  	return nil
    47  }