github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/cmd/check.go (about) 1 /* 2 Copyright © 2022 NAME HERE <EMAIL ADDRESS> 3 */ 4 package cmd 5 6 import ( 7 "github.com/everdrone/grab/internal/config" 8 "github.com/everdrone/grab/internal/utils" 9 10 "github.com/hashicorp/hcl/v2" 11 "github.com/spf13/cobra" 12 ) 13 14 var CheckCmd = &cobra.Command{ 15 Use: "check", 16 Short: "Validate the configuration file", 17 PreRunE: func(cmd *cobra.Command, args []string) error { 18 return utils.Getwd() 19 }, 20 RunE: func(cmd *cobra.Command, args []string) error { 21 quiet, _ := cmd.Flags().GetBool("quiet") 22 configPath, _ := cmd.Flags().GetString("config") 23 24 if configPath == "" { 25 configPath = utils.Wd 26 27 resolved, err := config.Resolve("grab.hcl", configPath) 28 if err != nil { 29 30 utils.PrintDiag(cmd.ErrOrStderr(), &hcl.Diagnostic{ 31 Severity: hcl.DiagError, 32 Summary: "could not resolve config file", 33 Detail: err.Error(), 34 }) 35 return utils.ErrSilent 36 } 37 38 configPath = resolved 39 } 40 41 // parse config 42 fc, err := utils.Io.ReadFile(utils.Fs, configPath) 43 if err != nil { 44 utils.PrintDiag(cmd.ErrOrStderr(), &hcl.Diagnostic{ 45 Severity: hcl.DiagError, 46 Summary: "could not read config file", 47 Detail: err.Error(), 48 }) 49 return utils.ErrSilent 50 } 51 52 _, _, _, diags := config.Parse(fc, configPath) 53 if diags.HasErrors() { 54 for _, diag := range diags { 55 utils.PrintDiag(cmd.ErrOrStderr(), diag) 56 } 57 return utils.ErrSilent 58 } 59 60 if !quiet { 61 cmd.Println("ok") 62 } 63 return nil 64 }, 65 } 66 67 func init() { 68 ConfigCmd.AddCommand(CheckCmd) 69 70 CheckCmd.Flags().BoolP("quiet", "q", false, "do not emit any output") 71 CheckCmd.Flags().StringP("config", "c", "", "the path of the config file to use") 72 }