github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/cmd/utils/command_chain.go (about) 1 /* 2 * Copyright 2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package utils 18 19 import ( 20 "github.com/spf13/cobra" 21 ) 22 23 /* 24 * Runs a chain of commands 25 */ 26 func CommandChain(commands ... *cobra.Command) *cobra.Command { 27 28 run := func(cmd *cobra.Command, args []string) { 29 for _, command := range commands { 30 if command.Run != nil { 31 command.Run(cmd, args) 32 } 33 } 34 } 35 36 /* 37 * Composite command using RunE to fail fast. SilenceUsage enabled to supress usage message following 38 * run time errors. If it gets this far, the usage was likely correct. 39 */ 40 runE := func(cmd *cobra.Command, args []string) error { 41 for _, command := range commands { 42 if command.RunE != nil { 43 err := command.RunE(command, args) 44 if err != nil { 45 cmd.SilenceUsage = true 46 return err 47 } 48 } 49 } 50 return nil 51 } 52 53 preRun := func(cmd *cobra.Command, args []string) { 54 for _, command := range commands { 55 if command.PreRun != nil { 56 command.PreRun(command, args) 57 } 58 } 59 } 60 61 preRunE := func(cmd *cobra.Command, args []string) error { 62 for _, command := range commands { 63 if command.PreRunE != nil { 64 err := command.PreRunE(command, args) 65 if err != nil { 66 return err 67 } 68 } 69 } 70 return nil 71 } 72 73 postRun := func(cmd *cobra.Command, args []string) { 74 for _, command := range commands { 75 if command.PostRun != nil { 76 command.PostRun(command, args) 77 } 78 } 79 } 80 81 postRunE := func(cmd *cobra.Command, args []string) error { 82 for _, command := range commands { 83 if command.PostRunE != nil { 84 err := command.PostRunE(command, args) 85 if err != nil { 86 return err 87 } 88 } 89 } 90 return nil 91 } 92 93 persistentPreRun := func(cmd *cobra.Command, args []string) { 94 95 for _, command := range commands { 96 for ; command.Parent() != nil && command.Parent().PersistentPreRun != nil; { 97 command = command.Parent() 98 } 99 if command.PersistentPreRun != nil { 100 command.PersistentPreRun(command, args) 101 } 102 } 103 } 104 105 persistentPreRunE := func(cmd *cobra.Command, args []string) error { 106 for _, command := range commands { 107 for ; command.Parent() != nil && command.Parent().PersistentPreRunE != nil; { 108 command = command.Parent() 109 } 110 if command.PersistentPreRunE != nil { 111 err := command.PersistentPreRunE(command, args) 112 if err != nil { 113 return err 114 } 115 } 116 } 117 return nil 118 } 119 120 persistentPostRun := func(cmd *cobra.Command, args []string) { 121 122 for _, command := range commands { 123 for ; command.Root() != nil && command.Parent().PersistentPostRun != nil; { 124 command = command.Parent() 125 } 126 if command.PersistentPostRun != nil { 127 command.PersistentPostRun(command, args) 128 } 129 } 130 } 131 132 persistentPostRunE := func(cmd *cobra.Command, args []string) error { 133 for _, command := range commands { 134 for ; command.Parent() != nil && command.Parent().PersistentPostRunE != nil; { 135 command = command.Root() 136 } 137 if command.PersistentPostRunE != nil { 138 err := command.PersistentPostRunE(command, args) 139 if err != nil { 140 return err 141 } 142 } 143 } 144 return nil 145 } 146 147 var chain = &cobra.Command{ 148 Run: run, 149 RunE: runE, 150 PreRun: preRun, 151 PreRunE: preRunE, 152 PostRun: postRun, 153 PostRunE: postRunE, 154 PersistentPreRun: persistentPreRun, 155 PersistentPreRunE: persistentPreRunE, 156 PersistentPostRun: persistentPostRun, 157 PersistentPostRunE: persistentPostRunE, 158 } 159 160 return chain 161 } 162