github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/cmd/get.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/everdrone/grab/internal/config"
     5  	"github.com/everdrone/grab/internal/instance"
     6  	"github.com/everdrone/grab/internal/update"
     7  	"github.com/everdrone/grab/internal/utils"
     8  	"github.com/fatih/color"
     9  	"github.com/rs/zerolog/log"
    10  
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var GetCmd = &cobra.Command{
    15  	Use:   "get",
    16  	Short: "Scrape and download assets from a URL, a file or a both",
    17  	Args:  cobra.MinimumNArgs(1),
    18  	RunE: func(cmd *cobra.Command, args []string) error {
    19  		log.Logger = log.Output(instance.DefaultLogger(cmd.OutOrStderr()))
    20  
    21  		g := instance.New(cmd)
    22  		g.ParseFlags()
    23  
    24  		if diags := g.ParseConfig(); diags.HasErrors() {
    25  			for _, diag := range diags.Errs() {
    26  				log.Err(diag).Msg("config error")
    27  			}
    28  			return utils.ErrSilent
    29  		}
    30  
    31  		if diags := g.ParseURLs(args); diags.HasErrors() {
    32  			for _, diag := range diags.Errs() {
    33  				log.Err(diag).Msg("argument error")
    34  			}
    35  			return utils.ErrSilent
    36  		}
    37  
    38  		updateMessageChan := make(chan string)
    39  		go func() {
    40  			newVersion, err := update.CheckForUpdates(config.Version, config.LatestReleaseURL)
    41  			if err != nil {
    42  				updateMessageChan <- ""
    43  			}
    44  
    45  			updateMessageChan <- newVersion
    46  		}()
    47  
    48  		g.BuildSiteCache()
    49  		if diags := g.BuildAssetCache(); diags.HasErrors() {
    50  			for _, diag := range diags.Errs() {
    51  				log.Err(diag).Msg("runtime error")
    52  			}
    53  			return utils.ErrSilent
    54  		}
    55  
    56  		if err := g.Download(); err != nil {
    57  			return utils.ErrSilent
    58  		}
    59  
    60  		latest := <-updateMessageChan
    61  		if latest != "" {
    62  			// TODO: take in account possible package managers
    63  			// if for example we installed with homebrew, we should display a different message
    64  			cmd.Printf("\n\n%s %s → %s\n",
    65  				color.New(color.FgMagenta).Sprintf("A new release of %s is available:", config.Name),
    66  				config.Version,
    67  				// color.New(color.FgHiBlack).Sprint(config.Version),
    68  				color.New(color.FgCyan).Sprint(latest),
    69  			)
    70  			cmd.Printf("%s\n\n", "https://github.com/everdrone/grab/releases/latest")
    71  		}
    72  
    73  		return nil
    74  	},
    75  }
    76  
    77  func init() {
    78  	RootCmd.AddCommand(GetCmd)
    79  
    80  	GetCmd.Flags().BoolP("force", "f", false, "overwrite existing files")
    81  	GetCmd.Flags().StringP("config", "c", "", "the path of the config file to use")
    82  
    83  	GetCmd.Flags().BoolP("strict", "s", false, "fail on errors")
    84  	GetCmd.Flags().BoolP("dry-run", "n", false, "do not write on disk")
    85  
    86  	GetCmd.Flags().BoolP("progress", "p", false, "show progress bars")
    87  	GetCmd.Flags().BoolP("quiet", "q", false, "do not emit any output")
    88  	GetCmd.Flags().CountP("verbose", "v", "verbosity level")
    89  }