예제 #1
0
func graphToEntry(g *graph.Description, path string, r library.Registrar) (*library.Entry, error) {
	entry := &library.Entry{
		Executable:  path,
		Description: g.Properties["name"],
		Inports:     []library.EntryPort{},
		Outports:    []library.EntryPort{},
	}

	for _, e := range g.Inports {
		parts := strings.SplitN(e.Private, ".", 2)
		rec, err := r.Get(g.Processes[parts[0]].Component)
		if err != nil {
			return nil, fmt.Errorf("Component %s not found in library", parts[0])
		}
		port, found := rec.FindInport(strings.ToLower(parts[1]))
		if !found {
			return nil, fmt.Errorf("Port %s not found in component %s", parts[1], parts[0])
		}
		port.Name = e.Public
		entry.Inports = append(entry.Inports, port)
	}

	for _, e := range g.Outports {
		parts := strings.SplitN(e.Private, ".", 2)
		rec, err := r.Get(g.Processes[parts[0]].Component)
		if err != nil {
			return nil, fmt.Errorf("Component %s not found in library", parts[0])
		}
		port, found := rec.FindOutport(strings.ToLower(parts[1]))
		if !found {
			return nil, fmt.Errorf("Port %s not found in component %s", parts[1], parts[0])
		}
		port.Name = e.Public
		entry.Outports = append(entry.Outports, port)
	}

	return entry, nil
}
예제 #2
0
func addFileToLibrary(c *cli.Context, r library.Registrar, file string, name string) error {
	var entry *library.Entry
	if strings.HasSuffix(file, ".fbp") {
		// adding a compsite component (subgraph) in .fbp format
		data, err := ioutil.ReadFile(file)
		if err != nil {
			return err
		}
		g, err := graph.ParseFBP(data)
		if err != nil {
			return err
		}
		if entry, err = graphToEntry(g, file, r); err != nil {
			return err
		}
		entry.Elementary = false

	} else if strings.HasSuffix(file, ".json") {
		// adding a compsite component (subgraph) in .json format
		data, err := ioutil.ReadFile(file)
		if err != nil {
			return err
		}
		g, err := graph.ParseJSON(data)
		if err != nil {
			return err
		}
		if entry, err = graphToEntry(g, file, r); err != nil {
			return err
		}
		entry.Elementary = false

	} else {
		// adding an elementary component
		c := exec.Command(file, "--json")
		out, err := c.Output()
		if err != nil {
			return fmt.Errorf("Cannot register component %s: %s\n", name, err.Error())
		}
		if err = json.Unmarshal(out, &entry); err != nil {
			return err
		}
		entry.Elementary = true
	}

	if len(entry.Inports) == 0 && len(entry.Outports) == 0 {
		return fmt.Errorf("Cannot register component %s: inports and outports are empty", name)
	}

	entry.Name = name
	entry.Executable = file
	if r.Exists(name) && !c.Bool("force") {
		fmt.Printf("WARNING \"%s\" already exists and --force is not provided. Ignoring this entry", name)
		fmt.Println("")
	} else {
		r.Add(*entry)
		fmt.Printf("Added \"%s\"", name)
		fmt.Println("")
	}

	return nil
}