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", RunE: func(cmd *cobra.Command, args []string) error { quiet, _ := cmd.Flags().GetBool("quiet") configPath, _ := cmd.Flags().GetString("config") if configPath == "" { wd, err := os.Getwd() if err != nil { utils.PrintDiag(cmd.ErrOrStderr(), &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "could not get current directory", Detail: err.Error(), }) return utils.ErrSilent } configPath = wd configPath, 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 } } fc, err := os.ReadFile(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.`, RunE: func(cmd *cobra.Command, args []string) error { startPath, err := cmd.Flags().GetString("path") if err != nil { cmd.PrintErrf("could not get path: %v\n", err) return utils.ErrSilent } if startPath == "" { startPath, err = os.Getwd() if err != nil { cmd.PrintErrf("could not get current directory: %v\n", err) return utils.ErrSilent } } else { _, err := os.Stat(startPath) if err != nil { if errors.Is(err, os.ErrNotExist) { cmd.PrintErrf("path does not exist: %s\n", startPath) return utils.ErrSilent } else { cmd.PrintErrf("could not stat path: %v\n", err) return utils.ErrSilent } } if !filepath.IsAbs(startPath) { startPath, err = filepath.Abs(startPath) if err != nil { cmd.PrintErrf("could not get absolute path: %v\n", err) 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. To write the file contents to a file or into a directory, use the --output flag.`, RunE: func(cmd *cobra.Command, args []string) error { stdout, err := cmd.Flags().GetBool("stdout") if err != nil { cmd.PrintErrln(err) return utils.ErrSilent } outpath, err := cmd.Flags().GetString("output") if err != nil { cmd.PrintErrln(err) return utils.ErrSilent } if stdout && outpath != "" { cmd.PrintErrln("error: flag `output` is mutually exclusive with flag `stdout`") cmd.Usage() return utils.ErrSilent } tmpl, err := template.New("test").Parse(configTemplate) if err != nil { cmd.PrintErrf("could not generate default config from template: %v\n", err) return utils.ErrSilent } homedir, err := os.UserHomeDir() if err != nil { cmd.PrintErrf("could not get user home directory: %v\n", err) return utils.ErrSilent } wd, err := os.Getwd() if err != nil { cmd.PrintErrf("could not get current directory: %v\n", err) return utils.ErrSilent } data := &ConfigTemplateData{ Location: filepath.Join(homedir, "Downloads"), } buffer := new(bytes.Buffer) 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 } if outpath == "" { if err = os.WriteFile(filepath.Join(wd, "grab.hcl"), buffer.Bytes(), os.ModePerm); err != nil { cmd.PrintErrf("could not write config to file: %v\n", err) return utils.ErrSilent } } else { fileInfo, err := os.Stat(outpath) if err != nil { cmd.PrintErrf("could not get file info for output path: %v\n", err) return utils.ErrSilent } if fileInfo.IsDir() { outpath = filepath.Join(outpath, "grab.hcl") } if err = os.WriteFile(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 { g := instance.New(cmd) if diags := g.ParseFlags(); diags.HasErrors() { g.Log(0, *diags) return utils.ErrSilent } if diags := g.ParseConfig(); diags.HasErrors() { g.Log(0, *diags) return utils.ErrSilent } if diags := g.ParseURLs(args); diags.HasErrors() { g.Log(0, *diags) return utils.ErrSilent } g.BuildSiteCache() if diags := g.BuildAssetCache(); diags.HasErrors() { g.Log(0, *diags) return utils.ErrSilent } if diags := g.Download(); diags.HasErrors() { g.Log(0, *diags) return utils.ErrSilent } 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, Run: func(cmd *cobra.Command, args []string) { fmt.Println("vt:", cmd.VersionTemplate()) if bi, ok := debug.ReadBuildInfo(); ok { fmt.Printf("\nBuild info: %+v\n", bi) } }, }
Functions ¶
Types ¶
type ConfigTemplateData ¶
type ConfigTemplateData struct {
Location string
}
Click to show internal directories.
Click to hide internal directories.