Documentation ¶
Overview ¶
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var CheckCmd = &cobra.Command{ Use: "check", Short: "Validate the configuration file", PreRunE: func(cmd *cobra.Command, args []string) error { return utils.Getwd() }, RunE: func(cmd *cobra.Command, args []string) error { quiet, _ := cmd.Flags().GetBool("quiet") configPath, _ := cmd.Flags().GetString("config") if configPath == "" { configPath = utils.Wd resolved, err := config.Resolve("grab.hcl", configPath) if err != nil { utils.PrintDiag(cmd.ErrOrStderr(), &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "could not resolve config file", Detail: err.Error(), }) return utils.ErrSilent } configPath = resolved } fc, err := utils.Io.ReadFile(utils.Fs, configPath) if err != nil { utils.PrintDiag(cmd.ErrOrStderr(), &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "could not read config file", Detail: err.Error(), }) return utils.ErrSilent } _, _, _, diags := config.Parse(fc, configPath) if diags.HasErrors() { for _, diag := range diags { utils.PrintDiag(cmd.ErrOrStderr(), diag) } return utils.ErrSilent } if !quiet { cmd.Println("ok") } return nil }, }
View Source
var ConfigCmd = &cobra.Command{
Use: "config",
Short: "Manage the configuration file",
Example: ` Check for errors in the configuration file:
grab config check -c ../grab.hcl
Generate the default configuration:
grab config generate
Print the path of the closest config file:
grab config find`,
}
View Source
var FindCmd = &cobra.Command{ Use: "find", Short: "Print the path of the closest configuration file", Long: `By default, this program will search in all parent directories of the current directory for a configuration file. To specify a path use the --path flag.`, PreRunE: func(cmd *cobra.Command, args []string) error { return utils.Getwd() }, RunE: func(cmd *cobra.Command, args []string) error { startPath, _ := cmd.Flags().GetString("path") if startPath == "" { startPath = utils.Wd } else { if !filepath.IsAbs(startPath) { startPath = utils.Abs(startPath) } if exists, err := utils.Io.Exists(utils.Fs, startPath); err != nil || !exists { cmd.PrintErrf("path does not exist: %s\n", startPath) return utils.ErrSilent } } resolved, err := config.Resolve("grab.hcl", startPath) if err != nil { cmd.PrintErrln(err) return utils.ErrSilent } cmd.Println(resolved) return nil }, }
View Source
var GenerateCmd = &cobra.Command{ Use: "generate", Short: "Generate the default configuration file in the current directory", Long: `To write the file contents to stdout, use the --stdout flag`, PreRunE: func(cmd *cobra.Command, args []string) error { return utils.Getwd() }, RunE: func(cmd *cobra.Command, args []string) error { stdout, _ := cmd.Flags().GetBool("stdout") homedir, err := os.UserHomeDir() if err != nil { cmd.PrintErrf("could not get user home directory: %v\n", err) return utils.ErrSilent } homedir = strings.Replace(homedir, "\\", "\\\\", -1) data := &ConfigTemplateData{ Location: filepath.Join(homedir, "Downloads", "grab"), } buffer := new(bytes.Buffer) tmpl := template.Must(template.New("test").Parse(configTemplate)) if err = tmpl.Execute(buffer, data); err != nil { cmd.PrintErrf("could not generate default config from template: %v\n", err) return utils.ErrSilent } if stdout { cmd.Print(buffer.String()) return nil } outPath := filepath.Join(utils.Wd, "grab.hcl") if err = utils.Io.WriteFile(utils.Fs, outPath, buffer.Bytes(), os.ModePerm); err != nil { cmd.PrintErrf("could not write config to file: %v\n", err) return utils.ErrSilent } return nil }, }
View Source
var GetCmd = &cobra.Command{ Use: "get", Short: "Scrape and download assets from a URL, a file or a both", Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { log.Logger = instance.DefaultLogger(cmd.OutOrStderr()) g := instance.New(cmd) g.ParseFlags() if diags := g.ParseConfig(); diags.HasErrors() { for _, diag := range diags.Errs() { log.Err(diag).Msg("config error") } return utils.ErrSilent } if diags := g.ParseURLs(args); diags.HasErrors() { for _, diag := range diags.Errs() { log.Err(diag).Msg("argument error") } return utils.ErrSilent } updateMessageChan := make(chan string) go func() { newVersion, err := update.CheckForUpdates(config.Version, config.LatestReleaseURL) if err != nil { updateMessageChan <- "" } updateMessageChan <- newVersion }() g.BuildSiteCache() if diags := g.BuildAssetCache(); diags.HasErrors() { for _, diag := range diags.Errs() { log.Err(diag).Msg("runtime error") } return utils.ErrSilent } if err := g.Download(); err != nil { return utils.ErrSilent } latest := <-updateMessageChan if latest != "" { cmd.Printf("\n\n%s %s → %s\n", color.New(color.FgMagenta).Sprintf("A new release of %s is available:", config.Name), config.Version, color.New(color.FgCyan).Sprint(latest), ) cmd.Printf("%s\n\n", "https://github.com/everdrone/grab/releases/latest") } return nil }, }
View Source
var RootCmd = &cobra.Command{ Use: "grab", Short: "Download and scrape web pages based on a set of regex patterns", Example: ` Generate the starter config file: grab config generate After customizing it, check for errors: grab config check Scrape and download assets from a url: grab get http://example.com Scrape and download assets from a list of urls: grab get list.ini`, SilenceErrors: true, SilenceUsage: true, Args: cobra.NoArgs, }
View Source
var VersionCmd = &cobra.Command{ Use: "version", Short: "Print the version number and check for updates", Run: func(cmd *cobra.Command, args []string) { updateMessageChan := make(chan string) go func() { newVersion, _ := update.CheckForUpdates(config.Version, config.LatestReleaseURL) updateMessageChan <- newVersion }() cmd.Printf("%s v%s %s/%s (%s)\n", cmd.Root().Name(), config.Version, config.BuildOS, config.BuildArch, config.CommitHash[:7]) latest := <-updateMessageChan if latest != "" { cmd.Printf("\n\n%s %s → %s\n", color.New(color.FgMagenta).Sprintf("A new release of %s is available:", config.Name), config.Version, color.New(color.FgCyan).Sprint(latest), ) cmd.Printf("%s\n\n", "https://github.com/everdrone/grab/releases/latest") } }, }
Functions ¶
Types ¶
type ConfigTemplateData ¶
type ConfigTemplateData struct {
Location string
}
Source Files ¶
Click to show internal directories.
Click to hide internal directories.