Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (113 sloc) 3.93 KB
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
func init() {
cmdUpdate.Flags().StringSliceP("exclude", "E", nil, "Set the prefix of the modules to exclude")
cmdUpdate.Flags().StringSliceP("folder", "f", []string{"."}, "Set the path to the folder file")
cmdUpdate.Flags().BoolP("recursive", "r", false, "If true, remod will look for mod files in given --folder and all 1 level subfolders")
cmdUpdate.Flags().String("version", "latest", "Set to which version you want to update the matching modules")
var cmdUpdate = &cobra.Command{
Use: "update",
Aliases: []string{"up"},
Short: "Update the module in bulk",
Long: `This command allows to update multiple modules to a new version at once
It will update all the modules prefixed by the given arguments, excluding the ones
passed through the '--exclude' flag to the given '--version'.
For instance:
remod update --version master --exclude
This will update all modules starting with '' but 'trireme-lib' to the latest
master version.
It is also possible to pass a list of folder with the flag '--folder' to perform this update
on a list of repository. if the '--recursive' flag is set in addition to '--folder' remod
will check all 1 level subfolders and do the update in these repositories.
PreRunE: func(cmd *cobra.Command, args []string) error {
return viper.BindPFlags(cmd.Flags())
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
folders := viper.GetStringSlice("folder")
if len(args) == 0 {
return fmt.Errorf("you must at least pass one argument")
if remod.Enabled() {
if err := remod.Off(); err != nil {
return fmt.Errorf("unable to set remod to off: %s", err)
defer func() {
if err := remod.On(); err != nil {
included := args
recursive := viper.GetBool("recursive")
excluded := viper.GetStringSlice("exclude")
version := viper.GetString("version")
for _, folder := range folders {
var paths []string
if recursive && folder != "." {
items, err := ioutil.ReadDir(folder)
if err != nil {
return fmt.Errorf("unable to list content of dir: %s", err)
for _, item := range items {
if !item.IsDir() {
p := path.Join(folder, item.Name(), "go.mod")
_, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("unable stat path '%s': %s", p, err)
paths = append(paths, p)
} else {
paths = append(paths, path.Join(folder, "go.mod"))
for _, p := range paths {
basedir := filepath.Dir(p)
if err := os.Chdir(basedir); err != nil {
return fmt.Errorf("unable to move to %s: %s", basedir, err)
if len(paths) > 1 {
fmt.Println("* Entering", basedir)
idata, err := ioutil.ReadFile(p)
if err != nil {
return fmt.Errorf("unable to read go.mod: %s", err)
modules, err := remod.Extract(idata, included, excluded)
if err != nil {
return fmt.Errorf("unable to extract modules: %s", err)
if err := remod.Update(modules, version); err != nil {
return fmt.Errorf("unable to extract modules: %s", err)
return nil
You can’t perform that action at this time.