Beispiel #1
0
func loadGraph(graphfile string) (g *graph.Description, err error) {
	data, err := ioutil.ReadFile(graphfile)
	if err != nil {
		return nil, fmt.Errorf("Failed to read graph definition from file: %s", err.Error())
	}
	if strings.HasSuffix(graphfile, ".fbp") {
		g, err = graph.ParseFBP(data)
	} else if strings.HasSuffix(graphfile, ".json") {
		g, err = graph.ParseJSON(data)
	} else {
		return nil, fmt.Errorf("Unsupported graph format (should be .fbp or .json): %s", err.Error())
	}
	if err != nil {
		return nil, fmt.Errorf("Failed to parse graph definition: %s", err.Error())
	}

	return g, nil
}
Beispiel #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
}