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