mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-09-13 14:59:09 +00:00
feat(resource): add expiry support (--expired, --clear-expired)
This commit is contained in:
parent
43cc96a43d
commit
072ae89378
3 changed files with 69 additions and 0 deletions
|
@ -25,6 +25,7 @@ func init() {
|
||||||
ResourceCreateCmd.Flags().StringP("password", "p", "", "Resource Password")
|
ResourceCreateCmd.Flags().StringP("password", "p", "", "Resource Password")
|
||||||
ResourceCreateCmd.Flags().StringP("description", "d", "", "Resource Description")
|
ResourceCreateCmd.Flags().StringP("description", "d", "", "Resource Description")
|
||||||
ResourceCreateCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to create the Resource")
|
ResourceCreateCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to create the Resource")
|
||||||
|
ResourceCreateCmd.Flags().String("expired", "", "Expiry date/time (ISO8601), e.g. 2025-12-31T23:59:59Z")
|
||||||
|
|
||||||
ResourceCreateCmd.MarkFlagRequired("name")
|
ResourceCreateCmd.MarkFlagRequired("name")
|
||||||
ResourceCreateCmd.MarkFlagRequired("password")
|
ResourceCreateCmd.MarkFlagRequired("password")
|
||||||
|
@ -55,6 +56,10 @@ func ResourceCreate(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
expired, err := cmd.Flags().GetString("expired")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
jsonOutput, err := cmd.Flags().GetBool("json")
|
jsonOutput, err := cmd.Flags().GetBool("json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -83,6 +88,12 @@ func ResourceCreate(cmd *cobra.Command, args []string) error {
|
||||||
return fmt.Errorf("Creating Resource: %w", err)
|
return fmt.Errorf("Creating Resource: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if expired != "" {
|
||||||
|
if err := SetResourceExpiry(ctx, client, id, expired); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput {
|
||||||
jsonId, err := json.MarshalIndent(
|
jsonId, err := json.MarshalIndent(
|
||||||
map[string]string{"id": id},
|
map[string]string{"id": id},
|
||||||
|
|
29
resource/expiry.go
Normal file
29
resource/expiry.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package resource
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/passbolt/go-passbolt/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetResourceExpiry updates only the expiry date of a resource.
|
||||||
|
func SetResourceExpiry(ctx context.Context, client *api.Client, id string, expired string) error {
|
||||||
|
if expired == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
_, _, err := client.DoCustomRequestAndReturnRawResponse(
|
||||||
|
ctx,
|
||||||
|
"PUT",
|
||||||
|
fmt.Sprintf("resources/%s.json", id),
|
||||||
|
"v2",
|
||||||
|
map[string]string{"expired": expired},
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Setting expiry: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,8 @@ func init() {
|
||||||
ResourceUpdateCmd.Flags().String("uri", "", "Resource URI")
|
ResourceUpdateCmd.Flags().String("uri", "", "Resource URI")
|
||||||
ResourceUpdateCmd.Flags().StringP("password", "p", "", "Resource Password")
|
ResourceUpdateCmd.Flags().StringP("password", "p", "", "Resource Password")
|
||||||
ResourceUpdateCmd.Flags().StringP("description", "d", "", "Resource Description")
|
ResourceUpdateCmd.Flags().StringP("description", "d", "", "Resource Description")
|
||||||
|
ResourceUpdateCmd.Flags().String("expired", "", "Expiry date/time (ISO8601), e.g. 2025-12-31T23:59:59Z; use empty to clear with --clear-expired")
|
||||||
|
ResourceUpdateCmd.Flags().Bool("clear-expired", false, "Clear expiry (sets expired to null)")
|
||||||
|
|
||||||
ResourceUpdateCmd.MarkFlagRequired("id")
|
ResourceUpdateCmd.MarkFlagRequired("id")
|
||||||
}
|
}
|
||||||
|
@ -53,6 +55,14 @@ func ResourceUpdate(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
expired, err := cmd.Flags().GetString("expired")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
clearExpired, err := cmd.Flags().GetBool("clear-expired")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
ctx := util.GetContext()
|
ctx := util.GetContext()
|
||||||
|
|
||||||
|
@ -76,5 +86,24 @@ func ResourceUpdate(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Updating Resource: %w", err)
|
return fmt.Errorf("Updating Resource: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if clearExpired {
|
||||||
|
// explicit clear to null
|
||||||
|
_, _, err := client.DoCustomRequestAndReturnRawResponse(
|
||||||
|
ctx,
|
||||||
|
"PUT",
|
||||||
|
fmt.Sprintf("resources/%s.json", id),
|
||||||
|
"v2",
|
||||||
|
map[string]*string{"expired": nil},
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Clearing expiry: %w", err)
|
||||||
|
}
|
||||||
|
} else if expired != "" {
|
||||||
|
if err := SetResourceExpiry(ctx, client, id, expired); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue