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

     1  package set
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/gopinath-langote/1build/cmd/config"
     8  	"github.com/gopinath-langote/1build/cmd/utils"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // Cmd cobra command for setting one build configuration command
    14  var Cmd = &cobra.Command{
    15  	Use:   "set",
    16  	Short: "Set new or update the existing command in the current project configuration",
    17  	Long: `Set new or update the existing command in the current project configuration
    18  
    19  - command name is a one word: without spaces, dashes and underscores are allowed
    20  - command should be double-quoted if it the command contain spaces
    21  
    22  For example:
    23  
    24    1build set test "npm run test"
    25    1build set npm-test "npm run test"
    26    1build set npm_test "npm run test"
    27  
    28  This will update the current project configuration file.`,
    29  	Args: cobra.ExactArgs(2),
    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  		commandName := args[0]
    38  		matched, _ := regexp.MatchString(`^[a-zA-Z0-9\-_]+$`, commandName)
    39  
    40  		if !matched {
    41  			fmt.Println("1build set: '" + commandName + "' is not a valid command name. See '1build set --help'.")
    42  			utils.ExitError()
    43  		}
    44  	},
    45  	Run: func(cmd *cobra.Command, args []string) {
    46  		commandName := args[0]
    47  		commandValue := args[1]
    48  
    49  		configuration, err := buildAndSetCommand(commandName, commandValue)
    50  
    51  		if err != nil {
    52  			fmt.Println(err)
    53  			return
    54  		}
    55  
    56  		_ = config.WriteConfigFile(configuration)
    57  	},
    58  }
    59  
    60  func buildAndSetCommand(name string, value string) (config.OneBuildConfiguration, error) {
    61  	configuration, err := config.LoadOneBuildConfiguration()
    62  	if err != nil {
    63  		return config.OneBuildConfiguration{}, err
    64  	}
    65  
    66  	switch name {
    67  	case config.BeforeCommand:
    68  		return setBefore(configuration, value), nil
    69  	case config.AfterCommand:
    70  		return setAfter(configuration, value), nil
    71  	default:
    72  		return setCommand(configuration, name, value), nil
    73  	}
    74  }
    75  
    76  func setBefore(configuration config.OneBuildConfiguration, value string) config.OneBuildConfiguration {
    77  	configuration.Before = value
    78  	return configuration
    79  }
    80  
    81  func setAfter(configuration config.OneBuildConfiguration, value string) config.OneBuildConfiguration {
    82  	configuration.After = value
    83  	return configuration
    84  }
    85  
    86  func setCommand(configuration config.OneBuildConfiguration, name string, value string) config.OneBuildConfiguration {
    87  	command := map[string]string{}
    88  	command[name] = value
    89  
    90  	index := IndexOfCommandIfPresent(configuration, name)
    91  	if index == -1 {
    92  		strings := append(configuration.Commands, command)
    93  		configuration.Commands = strings
    94  	} else {
    95  		configuration.Commands[index] = command
    96  	}
    97  	return configuration
    98  }
    99  
   100  // IndexOfCommandIfPresent returns index in configuration for command if exists
   101  func IndexOfCommandIfPresent(configuration config.OneBuildConfiguration, commandName string) int {
   102  	return utils.SliceIndex(len(configuration.Commands), func(i int) bool {
   103  		i2 := configuration.Commands[i]
   104  		for k := range i2 {
   105  			if k == commandName {
   106  				return true
   107  			}
   108  		}
   109  		return false
   110  	})
   111  }