11package cmd
22
33import (
4+ "bufio"
45 "fmt"
6+ "io"
57 "jarvis/internal/pkg/ansible"
68 "jarvis/internal/pkg/command"
79 "jarvis/internal/pkg/environment"
@@ -11,39 +13,62 @@ import (
1113 "github.com/spf13/viper"
1214)
1315
16+ //Ansible flags
17+ var (
18+ HideDiff bool
19+ BecomeSudo bool
20+ )
21+
22+ //Playbook flags
1423var (
15- HideDiff bool
1624 CheckModeDeactivated bool
17- CheckModeEnabled bool
18- BecomeSudo bool
1925 JoinInventories bool
2026)
2127
28+ //Run flags
29+ var (
30+ CheckModeEnabled bool
31+ ModuleName string
32+ ModuleArg string
33+ HostPattern string
34+ )
35+
36+ //Inventory flags
2237var (
23- ModuleName string
24- ModuleArg string
25- HostPattern string
38+ ListGroup bool
39+ HostGroupName string
40+ WithParent bool
2641)
2742
2843func init () {
2944 rootCmd .AddCommand (ansibleCmd )
30- ansibleCmd . AddCommand ( playCmd )
45+ //AnsibleCmd
3146 ansibleCmd .PersistentFlags ().BoolVar (& HideDiff , "nodiff" , false ,
3247 "Hide diff" )
3348 ansibleCmd .PersistentFlags ().BoolVarP (& BecomeSudo , "become" , "b" , false ,
3449 "Become sudo" )
50+
51+ //PlaybookCmd
3552 playCmd .Flags ().BoolVar (& CheckModeDeactivated , "nocheck" , false ,
3653 "Deactivate check mode" )
3754 playCmd .Flags ().BoolVar (& JoinInventories , "join-inventories" , false ,
3855 "Join platforms inventories" )
56+ ansibleCmd .AddCommand (playCmd )
3957
40- ansibleCmd . AddCommand ( runCmd )
58+ //RunCmd
4159 runCmd .Flags ().StringVarP (& ModuleName , "module" , "m" , "shell" , "Ansible module name" )
4260 runCmd .Flags ().StringVarP (& ModuleArg , "args" , "a" , "" , "Ansible module arg" )
4361 runCmd .Flags ().StringVarP (& HostPattern , "target" , "t" , "" , "Ansible host-pattern" )
4462 runCmd .Flags ().BoolVar (& CheckModeEnabled , "check" , false ,
4563 "Enable check mode" )
4664 runCmd .MarkFlagRequired ("target" )
65+ ansibleCmd .AddCommand (runCmd )
66+
67+ //InventoryCmd
68+ inventoryCmd .Flags ().BoolVarP (& WithParent , "with-parent" , "W" , false , "Query with parent group" )
69+ inventoryCmd .Flags ().BoolVarP (& ListGroup , "group" , "G" , false , "List group name (mutually exclusive with --host)" )
70+ inventoryCmd .Flags ().StringVarP (& HostGroupName , "host" , "H" , "" , "List host by group name (mutually exclusive with --group)" )
71+ ansibleCmd .AddCommand (inventoryCmd )
4772}
4873
4974//usage: jarvis ansible
@@ -148,3 +173,85 @@ var playCmd = &cobra.Command{
148173 return nil
149174 },
150175}
176+
177+ //usage: jarvis ansible list
178+ //returns:
179+ // --group(bool) >> list groups name
180+ // --hosts(string) >> hosts by group
181+ var inventoryCmd = & cobra.Command {
182+ Use : "list" ,
183+ Short : "Query inventory" ,
184+ PreRunE : func (cmd * cobra.Command , args []string ) error {
185+ //checking exclusivity
186+ if ListGroup && HostGroupName != "" {
187+ return fmt .Errorf ("--group and --hosts are mutually exclusive" )
188+ }
189+
190+ return nil
191+ },
192+ RunE : func (cmd * cobra.Command , args []string ) error {
193+ envsPath := viper .GetString ("environments.path" )
194+
195+ allInventories , err := environment .GetFullPathInventoriesFromEnvironments (envsPath , * environments )
196+ if err != nil {
197+ return err
198+ }
199+
200+ var invReaders []io.Reader
201+ for _ , invs := range allInventories {
202+ for _ , inv := range invs {
203+ if ! fileExists (inv ) {
204+ return fmt .Errorf ("the %v file does not exist" , inv )
205+ }
206+ f , err := os .Open (inv )
207+ if err != nil {
208+ return err
209+ }
210+ invReaders = append (invReaders , bufio .NewReader (f ))
211+
212+ if isDebug {
213+ fmt .Println (inv )
214+ }
215+ }
216+ }
217+ //to concatenate all files to one reader
218+ r := io .MultiReader (invReaders ... )
219+ manipulator := ansible .InitInventoryManipulator (r )
220+
221+ if ListGroup {
222+ groups , err := manipulator .GetGroupsName (WithParent )
223+ if err != nil {
224+ return err
225+ }
226+ //I know it is a bit odd but Jarvis is learning
227+ //be nice with him :)
228+ for _ , g := range groups {
229+ fmt .Println (g )
230+ }
231+
232+ return nil
233+ }
234+
235+ if HostGroupName != "" {
236+ hosts , err := manipulator .GetHostsByGroupName (HostGroupName )
237+ if err != nil {
238+ return err
239+ }
240+ for _ , h := range hosts {
241+ fmt .Println (h )
242+ }
243+
244+ return nil
245+ }
246+
247+ return nil
248+ },
249+ }
250+
251+ func fileExists (filename string ) bool {
252+ info , err := os .Stat (filename )
253+ if os .IsNotExist (err ) {
254+ return false
255+ }
256+ return ! info .IsDir ()
257+ }
0 commit comments