Example #1
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
}