// 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 } }