cmd

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2022 License: MIT Imports: 13 Imported by: 0

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.AFS.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.`,
	PreRunE: func(cmd *cobra.Command, args []string) error {
		return utils.Getwd()
	},
	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 = utils.Wd
		} else {
			if !filepath.IsAbs(startPath) {
				startPath = utils.Abs(startPath)
			}

			if exists, err := utils.AFS.Exists(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.
To write the file contents to a file or into a directory, use the --output flag.`,
	PreRunE: func(cmd *cobra.Command, args []string) error {
		return utils.Getwd()
	},
	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
		}

		homedir = strings.Replace(homedir, "\\", "\\\\", -1)

		data := &ConfigTemplateData{
			Location: filepath.Join(homedir, "Downloads", "grab"),
		}

		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 = utils.AFS.WriteFile(filepath.Join(utils.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 := utils.Fs.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 = utils.AFS.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)
		}
		fmt.Println(config.BuildArch, config.BuildOS)
	},
}
View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print the version number and exit",
	Run: func(cmd *cobra.Command, args []string) {
		cmd.Printf("%s v%s %s/%s (%s)\n",
			cmd.Root().Name(),
			config.Version,
			config.BuildOS,
			config.BuildArch,
			config.CommitHash[:7])
	},
}

Functions

func Execute

func Execute()

Types

type ConfigTemplateData

type ConfigTemplateData struct {
	Location string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL