Skip to content

Commit b3f80bd

Browse files
Merge pull request #23 from ShotaKitazawa/fix/22
fixed: required flag does not work well
2 parents b9fed77 + 6678272 commit b3f80bd

File tree

9 files changed

+65
-4
lines changed

9 files changed

+65
-4
lines changed

cmd/afterbench.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
var afterbenchCmd = &cobra.Command{
1111
Use: "afterbench",
1212
Short: "Collect and parse profile data & Send to Slack",
13+
PreRunE: func(cmd *cobra.Command, args []string) error {
14+
return checkRequiredFlags(cmd.Flags())
15+
},
1316
RunE: func(c *cobra.Command, args []string) error {
1417
executed = true
1518
conf := cmd.ConfigAfterBench{

cmd/deploy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
var deployCmd = &cobra.Command{
1010
Use: "deploy",
1111
Short: "Deploy files from specified revision",
12+
PreRunE: func(cmd *cobra.Command, args []string) error {
13+
return checkRequiredFlags(cmd.Flags())
14+
},
1215
RunE: func(c *cobra.Command, args []string) error {
1316
executed = true
1417
conf := cmd.ConfigDeploy{
@@ -37,7 +40,7 @@ func init() {
3740
"branch-name, tag-name, or commit-hash of deployed from Git remote-repo")
3841
deployCmd.PersistentFlags().BoolVarP(&deployForce, "force", "f", false,
3942
"force deploy")
40-
deployCmd.PersistentFlags().StringVarP(&deploySlackToken, "slack-token", "t", getenvDefault("SLACK_TOKEN", ""),
43+
deployCmd.PersistentFlags().StringVarP(&deploySlackToken, "slack-token", "t", getenvDefault("slack-token", ""),
4144
"slack token of workspace where deployment notification will be sent")
42-
_ = initCmd.MarkPersistentFlagRequired("slack-token")
45+
setRequired(initCmd, "slack-token")
4346
}

cmd/import.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
var importCmd = &cobra.Command{
1111
Use: "import",
1212
Short: "Import some files from hosts[].deploy.files[].target",
13+
PreRunE: func(cmd *cobra.Command, args []string) error {
14+
return checkRequiredFlags(cmd.Flags())
15+
},
1316
RunE: func(c *cobra.Command, args []string) error {
1417
executed = true
1518
conf := cmd.ConfigImport{

cmd/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
var initCmd = &cobra.Command{
1111
Use: "init",
1212
Short: "Initialize local repository",
13+
PreRunE: func(cmd *cobra.Command, args []string) error {
14+
return checkRequiredFlags(cmd.Flags())
15+
},
1316
RunE: func(c *cobra.Command, args []string) error {
1417
executed = true
1518
conf := cmd.ConfigInit{
@@ -41,5 +44,5 @@ func init() {
4144
"email of GitHub Account")
4245
initCmd.PersistentFlags().StringVarP(&gitRemoteUrl, "remote-url", "r", getenvDefault("REMOTE_URL", ""),
4346
"URL of remote repository (requirement)")
44-
_ = initCmd.MarkPersistentFlagRequired("remote-url")
47+
setRequired(initCmd, "remote-url")
4548
}

cmd/profiling.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
var profilingCmd = &cobra.Command{
1111
Use: "profiling",
1212
Short: "Execute profiling command and wait to finish synchronously.",
13+
PreRunE: func(cmd *cobra.Command, args []string) error {
14+
return checkRequiredFlags(cmd.Flags())
15+
},
1316
RunE: func(c *cobra.Command, args []string) error {
1417
executed = true
1518
conf := cmd.ConfigProfiling{

cmd/push.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
var pushCmd = &cobra.Command{
1010
Use: "push",
1111
Short: "Push local-repo to origin/${MAIN_BRANCH}",
12+
PreRunE: func(cmd *cobra.Command, args []string) error {
13+
return checkRequiredFlags(cmd.Flags())
14+
},
1215
RunE: func(c *cobra.Command, args []string) error {
1316
executed = true
1417
conf := cmd.ConfigPush{

cmd/root.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package cmd
22

33
import (
4+
"errors"
45
"flag"
56
"fmt"
7+
"log"
68
"os"
79
"path/filepath"
10+
"strings"
811

912
"github.com/spf13/cobra"
13+
"github.com/spf13/pflag"
1014
)
1115

1216
// rootCmd represents the base command when called without any subcommands
@@ -46,10 +50,43 @@ func init() {
4650
"local repository's path managed by isucontinuous")
4751
}
4852

49-
func getenvDefault(key, defaultV string) string {
53+
// utils
54+
55+
func getenvDefault(flagName, defaultV string) string {
56+
key := strings.ToUpper(strings.ReplaceAll(flagName, "-", "_"))
5057
result := os.Getenv(key)
5158
if result == "" {
5259
return defaultV
5360
}
5461
return result
5562
}
63+
64+
const requiredFlagAnnotation = "isucontinuous/required"
65+
66+
func setRequired(cmd *cobra.Command, flagNames ...string) {
67+
for _, flagName := range flagNames {
68+
if err := cmd.PersistentFlags().SetAnnotation(flagName, requiredFlagAnnotation, []string{"true"}); err != nil {
69+
log.Fatal(err)
70+
}
71+
}
72+
}
73+
74+
func checkRequiredFlags(flags *pflag.FlagSet) error {
75+
requiredError := false
76+
flagName := ""
77+
flags.VisitAll(func(flag *pflag.Flag) {
78+
requiredAnnotation := flag.Annotations[requiredFlagAnnotation]
79+
if len(requiredAnnotation) == 0 {
80+
return
81+
}
82+
flagRequired := requiredAnnotation[0] == "true"
83+
if flagRequired && !flag.Changed && getenvDefault(flag.Name, "") == "" {
84+
requiredError = true
85+
flagName = flag.Name
86+
}
87+
})
88+
if requiredError {
89+
return errors.New("Required flag `" + flagName + "` has not been set")
90+
}
91+
return nil
92+
}

cmd/setup.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
var setupCmd = &cobra.Command{
1111
Use: "setup",
1212
Short: "Install some softwares",
13+
PreRunE: func(cmd *cobra.Command, args []string) error {
14+
return checkRequiredFlags(cmd.Flags())
15+
},
1316
RunE: func(c *cobra.Command, args []string) error {
1417
executed = true
1518
conf := cmd.ConfigSetup{

cmd/sync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
var syncCmd = &cobra.Command{
1111
Use: "sync",
1212
Short: "synchronize local-repo with remote-repo",
13+
PreRunE: func(cmd *cobra.Command, args []string) error {
14+
return checkRequiredFlags(cmd.Flags())
15+
},
1316
RunE: func(c *cobra.Command, args []string) error {
1417
executed = true
1518
conf := cmd.ConfigSync{

0 commit comments

Comments
 (0)