func updateCheck(c *config.Config, q *registry.Queue) error { // Check update-check file before updating again shouldCheck, err := checkShouldCheckUpdate() if err != nil { return err } if !shouldCheck { return nil } // Fetch last commits, both localy & remotely latestSha, err := fetchLatestCommit() if err != nil { return err } currentSha, err := fetchCurrentCommit() if err != nil { return err } // Couldn't retrieve current/latest commit, ignore update if latestSha == "" || currentSha == "" { if *config.Verbose { log.Printf("local or remote version was not retrieved correctly\n") } return nil } if err := writeCheckUpdate(); err != nil { return err } // No update, return directly if latestSha == currentSha { if *config.Verbose { log.Printf("same version detected\n") } return nil } // Ask for update if utils.Ask("There's a new CB version. Do you want to auto-update it?") { return q.RunTasks(c, []string{"update@0"}) } return nil }
// Copy a file, using templates if needed, from srcPath to destPath. func copyFile(c *config.Config, appname, srcPath, destPath, root string) (string, error) { // Use a template for the file if needed if strings.HasPrefix(filepath.Base(srcPath), "cbtmpl.") { srcName, err := copyFileTemplate(appname, srcPath) if err != nil { return destPath, fmt.Errorf("copy file template failed: %s", err) } srcPath = srcName destPath = filepath.Join(filepath.Dir(destPath), filepath.Base(destPath)[7:]) } // Open source file src, err := os.Open(srcPath) if err != nil { return destPath, fmt.Errorf("open source file failed: %s", err) } defer src.Close() // Path of the file relative to the root relDest, err := filepath.Rel(root, destPath) if err != nil { return destPath, fmt.Errorf("cannot rel dest path: %s", err) } if _, err := os.Stat(destPath); err != nil { // Stat failed if !os.IsNotExist(err) { return destPath, fmt.Errorf("stat failed: %s", err) } // If it doesn't exists, but the config file is present, we're updating // the contents, ask for creation perms if c != nil { q := fmt.Sprintf("Do you want to create `%s`?", relDest) if !utils.Ask(q) { return destPath, nil } } } else { // If it exists, but they're equal, ignore the copy of this file if equal, err := compareFiles(srcPath, destPath); err != nil { return destPath, fmt.Errorf("compare files failed: %s", err) } else if equal { return destPath, nil } // Otherwise ask the user to overwrite the file q := fmt.Sprintf("Do you want to overwrite `%s`?", relDest) if !utils.Ask(q) { return destPath, nil } } // Open dest file dest, err := os.Create(destPath) if err != nil { return destPath, err } defer dest.Close() // Copy the file contents if *config.Verbose { log.Printf("copy file `%s`\n", relDest) } if _, err := io.Copy(dest, src); err != nil { return destPath, fmt.Errorf("copy file failed: %s", err) } return destPath, nil }