From bd1c3609a7cff21e8378d21ff5c5418b63ef9138 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 5 Apr 2018 16:21:39 +0200 Subject: [PATCH] Added `hostgroup update` and `usergroup update` features --- shell.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/shell.go b/shell.go index ab77973..d82e1a3 100644 --- a/shell.go +++ b/shell.go @@ -1095,6 +1095,47 @@ GLOBAL OPTIONS: return HostGroupsByIdentifiers(db, c.Args()).Delete(&HostGroup{}).Error }, + }, { + Name: "update", + Usage: "Updates a host group", + ArgsUsage: "HOSTGROUP...", + Flags: []cli.Flag{ + cli.StringFlag{Name: "name", Usage: "Assigns a new name to the host group"}, + cli.StringFlag{Name: "comment", Usage: "Adds a comment"}, + }, + Action: func(c *cli.Context) error { + if c.NArg() < 1 { + return cli.ShowSubcommandHelp(c) + } + + if err := myself.CheckRoles([]string{"admin"}); err != nil { + return err + } + + var hostgroups []HostGroup + if err := HostGroupsByIdentifiers(db, c.Args()).Find(&hostgroups).Error; err != nil { + return err + } + + if len(hostgroups) > 1 && c.String("name") != "" { + return fmt.Errorf("cannot set --name when editing multiple hostgroups at once") + } + + tx := db.Begin() + for _, hostgroup := range hostgroups { + model := tx.Model(&hostgroup) + // simple fields + for _, fieldname := range []string{"name", "comment"} { + if c.String(fieldname) != "" { + if err := model.Update(fieldname, c.String(fieldname)).Error; err != nil { + tx.Rollback() + return err + } + } + } + } + return tx.Commit().Error + }, }, }, }, { @@ -1804,6 +1845,47 @@ GLOBAL OPTIONS: return UserGroupsByIdentifiers(db, c.Args()).Delete(&UserGroup{}).Error }, + }, { + Name: "update", + Usage: "Updates a user group", + ArgsUsage: "USERGROUP...", + Flags: []cli.Flag{ + cli.StringFlag{Name: "name", Usage: "Assigns a new name to the user group"}, + cli.StringFlag{Name: "comment", Usage: "Adds a comment"}, + }, + Action: func(c *cli.Context) error { + if c.NArg() < 1 { + return cli.ShowSubcommandHelp(c) + } + + if err := myself.CheckRoles([]string{"admin"}); err != nil { + return err + } + + var usergroups []UserGroup + if err := UserGroupsByIdentifiers(db, c.Args()).Find(&usergroups).Error; err != nil { + return err + } + + if len(usergroups) > 1 && c.String("name") != "" { + return fmt.Errorf("cannot set --name when editing multiple usergroups at once") + } + + tx := db.Begin() + for _, usergroup := range usergroups { + model := tx.Model(&usergroup) + // simple fields + for _, fieldname := range []string{"name", "comment"} { + if c.String(fieldname) != "" { + if err := model.Update(fieldname, c.String(fieldname)).Error; err != nil { + tx.Rollback() + return err + } + } + } + } + return tx.Commit().Error + }, }, }, }, {