package Package import ( "errors" "fmt" "io/ioutil" "os" "os/exec" "path/filepath" "strconv" "strings" bolt "go.etcd.io/bbolt" "PackageManager/Archive" "PackageManager/Client/Database" "PackageManager/Client/Filesystem" "PackageManager/Color" "PackageManager/Helper" "PackageManager/Variables" ) func editManifestFile(filePath string) error { var ( cmd *exec.Cmd ) cmd = exec.Command(Variables.Editor, filePath) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout return cmd.Run() } func writeManifestFile(path, name, version string) error { var ( manifest string filePath string e error ) manifest, e = Manifest{ Name: name, Version: version, Dependancies: make(map[string]string), }.CreateManifestString() if e != nil { return e } filePath = filepath.Join(path, "manifest.yml") return ioutil.WriteFile(filePath, []byte(manifest), 0644) } func CreatePackage() error { var ( picksBucket *bolt.Bucket pickedFiles []string pkgFiles []string choices string choicesSplit []string pkgName string pkgVersion string pkgNameVersion string pkg string index int err error ) fmt.Println("Initialising package creation...") err = Database.FsDB.View(func(tx *bolt.Tx) error { picksBucket = tx.Bucket(Variables.FsHashPicksBucket) picksBucket.ForEach(func(k, v []byte) error { pickedFiles = append(pickedFiles, string(k)) return nil }) return nil }) if err != nil { return err } fmt.Println("Added files:") Filesystem.PrintFiles(pickedFiles, Color.Green, true) fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.") choices = Helper.Input() if choices == "" { for _, pkg = range pickedFiles { pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pkg)) } } else { choicesSplit = strings.Split(choices, ",") for _, i := range choicesSplit { index, err = strconv.Atoi(i) if err != nil { return err } if len(pickedFiles) < index { return errors.New("invalid choice") } pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pickedFiles[index])) } } fmt.Println("Please enter the package name:") pkgName = Helper.Input() if pkgName == "" { return errors.New("invalid package name") } fmt.Println("Please enter the package version:") pkgVersion = Helper.Input() if pkgVersion == "" { return errors.New("invalid package name") } fmt.Printf("Package Name: %s\n", pkgName) fmt.Printf("Package Version: %s\n", pkgVersion) fmt.Println("Files to be added") Filesystem.PrintFiles(pkgFiles, Color.Green, false) // TODO: Move this check to below manifest file fmt.Println("Is this correct? [y/N]") if strings.ToLower(Helper.Input()) != "y" { return errors.New("user aborted") } pkgNameVersion = fmt.Sprintf("%s-%s", pkgName, pkgVersion) err = writeManifestFile("/tmp/", pkgName, pkgVersion) if err != nil { return err } err = editManifestFile(filepath.Join("/tmp/", "manifest.yml")) if err != nil { fmt.Println(Color.Fatal(err)) } // TODO: Write this file to a better spot? pkgFiles = append(pkgFiles, "/tmp/manifest.yml") err = Archive.CreateArchive(pkgFiles, pkgNameVersion+".tar.gz") if err != nil { return err } fmt.Printf( Color.Green("\nSuccessfully created package %s\n"), pkgNameVersion, ) return nil }