hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

helpers.go (2146B)

    1 // Copyright 2018 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 // Package commands defines and implements command-line commands and flags
   15 // used by Hugo. Commands and flags are implemented using Cobra.
   16 package commands
   17 
   18 import (
   19 	"fmt"
   20 	"regexp"
   21 
   22 	"github.com/gohugoio/hugo/config"
   23 	"github.com/spf13/cobra"
   24 )
   25 
   26 const (
   27 	ansiEsc    = "\u001B"
   28 	clearLine  = "\r\033[K"
   29 	hideCursor = ansiEsc + "[?25l"
   30 	showCursor = ansiEsc + "[?25h"
   31 )
   32 
   33 type flagsToConfigHandler interface {
   34 	flagsToConfig(cfg config.Provider)
   35 }
   36 
   37 type cmder interface {
   38 	flagsToConfigHandler
   39 	getCommand() *cobra.Command
   40 }
   41 
   42 // commandError is an error used to signal different error situations in command handling.
   43 type commandError struct {
   44 	s         string
   45 	userError bool
   46 }
   47 
   48 func (c commandError) Error() string {
   49 	return c.s
   50 }
   51 
   52 func (c commandError) isUserError() bool {
   53 	return c.userError
   54 }
   55 
   56 func newUserError(a ...any) commandError {
   57 	return commandError{s: fmt.Sprintln(a...), userError: true}
   58 }
   59 
   60 func newSystemError(a ...any) commandError {
   61 	return commandError{s: fmt.Sprintln(a...), userError: false}
   62 }
   63 
   64 func newSystemErrorF(format string, a ...any) commandError {
   65 	return commandError{s: fmt.Sprintf(format, a...), userError: false}
   66 }
   67 
   68 // Catch some of the obvious user errors from Cobra.
   69 // We don't want to show the usage message for every error.
   70 // The below may be to generic. Time will show.
   71 var userErrorRegexp = regexp.MustCompile("unknown flag")
   72 
   73 func isUserError(err error) bool {
   74 	if cErr, ok := err.(commandError); ok && cErr.isUserError() {
   75 		return true
   76 	}
   77 
   78 	return userErrorRegexp.MatchString(err.Error())
   79 }