From 426ac1019ddb1658c5f4d6407dc74da77c4f6fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Tue, 17 Mar 2020 23:17:35 +0100 Subject: [PATCH] feat: add package and collection flag to 'class import' 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! --- cmd/proji/cmd/classImport.go | 84 ++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/cmd/proji/cmd/classImport.go b/cmd/proji/cmd/classImport.go index 11e96786..119c8239 100644 --- a/cmd/proji/cmd/classImport.go +++ b/cmd/proji/cmd/classImport.go @@ -9,29 +9,35 @@ 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 { @@ -39,7 +45,6 @@ var classImportCmd = &cobra.Command{ } } } - return nil }, } @@ -55,12 +60,18 @@ 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 } @@ -68,38 +79,47 @@ func importClass(path, pathType string, excludes []string) (string, error) { 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 }