func remove(c *cli.Context) { if len(c.Args()) != 1 { cli.ShowCommandHelp(c, "remove") fatalErrorf("Expected exactly 1 argument instead of %d", len(c.Args())) } project, err := godm.NewLocalProject(path.Dir(os.Args[0]), "") if err != nil { fatalErrorf("Error building the current project : %s", err.Error()) } Log.Debug("Project's Type : %T", project, project.GetBaseDir()) Log.Debug("Project's Base Dir : %s", project.GetBaseDir()) importPath := c.Args().First() err = project.RemoveVendor(importPath) if err == godm.ErrUnknownVendor { Log.Warning("Import path %q cannot be removed from vendors because it is not vendored in current project", importPath) return } else if err != nil { fatalErrorf("Error removing import path %q from vendors : %s", importPath, err.Error()) } Log.Notice("%q removed from vendors", importPath) }
func clean(c *cli.Context) { if len(c.Args()) > 0 { cli.ShowCommandHelp(c, "clean") fatalErrorf("Too much arguments") } project, err := godm.NewLocalProject(path.Dir(os.Args[0]), "") if err != nil { fatalErrorf("Error building the current project : %s", err.Error()) } Log.Debug("Project's Type : %T", project, project.GetBaseDir()) Log.Debug("Project's Base Dir : %s", project.GetBaseDir()) imports, err := project.GetImports() if err != nil { fatalErrorf("Error listing import paths : %s", err.Error()) } Log.Info("Found %d imports", len(imports)) if len(imports) == 0 { // No imports, nothing to do return } vendors, err := project.GetVendors() if err != nil { fatalErrorf("Error listing vendors : %s", err.Error()) } Log.Info("Found %d vendors", len(vendors)) Log.Debug("Vendors list : %# v", vendors) errorCount := 0 vendorsLoop: for vendorImportPath := range vendors { if !imports.Has(vendorImportPath) { for importPath := range imports { if strings.HasPrefix(importPath, vendorImportPath) { // We are importing a sub-package of this vendor continue vendorsLoop } } err = project.RemoveVendor(vendorImportPath) if err != nil { errorCount++ Log.Error("Error removing vendor %q : %s", vendorImportPath, err.Error()) } else { fmt.Println(vendorImportPath) } } } if errorCount > 0 { os.Exit(errorCount) } }
func main() { checkGo15VendorActivated() cli.VersionFlag.Name = "version" // We use "v" for verbose app := cli.NewApp() app.Name = "godm" app.Usage = "Package Manager for Go 1.5+" app.Authors = []cli.Author{ { Name: "HectorJ", Email: "*****@*****.**", }, } app.Flags = []cli.Flag{ cli.BoolFlag{ Name: "verbose", Usage: "Verbose output", }, } app.EnableBashCompletion = true app.Before = func(c *cli.Context) error { logging.SetFormatter(logFormatter) backend := logging.AddModuleLevel(logging.NewLogBackend(os.Stderr, "", 0)) if c.GlobalBool("verbose") { backend.SetLevel(logging.DEBUG, "") } else { backend.SetLevel(logging.WARNING, "") } Log.SetBackend(backend) return nil } app.Commands = []cli.Command{ { Name: "clean", Usage: "Removes vendors that are not imported in the current project. Outputs the import paths of vendors successfully removed.", Action: clean, }, { Name: "vendor", Usage: "Vendors imports that are not vendored yet in the current project. Outputs the import paths of vendors successfully added.", Flags: []cli.Flag{ // Removed for now. Here is the current behavior : // - For Git projects : we start from the root dir and do scan recursively all sub-packages (except vendors) // - For "no-vcl" projects : we only scan the current dir we're in // cli.BoolFlag{ // Name: "r,recursive", // Usage: "Scan dirs recursively for sub-packages", // }, // Removed for now. Here is the current behavior : // We exclude "vendor" directories // cli.StringSliceFlag{ // Name: "e,exclude", // Usage: "Files/Directories names to exclude from scanning, as regexp.", // }, }, Action: vendor, }, { Name: "remove", Aliases: []string{"rm"}, Usage: "Unvendors an import path. Takes a single import path as argument.", Flags: []cli.Flag{ // Removed for now, as there is no confirmation asked anywhere // cli.BoolFlag{ // Name: "y,yes", // Usage: "Remove the submodule without asking any confirmation", // }, }, Action: remove, BashComplete: func(c *cli.Context) { if len(c.Args()) > 0 { return } project, err := godm.NewLocalProject(path.Dir(os.Args[0]), "") if err != nil { return } vendors, err := project.GetVendors() if err != nil { return } for importPath := range vendors { fmt.Println(importPath) } }, }, } app.RunAndExitOnError() }
func vendor(c *cli.Context) { if len(c.Args()) > 0 { cli.ShowCommandHelp(c, "vendor") fatalErrorf("Too much arguments") } project, err := godm.NewLocalProject(path.Dir(os.Args[0]), "") if err != nil { fatalErrorf("Error building the current project : %s", err.Error()) } Log.Debug("Project's Type : %T", project, project.GetBaseDir()) Log.Debug("Project's Base Dir : %s", project.GetBaseDir()) imports, err := project.GetImports() if err != nil { fatalErrorf("Error listing import paths : %s", err.Error()) } Log.Info("Found %d imports", len(imports)) if len(imports) == 0 { // No imports, nothing to do return } vendors, err := project.GetVendors() if err != nil { fatalErrorf("Error listing vendors : %s", err.Error()) } Log.Info("Found %d vendors", len(vendors)) Log.Debug("Vendors list : %# v", vendors) importsLoop: for importPath := range imports { if _, exists := vendors[importPath]; !exists { vendorProject, canonicalImportPath, err := godm.NewProjectFromImportPath(importPath) if _, exists := vendors[canonicalImportPath]; exists { Log.Info("%q is a sub-package of %q which is already vendored", importPath, canonicalImportPath) continue importsLoop } if localVendorProject, ok := vendorProject.(godm.LocalProject); ok { if strings.HasPrefix(localVendorProject.GetBaseDir(), project.GetBaseDir()) { Log.Debug("%q ignored because it is a sub-package of current project", canonicalImportPath) continue importsLoop } } if err != nil { if err == godm.ErrStandardLibrary { Log.Debug("%q ignored because part of the standard library", canonicalImportPath) err = nil } else { fatalErrorf("Error with import path %q : %s", canonicalImportPath, err.Error()) } } else { _, err = project.AddVendor(canonicalImportPath, vendorProject) if err != nil { fatalErrorf("Error vendoring import path %q : %s", canonicalImportPath, err.Error()) } else { Log.Notice("%q vendored", canonicalImportPath) fmt.Println(canonicalImportPath) } } } else { Log.Info("%q is already vendored", importPath) } } }