github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/internal/instance/setup.go (about)

     1  package instance
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/everdrone/grab/internal/config"
     7  	"github.com/everdrone/grab/internal/utils"
     8  	"github.com/mitchellh/go-homedir"
     9  	"github.com/rs/zerolog"
    10  	"github.com/rs/zerolog/log"
    11  
    12  	"github.com/hashicorp/hcl/v2"
    13  )
    14  
    15  func (s *Grab) ParseFlags() {
    16  	flags := &FlagsState{}
    17  
    18  	flags.Force, _ = s.Command.Flags().GetBool("force")
    19  	flags.Quiet, _ = s.Command.Flags().GetBool("quiet")
    20  	flags.Strict, _ = s.Command.Flags().GetBool("strict")
    21  	flags.DryRun, _ = s.Command.Flags().GetBool("dry-run")
    22  	flags.Progress, _ = s.Command.Flags().GetBool("progress")
    23  	flags.Verbosity, _ = s.Command.Flags().GetCount("verbose")
    24  	flags.ConfigPath, _ = s.Command.Flags().GetString("config")
    25  
    26  	// if both quiet and verbose are set, quiet wins
    27  	if flags.Quiet {
    28  		flags.Verbosity = 0
    29  	} else {
    30  		// make verbosity 1 indexed (just add one)
    31  		flags.Verbosity++
    32  	}
    33  
    34  	switch flags.Verbosity {
    35  	case 0:
    36  		zerolog.SetGlobalLevel(zerolog.ErrorLevel)
    37  	case 1:
    38  		zerolog.SetGlobalLevel(zerolog.WarnLevel)
    39  	case 2:
    40  		zerolog.SetGlobalLevel(zerolog.InfoLevel)
    41  	case 3:
    42  		zerolog.SetGlobalLevel(zerolog.DebugLevel)
    43  	case 4:
    44  		zerolog.SetGlobalLevel(zerolog.TraceLevel)
    45  	default:
    46  		zerolog.SetGlobalLevel(zerolog.TraceLevel)
    47  	}
    48  
    49  	s.Flags = flags
    50  }
    51  
    52  func (s *Grab) ParseConfig() *hcl.Diagnostics {
    53  	if s.Flags.ConfigPath == "" {
    54  		log.Trace().Msg("no config file specified, resolving")
    55  
    56  		resolved, err := config.Resolve("grab.hcl", utils.Wd)
    57  		if err != nil {
    58  			return &hcl.Diagnostics{{
    59  				Severity: hcl.DiagError,
    60  				Summary:  "Could not resolve config file",
    61  				Detail:   err.Error(),
    62  			}}
    63  		}
    64  
    65  		log.Debug().Str("path", resolved).Msg("using config file")
    66  
    67  		s.Flags.ConfigPath = resolved
    68  	}
    69  
    70  	log.Trace().Str("path", s.Flags.ConfigPath).Msg("parsing config file")
    71  
    72  	// read file contents of config file
    73  	fc, err := utils.Io.ReadFile(utils.Fs, s.Flags.ConfigPath)
    74  	if err != nil {
    75  		return &hcl.Diagnostics{{
    76  			Severity: hcl.DiagError,
    77  			Summary:  "Could not read config file",
    78  			Detail:   err.Error(),
    79  		}}
    80  	}
    81  
    82  	// parse config and get regexCache
    83  	config, _, regexCache, diags := config.Parse(fc, s.Flags.ConfigPath)
    84  	if diags.HasErrors() {
    85  		return &diags
    86  	}
    87  
    88  	s.Config = config
    89  	s.RegexCache = regexCache
    90  
    91  	// get global location
    92  	if !filepath.IsAbs(s.Config.Global.Location) {
    93  		expanded, err := homedir.Expand(s.Config.Global.Location)
    94  		if err != nil {
    95  			return &hcl.Diagnostics{{
    96  				Severity: hcl.DiagError,
    97  				Summary:  "Could not expand home directory",
    98  				Detail:   err.Error(),
    99  			}}
   100  		}
   101  
   102  		if filepath.IsAbs(expanded) {
   103  			s.Config.Global.Location = expanded
   104  		} else {
   105  			s.Config.Global.Location = filepath.Join(utils.Wd, s.Config.Global.Location)
   106  		}
   107  	}
   108  
   109  	return &hcl.Diagnostics{}
   110  }
   111  
   112  func (s *Grab) ParseURLs(args []string) *hcl.Diagnostics {
   113  	log.Trace().Msg("parsing arguments")
   114  
   115  	args = utils.Unique(args)
   116  
   117  	urls, diags := utils.GetURLsFromArgs(args)
   118  	if diags.HasErrors() {
   119  		return &diags
   120  	}
   121  
   122  	s.URLs = utils.Unique(urls)
   123  
   124  	log.Trace().Strs("urls", s.URLs).Msgf("found %d %s", len(s.URLs), utils.Plural(len(s.URLs), "url", "urls"))
   125  
   126  	return &hcl.Diagnostics{}
   127  }