github.com/gopinath-langote/1build@v1.7.0/cmd/unset/unset.go (about)

     1  package unset
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/gopinath-langote/1build/cmd/set"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/gopinath-langote/1build/cmd/config"
    10  	"github.com/gopinath-langote/1build/cmd/utils"
    11  
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  // Cmd cobra command for unsetting one build configuration command
    16  var Cmd = &cobra.Command{
    17  	Use:   "unset",
    18  	Short: "Remove one or more existing command(s) in the current project configuration",
    19  	Long: `Remove one or more existing command(s) in the current project configuration
    20  
    21  - command name is a one word: without spaces, dashes and underscores are allowed
    22  
    23  For example:
    24  
    25    1build unset test
    26    1build unset build lint other-command
    27  
    28  This will update the current project configuration file.`,
    29  	Args: cobra.MinimumNArgs(1),
    30  	PreRun: func(cmd *cobra.Command, args []string) {
    31  		_, err := config.LoadOneBuildConfiguration()
    32  		if err != nil {
    33  			fmt.Println(err)
    34  			utils.ExitError()
    35  		}
    36  
    37  		validNameRegex := regexp.MustCompile(`^[a-zA-Z0-9\-_]+$`)
    38  		for _, commandName := range args {
    39  			matched := validNameRegex.MatchString(commandName)
    40  
    41  			if !matched {
    42  				fmt.Println("1build unset: '" + commandName + "' is not a valid command name. See '1build unset --help'.")
    43  				utils.ExitError()
    44  			}
    45  		}
    46  	},
    47  	Run: func(cmd *cobra.Command, args []string) {
    48  		configuration, err := config.LoadOneBuildConfiguration()
    49  		if err != nil {
    50  			fmt.Println(err)
    51  			return
    52  		}
    53  
    54  		var commandsNotFound []string
    55  		var configIsChanged bool
    56  
    57  		for _, commandName := range args {
    58  			index := findIndex(configuration, commandName)
    59  
    60  			if index == -1 {
    61  				commandsNotFound = append(commandsNotFound, commandName)
    62  			} else {
    63  				configuration = removeCommand(configuration, commandName, index)
    64  				configIsChanged = true
    65  			}
    66  		}
    67  
    68  		if len(commandsNotFound) != 0 {
    69  			errorMsg := "\nFollowing command(s) not found: " + strings.Join(commandsNotFound, ", ")
    70  			utils.CPrintln(errorMsg, utils.Style{Color: utils.RED, Bold: true})
    71  		}
    72  
    73  		if configIsChanged {
    74  			_ = config.WriteConfigFile(configuration)
    75  		}
    76  	},
    77  }
    78  
    79  func removeCommandByIndex(configuration config.OneBuildConfiguration, index int) (ret []map[string]string) {
    80  	for i, command := range configuration.Commands {
    81  		if i != index {
    82  			ret = append(ret, command)
    83  		}
    84  	}
    85  	return
    86  }
    87  
    88  func findIndex(configuration config.OneBuildConfiguration, name string) int {
    89  	switch name {
    90  	case config.BeforeCommand, config.AfterCommand:
    91  		return callbackExistence(configuration, name)
    92  	default:
    93  		return set.IndexOfCommandIfPresent(configuration, name)
    94  	}
    95  }
    96  
    97  func callbackExistence(configuration config.OneBuildConfiguration, name string) int {
    98  	switch {
    99  	case name == config.BeforeCommand && configuration.Before == "":
   100  		return -1
   101  	case name == config.AfterCommand && configuration.After == "":
   102  		return -1
   103  	default:
   104  		return -2
   105  	}
   106  }
   107  
   108  func removeCommand(configuration config.OneBuildConfiguration, name string, index int) config.OneBuildConfiguration {
   109  	switch name {
   110  	case config.BeforeCommand:
   111  		configuration.Before = ""
   112  	case config.AfterCommand:
   113  		configuration.After = ""
   114  	default:
   115  		configuration.Commands = removeCommandByIndex(configuration, index)
   116  	}
   117  	return configuration
   118  }
   119