Skip to content

Commit

Permalink
refactor: add special case for repos, packages and collections
Browse files Browse the repository at this point in the history
The given path will now be parsed to an URL structure in case of a repo,
package or collection. For configs or folders the path stays unchanged.
  • Loading branch information
nikoksr committed May 26, 2020
1 parent 8a1afca commit c154dd2
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions cmd/proji/cmd/classImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cmd

import (
"fmt"
"net/url"

"github.com/nikoksr/proji/pkg/proji/repo"

"github.com/nikoksr/proji/pkg/helper"
"github.com/nikoksr/proji/pkg/proji/storage/item"
Expand Down Expand Up @@ -70,56 +73,70 @@ func init() {
_ = classImportCmd.MarkFlagFilename("exclude")
}

func importClass(location, importType string, excludes []string) (string, error) {
if helper.IsInSlice(excludes, location) {
func importClass(path, importType string, excludes []string) (string, error) {
if helper.IsInSlice(excludes, path) {
return "", nil
}

class := item.NewClass("", "", false)
var err error
var confName, msg string
var URL *url.URL
var importer repo.Importer

// In case of a repo, package or collection try to parse the path to a URL structure
if importType == "repo" || importType == "package" || importType == "collection" {
URL, err = repo.ParseURL(path)
if err != nil {
return "", err
}
importer, err = item.GetRepoImporterFromURL(URL)
if err != nil {
return "", err
}
}

switch importType {
case "config":
err = class.ImportConfig(location)
err = class.ImportConfig(path)
if err != nil {
return "", err
}
err = projiEnv.Svc.SaveClass(class)
if err == nil {
msg = fmt.Sprintf("> Successfully imported class '%s' from '%s'", class.Name, location)
msg = fmt.Sprintf("> Successfully imported class '%s' from '%s'", class.Name, path)
}
case "dir":
err = class.ImportFolderStructure(location, excludes)
err = class.ImportFolderStructure(path, excludes)
if err != nil {
return "", err
}
case "repo":
err = class.ImportRepoStructure(location)
err = class.ImportRepoStructure(URL, importer, nil)
if err != nil {
return "", err
}
case "package":
err = class.ImportPackage(location)
err = class.ImportPackage(URL, importer)
if err != nil {
return "", err
}
err = projiEnv.Svc.SaveClass(class)
if err == nil {
msg = fmt.Sprintf("> Successfully imported class '%s' from '%s'", class.Name, location)
msg = fmt.Sprintf("> Successfully imported class '%s' from '%s'", class.Name, path)
}
case "collection":
classList := make([]*item.Class, 0)
classList, err = item.ImportClassesFromCollection(location)
classList, err = item.ImportClassesFromCollection(URL, importer)
if err != nil {
return "", err
}
for _, class := range classList {
err = projiEnv.Svc.SaveClass(class)
if err == nil {
msg += fmt.Sprintf("> Successfully imported class '%s' from '%s'\n", class.Name, location)
msg += fmt.Sprintf("> Successfully imported class '%s' from '%s'\n", class.Name, path)
} else {
msg += fmt.Sprintf("> Importing class '%s' from '%s' failed: %v\n", class.Name, location, err)
msg += fmt.Sprintf("> Importing class '%s' from '%s' failed: %v\n", class.Name, path, err)
}
}
default:
Expand All @@ -131,7 +148,7 @@ func importClass(location, importType string, excludes []string) (string, error)
if importType != "config" && importType != "package" && importType != "collection" {
confName, err = class.Export(".")
if err == nil {
msg = fmt.Sprintf("> '%s' was successfully exported to '%s'", location, confName)
msg = fmt.Sprintf("> '%s' was successfully exported to '%s'", path, confName)
}
}
return msg, err
Expand Down

0 comments on commit c154dd2

Please sign in to comment.