github.com/dop251/modtools@v0.0.0-20220314120634-3b2fc95d1790/cmd/freeze.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "strconv" 6 "time" 7 8 "github.com/muesli/coral" 9 ) 10 11 const ( 12 defaultFreezeDays = 7 13 ) 14 15 func init() { 16 rootCmd.AddCommand(&coral.Command{ 17 Use: "freeze modpath [days]", 18 Short: "Freeze a dependency", 19 Long: "Adds the specified module path to the list of exceptions. The check and update commands will ignore this " + 20 "module for the specified number of days\n(defaults to " + strconv.Itoa(defaultFreezeDays) + 21 ").\nDon't forget to add " + frozendepsFilename + " to the repository.", 22 Args: coral.RangeArgs(1, 2), 23 RunE: func(cmd *coral.Command, args []string) error { 24 var days int 25 if len(args) > 1 { 26 var err error 27 days, err = strconv.Atoi(args[1]) 28 if err != nil { 29 return err 30 } 31 } else { 32 days = defaultFreezeDays 33 } 34 until := time.Now().Add(time.Duration(days) * 24 * time.Hour).Truncate(time.Second) 35 return freeze(args[0], until) 36 }, 37 }) 38 } 39 40 func freeze(p string, until time.Time) error { 41 e, err := loadExceptions() 42 if err != nil { 43 return err 44 } 45 deps, err := readDeps(false, false) 46 if err != nil { 47 return err 48 } 49 curVersion := "" 50 for _, item := range deps { 51 if item.Path == p { 52 curVersion = item.Version 53 break 54 } 55 } 56 if curVersion == "" { 57 return fmt.Errorf("module %q is not a dependency", p) 58 } 59 e.Add(&Exception{ 60 Path: p, 61 MinVersion: curVersion, 62 ValidUntil: until, 63 }) 64 wasNew := e.IsNew() 65 err = e.Save() 66 if err == nil { 67 if wasNew { 68 fmt.Println("Don't forget to add " + frozendepsFilename + " to the repository.") 69 } 70 } 71 return err 72 }