Skip to content

Commit

Permalink
Check more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 9, 2020
1 parent b3dfbea commit de3d05b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 28 deletions.
4 changes: 1 addition & 3 deletions cmd/chattr.go
Expand Up @@ -47,9 +47,7 @@ func init() {
for _, attribute := range attributes {
words = append(words, attribute, "-"+attribute, "+"+attribute, "no"+attribute)
}
if err := chattrCmd.MarkZshCompPositionalArgumentWords(1, words...); err != nil {
panic(err)
}
panicOnError(chattrCmd.MarkZshCompPositionalArgumentWords(1, words...))
markRemainingZshCompPositionalArgumentsAsFiles(chattrCmd, 2)
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/config.go
Expand Up @@ -542,6 +542,12 @@ func isWellKnownAbbreviation(word string) bool {
return ok
}

func panicOnError(err error) {
if err != nil {
panic(err)
}
}

func printErrorAndExit(err error) {
fmt.Printf("chezmoi: %v\n", err)
os.Exit(1)
Expand Down
4 changes: 1 addition & 3 deletions cmd/import.go
Expand Up @@ -38,9 +38,7 @@ func init() {
persistentFlags.IntVar(&config._import.importTAROptions.StripComponents, "strip-components", 0, "strip components")
persistentFlags.BoolVarP(&config._import.removeDestination, "remove-destination", "r", false, "remove destination before import")

if err := _importCmd.MarkZshCompPositionalArgumentFile(1, "*.tar", "*.tar.bz2", "*.tar.gz", "*.tgz"); err != nil {
panic(err)
}
panicOnError(_importCmd.MarkZshCompPositionalArgumentFile(1, "*.tar", "*.tar.bz2", "*.tar.gz", "*.tgz"))
}

func (c *Config) runImportCmd(cmd *cobra.Command, args []string) error {
Expand Down
4 changes: 1 addition & 3 deletions cmd/init.go
Expand Up @@ -173,8 +173,6 @@ func (c *Config) findConfigTemplate() (string, string, string, error) {
func (c *Config) promptString(field string) string {
fmt.Fprintf(c.Stdout(), "%s? ", field)
value, err := bufio.NewReader(c.Stdin()).ReadString('\n')
if err != nil {
panic(err)
}
panicOnError(err)
return strings.TrimSuffix(value, "\n")
}
20 changes: 9 additions & 11 deletions cmd/root.go
Expand Up @@ -80,28 +80,28 @@ func init() {
persistentFlags.StringVarP(&config.configFile, "config", "c", getDefaultConfigFile(config.bds), "config file")

persistentFlags.BoolVarP(&config.DryRun, "dry-run", "n", false, "dry run")
_ = viper.BindPFlag("dry-run", persistentFlags.Lookup("dry-run"))
panicOnError(viper.BindPFlag("dry-run", persistentFlags.Lookup("dry-run")))

persistentFlags.BoolVar(&config.Follow, "follow", false, "follow symlinks")
_ = viper.BindPFlag("follow", persistentFlags.Lookup("follow"))
panicOnError(viper.BindPFlag("follow", persistentFlags.Lookup("follow")))

persistentFlags.BoolVar(&config.Remove, "remove", false, "remove targets")
_ = viper.BindPFlag("remove", persistentFlags.Lookup("remove"))
panicOnError(viper.BindPFlag("remove", persistentFlags.Lookup("remove")))

persistentFlags.StringVarP(&config.SourceDir, "source", "S", getDefaultSourceDir(config.bds), "source directory")
_ = viper.BindPFlag("source", persistentFlags.Lookup("source"))
panicOnError(viper.BindPFlag("source", persistentFlags.Lookup("source")))

persistentFlags.StringVarP(&config.DestDir, "destination", "D", homeDir, "destination directory")
_ = viper.BindPFlag("destination", persistentFlags.Lookup("destination"))
panicOnError(viper.BindPFlag("destination", persistentFlags.Lookup("destination")))

persistentFlags.BoolVarP(&config.Verbose, "verbose", "v", false, "verbose")
_ = viper.BindPFlag("verbose", persistentFlags.Lookup("verbose"))
panicOnError(viper.BindPFlag("verbose", persistentFlags.Lookup("verbose")))

persistentFlags.StringVar(&config.Color, "color", "auto", "colorize diffs")
_ = viper.BindPFlag("color", persistentFlags.Lookup("color"))
panicOnError(viper.BindPFlag("color", persistentFlags.Lookup("color")))

persistentFlags.BoolVar(&config.Debug, "debug", false, "write debug logs")
_ = viper.BindPFlag("debug", persistentFlags.Lookup("debug"))
panicOnError(viper.BindPFlag("debug", persistentFlags.Lookup("debug")))

cobra.OnInitialize(func() {
_, err := os.Stat(config.configFile)
Expand Down Expand Up @@ -197,9 +197,7 @@ func markRemainingZshCompPositionalArgumentsAsFiles(cmd *cobra.Command, from int
// should be enough for everybody.
// FIXME mark all remaining positional arguments as files
for i := 0; i < 8; i++ {
if err := cmd.MarkZshCompPositionalArgumentFile(from + i); err != nil {
panic(err)
}
panicOnError(cmd.MarkZshCompPositionalArgumentFile(from + i))
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/secretkeyring.go
Expand Up @@ -32,10 +32,10 @@ func init() {
persistentFlags := keyringCmd.PersistentFlags()

persistentFlags.StringVar(&config.keyring.service, "service", "", "service")
_ = keyringCmd.MarkPersistentFlagRequired("service")
panicOnError(keyringCmd.MarkPersistentFlagRequired("service"))

persistentFlags.StringVar(&config.keyring.user, "user", "", "user")
_ = keyringCmd.MarkPersistentFlagRequired("user")
panicOnError(keyringCmd.MarkPersistentFlagRequired("user"))

config.addTemplateFunc("keyring", config.keyringFunc)
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/secretlastpass.go
Expand Up @@ -65,17 +65,13 @@ func (c *Config) lastpassOutput(args ...string) ([]byte, error) {

func (c *Config) lastpassFunc(id string) interface{} {
c.Lastpass.versionCheckOnce.Do(func() {
if err := c.lastpassVersionCheck(); err != nil {
panic(err)
}
panicOnError(c.lastpassVersionCheck())
})
if data, ok := lastPassCache[id]; ok {
return data
}
output, err := c.lastpassOutput("show", "--json", id)
if err != nil {
panic(err)
}
panicOnError(err)
var data []map[string]interface{}
if err := json.Unmarshal(output, &data); err != nil {
panic(fmt.Errorf("lastpass: parse error: %w\n%q", err, output))
Expand Down

0 comments on commit de3d05b

Please sign in to comment.