Skip to content

Commit

Permalink
feat: add package and collection flag to 'class import'
Browse files Browse the repository at this point in the history
In near future the import of proji packages and collections will be
supported. Note that right now this is only adds the commandline flags;
there is no functionality behind it yet!
  • Loading branch information
nikoksr committed Mar 17, 2020
1 parent 0a95666 commit 426ac10
Showing 1 changed file with 52 additions and 32 deletions.
84 changes: 52 additions & 32 deletions cmd/proji/cmd/classImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,42 @@ import (
"github.com/spf13/cobra"
)

var remoteRepos, directories, configs, excludes []string
var remoteRepos, directories, configs, excludes, packages, collections []string

var classImportCmd = &cobra.Command{
Use: "import FILE [FILE...]",
Short: "Import one or more classes",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(configs) < 1 && len(directories) < 1 && len(remoteRepos) < 1 {
return fmt.Errorf("no flag was passed. You have to pass the '--config', '--directory' or '--remote-repo' flag at least once")
return fmt.Errorf("no flag given")
}
excludes = append(excludes, projiEnv.Excludes...)
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
// Concat the two arrays so that '... import --config *.toml' is a valid command.
// Without appending the args, proji would only use the first toml-file and not all of
// them as intended with the '*'.
configs = append(configs, args...)
pathMap := map[string][]string{"files": configs, "dirs": directories, "urls": remoteRepos}
importTypes := map[string][]string{
"config": configs,
"dir": directories,
"repo": remoteRepos,
"package": packages,
"collection": collections,
}

// Import configs
for pathType, paths := range pathMap {
for _, path := range paths {
result, err := importClass(path, pathType, excludes)
for importType, locations := range importTypes {
for _, location := range locations {
result, err := importClass(location, importType, excludes)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Println(result)
}
}
}
return nil
},
}

Expand All @@ -55,51 +60,66 @@ func init() {
classImportCmd.Flags().StringSliceVar(&configs, "config", make([]string, 0), "import a class from a config file")
_ = classImportCmd.MarkFlagFilename("config")

classImportCmd.Flags().StringSliceVar(&packages, "package", make([]string, 0), "import a package")
_ = classImportCmd.MarkFlagFilename("package")

classImportCmd.Flags().StringSliceVar(&collections, "collection", make([]string, 0), "import a collection of packages")
_ = classImportCmd.MarkFlagFilename("collection")

classImportCmd.Flags().StringSliceVar(&excludes, "exclude", make([]string, 0), "folder to exclude from local directory import")
_ = classImportCmd.MarkFlagFilename("exclude")
}

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

class := item.NewClass("", "", false)
var err error
var confName, msg string

switch pathType {
case "files":
err = class.ImportFromConfig(path)
switch importType {
case "config":
err = class.ImportFromConfig(location)
if err != nil {
return "", err
}
err = projiEnv.Svc.SaveClass(class)
if err == nil {
msg = fmt.Sprintf("> Successfully imported class '%s' from '%s'", class.Name, path)
msg = fmt.Sprintf("> Successfully imported class '%s' from '%s'", class.Name, location)
}
case "dirs":
fallthrough
case "urls":
if pathType == "dirs" {
err = class.ImportFromDirectory(path, excludes)
if err != nil {
return "", err
}
} else {
err = class.ImportFromURL(path)
if err != nil {
return "", err
}
case "dir":
err = class.ImportFromDirectory(location, excludes)
if err != nil {
return "", err
}
case "repo":
err = class.ImportFromRepo(location)
if err != nil {
return "", err
}
// Classes that are generated from directories and URLs should be exported to a config file first
// so that the user can fine tune them
case "package":
err = class.ImportFromPackage(location)
if err != nil {
return "", err
}
case "collection":
err = class.ImportFromCollection(location)
if err != nil {
return "", err
}
default:
err = fmt.Errorf("path type %s is not supported", importType)
}

// Classes that are generated from directories or repos (structure, package and collection) should be exported to a config file first
// so that the user can fine tune them
if importType != "config" {
confName, err = class.Export(".")
if err == nil {
msg = fmt.Sprintf("> '%s' was successfully exported to '%s'", path, confName)
msg = fmt.Sprintf("> '%s' was successfully exported to '%s'", location, confName)
}
default:
err = fmt.Errorf("path type %s is not supported", pathType)
}
return msg, err
}

0 comments on commit 426ac10

Please sign in to comment.