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