github.com/greenpau/go-authcrunch@v1.1.4/cmd/authdbctl/user.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  	fileutil "github.com/greenpau/go-authcrunch/pkg/util/file"
    21  	"github.com/urfave/cli/v2"
    22  	"go.uber.org/zap"
    23  	"net/http"
    24  	"os"
    25  )
    26  
    27  // User represents input user identity.
    28  type User struct {
    29  	Username string   `json:"username,omitempty" xml:"username,omitempty" yaml:"username,omitempty"`
    30  	Password string   `json:"password,omitempty" xml:"password,omitempty" yaml:"password,omitempty"`
    31  	Name     string   `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"`
    32  	Email    string   `json:"email,omitempty" xml:"email,omitempty" yaml:"email,omitempty"`
    33  	Roles    []string `json:"roles,omitempty" xml:"roles,omitempty" yaml:"roles,omitempty"`
    34  }
    35  
    36  func addUser(c *cli.Context) error {
    37  	wr := new(wrapper)
    38  	if err := wr.configure(c); err != nil {
    39  		return err
    40  	}
    41  	wr.logger.Debug("adding user")
    42  
    43  	b, err := fileutil.ReadFileBytes(c.String("batch"))
    44  	if err != nil {
    45  		return err
    46  	}
    47  	for _, entry := range bytes.Split(b, []byte("\n")) {
    48  		if !bytes.HasPrefix(entry, []byte("{")) {
    49  			// fmt.Fprintf(os.Stdout, string(b)+"\n")
    50  		}
    51  		wr.logger.Debug("user entry", zap.String("entry", string(entry)))
    52  	}
    53  	return nil
    54  }
    55  
    56  func listUsers(c *cli.Context) error {
    57  	wr := new(wrapper)
    58  	if err := wr.configure(c); err != nil {
    59  		return err
    60  	}
    61  	wr.logger.Debug("listing users")
    62  
    63  	var reqData = []byte(`{
    64  		"name": "",
    65  		"job": "leader"
    66  	}`)
    67  
    68  	req, _ := http.NewRequest(http.MethodPost, wr.config.BaseURL+"/api/users", bytes.NewBuffer(reqData))
    69  	req.Header.Set("Content-Type", "application/json; charset=UTF-8")
    70  	req.Header.Set("Authorization", "access_token="+wr.config.token)
    71  	respBody, _, err := wr.browser.Do(req)
    72  	if err != nil {
    73  		return fmt.Errorf("failed listing users: %v", err)
    74  	}
    75  	fmt.Fprintf(os.Stdout, "%s\n", respBody)
    76  
    77  	return nil
    78  }