func infoFromLibrary(c *cli.Context) { if len(c.Args()) != 1 { fmt.Printf("Incorrect Usage. You need to provide a component name as argument!\n\n") cli.ShowAppHelp(c) return } // read components library file if exists data, err := ioutil.ReadFile(c.GlobalString("file")) if err != nil && os.IsExist(err) { fmt.Printf("Failed to read catalogue file: %s", err.Error()) return } if data == nil { fmt.Println("Library is empty") return } // create JSON library (the only implementation for now) var db library.JSONLibrary err = json.Unmarshal(data, &db) if err != nil { fmt.Printf("Library file is invalid: %s", err.Error()) return } component := strings.ToLower(c.Args().First()) for _, e := range db.List() { if e.Name != component { continue } fmt.Printf("NAME:\n %s\n", e.Name) fmt.Printf("LOCATION:\n %s\n", e.Executable) fmt.Println("INPUTS:") if len(e.Inports) == 0 { fmt.Println(" None") } else { for _, p := range e.Inports { fmt.Printf(" %s - type=%s, array=%v, required=%v\n", strings.ToUpper(p.Name), p.Type, p.Addressable, p.Required) } } fmt.Println("OUTPUTS:") if len(e.Outports) == 0 { fmt.Println(" None") } else { for _, p := range e.Outports { fmt.Printf(" %s - type=%s, array=%v, required=%v\n", strings.ToUpper(p.Name), p.Type, p.Addressable, p.Required) } } fmt.Printf("DESCRIPTION:\n %s\n", e.Description) break } }
// Print catalog command func listLibrary(c *cli.Context) { // read components library file if exists data, err := ioutil.ReadFile(c.GlobalString("file")) if err != nil && os.IsExist(err) { fmt.Printf("Failed to read catalogue file: %s", err.Error()) return } if data == nil { fmt.Println("Library is empty") return } // create JSON library (the only implementation for now) var db library.JSONLibrary err = json.Unmarshal(data, &db) if err != nil { fmt.Printf("Library file is invalid: %s", err.Error()) return } for _, e := range db.List() { fmt.Println(e.Name) } }
// Implements catalog updating command func addToLibrary(c *cli.Context) { if len(c.Args()) != 1 { fmt.Printf("Incorrect Usage. You need to provide a directory/file path as argument!\n\n") cli.ShowAppHelp(c) return } // read components library file if exists data, err := ioutil.ReadFile(c.GlobalString("file")) if err != nil && os.IsExist(err) { fmt.Printf("Failed to read catalogue file: %s", err.Error()) return } // create JSON library (the only implementation for now) var db library.JSONLibrary if data != nil { err = json.Unmarshal(data, &db) } if err != nil { db = library.JSONLibrary{ Entries: make(map[string]library.Entry), } db.Name = "Local Components Library" db.Created = time.Now() } info, err := os.Stat(c.Args().First()) if err != nil { fmt.Printf("Failed to access given directory/file: %s", err.Error()) fmt.Println("") return } if info.IsDir() { path, err := filepath.Abs(c.Args().First()) if err != nil { fmt.Printf("Failed to resolve absolute path for %s: %s", c.Args().First(), err.Error()) fmt.Println("") return } addDirToLibrary(c, db, path) } else { if c.String("name") == "" { fmt.Println("You need to provide a name when adding a component file") cli.ShowAppHelp(c) fmt.Println("") return } err = addFileToLibrary(c, db, c.Args().First(), c.String("name")) if err != nil { fmt.Printf("Failed to add a component: %s", err.Error()) fmt.Println("") return } } // write index back or create if not exists db.Updated = time.Now() result, err := db.JSON() if err != nil { fmt.Printf("Failed to generate JSON: %s", err.Error()) return } err = ioutil.WriteFile(c.GlobalString("file"), result, os.FileMode(0644)) if err != nil { fmt.Printf("Failed to save registry file: %s", err.Error()) return } }