github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/agent/mcorpc/replyfmt/replyfmt.go (about)

     1  // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  // Package replyfmt formats Replies for presentation to users
     6  package replyfmt
     7  
     8  import (
     9  	"fmt"
    10  	"io"
    11  
    12  	"github.com/choria-io/go-choria/providers/agent/mcorpc/client"
    13  )
    14  
    15  // Formatter formats and writes a reply into the bufio writer
    16  type Formatter interface {
    17  	FormatReply(w io.Writer, action ActionDDL, sender string, reply *client.RPCReply) error
    18  	FormatAggregates(w io.Writer, action ActionDDL) error
    19  
    20  	SetVerbose()
    21  	SetSilent()
    22  	SetDisplay(mode DisplayMode)
    23  }
    24  
    25  // DisplayMode overrides the DDL display hints
    26  type DisplayMode uint8
    27  
    28  const (
    29  	DisplayDDL = DisplayMode(iota)
    30  	DisplayOK
    31  	DisplayFailed
    32  	DisplayAll
    33  	DisplayNone
    34  )
    35  
    36  // OutputFormat is the format of reply desired
    37  type OutputFormat uint8
    38  
    39  const (
    40  	// UnknownFormat is an unknown format
    41  	UnknownFormat = OutputFormat(iota)
    42  
    43  	// ConsoleFormat is a format suitable for displaying on the console
    44  	ConsoleFormat
    45  )
    46  
    47  // Option configures a formatter
    48  type Option func(f Formatter) error
    49  
    50  // Verbose sets verbose output mode
    51  func Verbose() Option {
    52  	return func(f Formatter) error {
    53  		f.SetVerbose()
    54  
    55  		return nil
    56  	}
    57  }
    58  
    59  // Silent sets verbose output mode
    60  func Silent() Option {
    61  	return func(f Formatter) error {
    62  		f.SetSilent()
    63  
    64  		return nil
    65  	}
    66  }
    67  
    68  func Display(d DisplayMode) Option {
    69  	return func(f Formatter) error {
    70  		f.SetDisplay(d)
    71  
    72  		return nil
    73  	}
    74  }
    75  
    76  func formatter(f OutputFormat, opts ...Option) (Formatter, error) {
    77  	switch f {
    78  	case ConsoleFormat:
    79  		return NewConsoleFormatter(opts...), nil
    80  	default:
    81  		return nil, fmt.Errorf("unknown formatter specified")
    82  	}
    83  }
    84  
    85  func FormatAggregates(w io.Writer, f OutputFormat, action ActionDDL, opts ...Option) error {
    86  	rf, err := formatter(f, opts...)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	return rf.FormatAggregates(w, action)
    92  }
    93  
    94  func FormatReply(w io.Writer, f OutputFormat, action ActionDDL, sender string, reply *client.RPCReply, opts ...Option) error {
    95  	rf, err := formatter(f, opts...)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	return rf.FormatReply(w, action, sender, reply)
   101  }