github.com/TNTworks/flogo-cli@v0.9.1-0.20220522183836-60b8a963ae00/api/list.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/TNTworks/flogo-cli/common"
    11  	"github.com/TNTworks/flogo-cli/util"
    12  )
    13  
    14  type ListFilter int
    15  
    16  func ListContribs(project common.AppProject, jsonFormat bool, filter string) error {
    17  
    18  	ai, err := util.GetAppImports(filepath.Join(project.Dir(), fileFlogoJson), project.DepManager(), true)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	var specs []*ContribSpec
    24  
    25  	for _, details := range ai.GetAllImportDetails() {
    26  
    27  		if !includeContrib(details, filter) {
    28  			continue
    29  		}
    30  
    31  		specs = append(specs, getContribSpec(project, details))
    32  
    33  	}
    34  
    35  	for _, details := range ai.GetAllImportDetails() {
    36  
    37  		if details.ContribDesc == nil {
    38  			continue
    39  		}
    40  
    41  		if details.ContribDesc.Type == "flogo:function" {
    42  			specs = append(specs, getContribSpec(project, details))
    43  		}
    44  	}
    45  
    46  	if len(specs) == 0 {
    47  		return nil
    48  	}
    49  
    50  	if jsonFormat {
    51  		resp, err := json.MarshalIndent(specs, "", "  ")
    52  		if err != nil {
    53  			return err
    54  		}
    55  
    56  		fmt.Fprintf(os.Stdout, "%v \n", string(resp))
    57  	} else {
    58  		for _, spec := range specs {
    59  			fmt.Println("Contrib: " + spec.Name)
    60  			fmt.Println("  Type       : " + spec.Type)
    61  			if spec.IsLegacy != nil {
    62  				fmt.Println("  IsLegacy   : true")
    63  			}
    64  			fmt.Println("  Homepage   : " + spec.Homepage)
    65  			fmt.Println("  Ref        : " + spec.Ref)
    66  			fmt.Println("  Path       : " + spec.Path)
    67  			fmt.Println("  Descriptor : " + spec.Path)
    68  			fmt.Println("  Description: " + spec.Description)
    69  			fmt.Println()
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func includeContrib(details *util.AppImportDetails, filter string) bool {
    77  
    78  	if details.IsCoreContrib() {
    79  
    80  		switch strings.ToLower(filter) {
    81  		case "used":
    82  			return details.Referenced()
    83  		case "unused":
    84  			return !details.Referenced()
    85  		default:
    86  			return true
    87  		}
    88  	}
    89  
    90  	return false
    91  
    92  }
    93  
    94  type ContribSpec struct {
    95  	Name        string      `json:"name"`
    96  	Type        string      `json:"type"`
    97  	Description string      `json:"description"`
    98  	Homepage    string      `json:"homepage"`
    99  	Ref         string      `json:"ref"`
   100  	Path        string      `json:"path"`
   101  	Descriptor  string      `json:"descriptor"`
   102  	IsLegacy    interface{} `json:"isLegacy,omitempty"`
   103  }
   104  
   105  func getContribSpec(project common.AppProject, details *util.AppImportDetails) *ContribSpec {
   106  	path, err := project.GetPath(details.Imp)
   107  	if err != nil {
   108  		return nil
   109  	}
   110  
   111  	if Verbose() {
   112  		fmt.Println("Path of contrib", path, "for contrib", details.Imp)
   113  	}
   114  
   115  	desc := details.ContribDesc
   116  
   117  	spec := &ContribSpec{}
   118  	spec.Name = desc.Name
   119  	spec.Type = desc.Type
   120  	spec.Description = desc.Description
   121  	spec.Homepage = desc.Homepage
   122  	spec.Ref = details.Imp.ModulePath()
   123  	spec.Path = path
   124  
   125  	if desc.IsLegacy {
   126  		spec.IsLegacy = true
   127  		spec.Descriptor = desc.GetContribType() + ".json"
   128  	} else {
   129  		spec.Descriptor = "descriptor.json"
   130  	}
   131  
   132  	return spec
   133  }
   134  func ListOrphanedRefs(project common.AppProject, jsonFormat bool) error {
   135  
   136  	ai, err := util.GetAppImports(filepath.Join(project.Dir(), fileFlogoJson), project.DepManager(), true)
   137  	if err != nil {
   138  		return err
   139  	}
   140  
   141  	orphaned := ai.GetOrphanedReferences()
   142  
   143  	if jsonFormat {
   144  		resp, err := json.MarshalIndent(orphaned, "", "  ")
   145  		if err != nil {
   146  			return err
   147  		}
   148  
   149  		fmt.Fprintf(os.Stdout, "%v \n", string(resp))
   150  	} else {
   151  		for _, ref := range orphaned {
   152  			fmt.Println(ref)
   153  		}
   154  	}
   155  
   156  	return nil
   157  }