github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/report/diff.go (about)

     1  package report
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/containers/libpod/pkg/domain/entities"
     8  	"github.com/containers/storage/pkg/archive"
     9  	jsoniter "github.com/json-iterator/go"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type ChangesReportJSON struct {
    14  	Changed []string `json:"changed,omitempty"`
    15  	Added   []string `json:"added,omitempty"`
    16  	Deleted []string `json:"deleted,omitempty"`
    17  }
    18  
    19  func ChangesToJSON(diffs *entities.DiffReport) error {
    20  	body := ChangesReportJSON{}
    21  	for _, row := range diffs.Changes {
    22  		switch row.Kind {
    23  		case archive.ChangeAdd:
    24  			body.Added = append(body.Added, row.Path)
    25  		case archive.ChangeDelete:
    26  			body.Deleted = append(body.Deleted, row.Path)
    27  		case archive.ChangeModify:
    28  			body.Changed = append(body.Changed, row.Path)
    29  		default:
    30  			return errors.Errorf("output kind %q not recognized", row.Kind)
    31  		}
    32  	}
    33  
    34  	json := jsoniter.ConfigCompatibleWithStandardLibrary
    35  	enc := json.NewEncoder(os.Stdout)
    36  	return enc.Encode(body)
    37  }
    38  
    39  func ChangesToTable(diffs *entities.DiffReport) error {
    40  	for _, row := range diffs.Changes {
    41  		fmt.Fprintln(os.Stdout, row.String())
    42  	}
    43  	return nil
    44  }