|
| 1 | +package group |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/aborroy/alfresco-cli/cmd" |
| 10 | + "github.com/aborroy/alfresco-cli/httpclient" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +const CreateGroupCmdId string = "[GROUP CREATE]" |
| 15 | + |
| 16 | +var displayName string |
| 17 | +var parentIds []string |
| 18 | +var groupCreateCmd = &cobra.Command{ |
| 19 | + Use: "create", |
| 20 | + Short: "Create new Group in ACS Repository", |
| 21 | + Long: `Creates a new group in the repository. |
| 22 | +The group can be created setting only required properties. |
| 23 | +When specifying parentIds, the group is created associated to those parentIds, not as a children of them.`, |
| 24 | + Run: func(command *cobra.Command, args []string) { |
| 25 | + |
| 26 | + var groupCreate GroupUpdate |
| 27 | + groupCreate.ID = groupId |
| 28 | + groupCreate.DisplayName = displayName |
| 29 | + if parentIds != nil { |
| 30 | + groupCreate.ParentIds = parentIds |
| 31 | + } |
| 32 | + jsonGroupCreate, _ := json.Marshal(groupCreate) |
| 33 | + |
| 34 | + execution := &httpclient.HttpExecution{ |
| 35 | + Method: http.MethodPost, |
| 36 | + Data: string(jsonGroupCreate), |
| 37 | + Format: httpclient.Json, |
| 38 | + Url: strings.TrimSuffix(groupsUrlPath, "/"), |
| 39 | + ResponseBodyOutput: &responseBody, |
| 40 | + } |
| 41 | + |
| 42 | + _error := httpclient.Execute(execution) |
| 43 | + if _error != nil { |
| 44 | + cmd.ExitWithError(CreateGroupCmdId, _error) |
| 45 | + } |
| 46 | + }, |
| 47 | + PostRun: func(command *cobra.Command, args []string) { |
| 48 | + log.Println(CreateGroupCmdId, "Group "+groupId+" has been created") |
| 49 | + }, |
| 50 | +} |
| 51 | + |
| 52 | +func init() { |
| 53 | + groupCmd.AddCommand(groupCreateCmd) |
| 54 | + groupCreateCmd.Flags().StringVarP(&groupId, "groupId", "i", "", "ID of the group in Alfresco Repository.") |
| 55 | + groupCreateCmd.Flags().StringVarP(&displayName, "displayName", "d", "", "Display name of the group in Alfresco Repository.") |
| 56 | + groupCreateCmd.Flags().StringArrayVarP(&parentIds, "parentIds", "p", nil, "List containing the IDs of parent groups.") |
| 57 | + groupCreateCmd.Flags().SortFlags = false |
| 58 | + groupCreateCmd.MarkFlagRequired("groupId") |
| 59 | + |
| 60 | +} |
0 commit comments