diff --git a/fetch.go b/fetch.go index 0d757ace..75124afa 100644 --- a/fetch.go +++ b/fetch.go @@ -21,7 +21,7 @@ var cmdFetch = &Command{ -n, -name # name of specific metadata to retrieve (must be used with -type) -d, -directory # override the default target directory -u, -unpack # unpack any zipped static resources (ignored if type is not StaticResource) - -p, -preserve # preserve the zip file + -p, -preserve # preserve the zip file Export specified artifact(s) to a local directory. Use "package" type to retrieve an unmanaged package. @@ -137,8 +137,7 @@ func persistBundles(bundles AuraDefinitionBundleResult, definitions AuraDefiniti if err := os.MkdirAll(root, 0755); err != nil { ErrorAndExit(err.Error()) } - var wd, _ = os.Getwd() - fmt.Println("Working Dir: ", wd) + for key, value := range bundleMap { if err := os.MkdirAll(filepath.Join(root, value), 0755); err != nil { ErrorAndExit(err.Error()) diff --git a/push.go b/push.go index 41c5d447..e87618ff 100644 --- a/push.go +++ b/push.go @@ -204,7 +204,11 @@ func contains(s []string, e string) bool { func pushPackage() { if len(resourcepath) == 0 { - ErrorAndExit(fmt.Sprintf("No resource path sepcified.")) + var packageFolder = findPackageFolder(metadataName[0]) + zipResource(packageFolder, metadataName[0]) + resourcepath.Set(packageFolder + ".resource") + //var dir, _ = os.Getwd(); + //ErrorAndExit(fmt.Sprintf("No resource path sepcified. %s, %s", metadataName[0], dir)) } deployPackage() } @@ -255,6 +259,41 @@ func findMetadataTypeFolder(mdtype string, root string) (folder string) { return } +func findPackageFolder(packageName string) (folder string) { + var wd, _ = os.Getwd() + // We need to start at the metadata folder, go down first + folder = findMetadataFolder(wd) + if len(folder) == 0 { + // Didn't find it, error out + fmt.Println("Could not find metadata folder.") + } + if _, err := os.Stat(filepath.Join(folder, packageName)); err == nil { + folder = filepath.Join(folder, packageName) + } + return +} + +func findMetadataFolder(dir string) (folderPath string) { + filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { + if filepath.Base(path) == "metadata" { + folderPath = path + return errors.New("walk cancelled") + } + return nil + }) + if len(folderPath) == 0 { + // not down, so, go up + for dir != string(os.PathSeparator) { + dir = filepath.Dir(dir) + if filepath.Base(dir) == "metadata" { + folderPath = dir + return + } + } + } + return +} + // This method will use the type that is passed to the -type flag to find all // metadata that matches that type. It will also filter on the metadata // name(s) passed on the -name flag(s). This method also looks for unpacked @@ -274,13 +313,13 @@ func pushByMetadataType() { // Check to see if any names where specified in the -name flag if len(metadataName) == 0 { // Take all - zipResource(path) + zipResource(path, "") } else { for _, name := range metadataName { fname := filepath.Base(path) // Check to see if the resource name matches the one of the ones passed on the -name flag if fname == name { - zipResource(path) + zipResource(path, "") } } } @@ -317,7 +356,7 @@ func pushByMetadataType() { } // Just zip up what ever is in the path -func zipResource(path string) { +func zipResource(path string, topLevelFolder string) { zipfile := new(bytes.Buffer) zipper := zip.NewWriter(zipfile) startPath := path + "/" @@ -329,7 +368,7 @@ func zipResource(path string) { if err != nil { return err } - fl, err := zipper.Create(strings.Replace(path, startPath, "", -1)) + fl, err := zipper.Create(filepath.Join(topLevelFolder, strings.Replace(path, startPath, "", -1))) if err != nil { ErrorAndExit(err.Error()) } @@ -362,7 +401,7 @@ func pushByName() { // Check to see if any names where specified in the -name flag if len(metadataName) == 0 { // Take all - zipResource(path) + zipResource(path, "") } else { for _, name := range metadataName { fname := filepath.Base(path) @@ -372,7 +411,7 @@ func pushByName() { if metadataType == "staticresources" { metadataType = "StaticResource" } - zipResource(path) + zipResource(path, "") } } }